File
Blob: src/client/hooks/use-shared-inbox-navigation.ts
| 1 | import { useCallback } from "react"; |
| 2 | import { useLocation, useNavigate } from "@tanstack/react-router"; |
| 3 | import { getSharedInboxReturnTo, withSharedInboxReturnTo } from "@/client/lib/shared-inbox-navigation"; |
| 4 | import { useMemberWorkspaceCount } from "@/client/stores/workspace-directory"; |
| 5 | |
| 6 | interface SharedInboxNavigationOptions { |
| 7 | returnTo?: string | null; |
| 8 | } |
| 9 | |
| 10 | export function useSharedInboxNavigation(options?: SharedInboxNavigationOptions) { |
| 11 | const navigate = useNavigate(); |
| 12 | const location = useLocation(); |
| 13 | const memberWorkspaceCount = useMemberWorkspaceCount(); |
| 14 | const isSharedInbox = location.pathname === "/shared-with-me"; |
| 15 | const returnTo = options?.returnTo ?? getSharedInboxReturnTo(location.state); |
| 16 | const canLeaveSharedInbox = Boolean(returnTo || memberWorkspaceCount > 0); |
| 17 | const backLabel = returnTo ? "Back" : memberWorkspaceCount > 0 ? "Back to workspaces" : null; |
| 18 | |
| 19 | const openSharedInbox = useCallback(() => { |
| 20 | navigate({ |
| 21 | to: "/shared-with-me", |
| 22 | state: (prev) => withSharedInboxReturnTo(prev, location.href), |
| 23 | }); |
| 24 | }, [location.href, navigate]); |
| 25 | |
| 26 | const leaveSharedInbox = useCallback(() => { |
| 27 | if (returnTo) { |
| 28 | navigate({ href: returnTo, replace: true }); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (memberWorkspaceCount > 0) { |
| 33 | navigate({ to: "/", replace: true }); |
| 34 | } |
| 35 | }, [memberWorkspaceCount, navigate, returnTo]); |
| 36 | |
| 37 | const toggleSharedInbox = useCallback(() => { |
| 38 | if (isSharedInbox) { |
| 39 | leaveSharedInbox(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | openSharedInbox(); |
| 44 | }, [isSharedInbox, leaveSharedInbox, openSharedInbox]); |
| 45 | |
| 46 | return { |
| 47 | backLabel, |
| 48 | canLeaveSharedInbox, |
| 49 | isSharedInbox, |
| 50 | leaveSharedInbox, |
| 51 | toggleSharedInbox, |
| 52 | }; |
| 53 | } |