()
| 20 | } |
| 21 | |
| 22 | export const useSquadNavigation = (): UseSquadNavigation => { |
| 23 | const { user, showLogin } = useAuthContext(); |
| 24 | const router = useRouter(); |
| 25 | const { openModal } = useLazyModal(); |
| 26 | |
| 27 | const newSquadUrl = `${webappUrl}squads/new`; |
| 28 | |
| 29 | const openNewSquad = useCallback( |
| 30 | (props?: OpenNewSquadProps) => { |
| 31 | if (!user) { |
| 32 | props?.event?.preventDefault(); |
| 33 | showLogin({ trigger: AuthTriggers.CreateSquad }); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | props?.event?.preventDefault(); |
| 38 | |
| 39 | if (props?.pageRedirect) { |
| 40 | router.push(`${newSquadUrl}?origin=${props.origin}`); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | openModal({ type: LazyModal.NewSquad }); |
| 45 | }, |
| 46 | [user, openModal, showLogin, router, newSquadUrl], |
| 47 | ); |
| 48 | |
| 49 | const editSquad = useCallback( |
| 50 | ({ handle }: EditSquadProps) => { |
| 51 | router.push(`${webappUrl}squads/${handle}/edit`); |
| 52 | }, |
| 53 | [router], |
| 54 | ); |
| 55 | |
| 56 | useEffect(() => { |
| 57 | const search = new URLSearchParams(window.location.search); |
| 58 | const query = Object.fromEntries(search); |
| 59 | if (!query?.squad) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const { origin, pathname } = window.location; |
| 64 | openNewSquad({ origin: Origin.Notification }); |
| 65 | router.replace(origin + pathname); |
| 66 | // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM |
| 67 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 68 | }, [router.pathname]); |
| 69 | |
| 70 | return { openNewSquad, editSquad, newSquadUrl }; |
| 71 | }; |
no test coverage detected