diff --git a/apps/web/src/providers/modal.tsx b/apps/web/src/providers/modal.tsx index 69363037..e57ed1f6 100644 --- a/apps/web/src/providers/modal.tsx +++ b/apps/web/src/providers/modal.tsx @@ -1,4 +1,4 @@ -import { createContext, useContext, useState } from "react"; +import { createContext, useCallback, useContext, useState } from "react"; interface ModalState { contentType: string; @@ -42,57 +42,59 @@ export const ModalProvider: React.FC = ({ children }) => { const entityId = currentModal?.entityId || ""; const entityLabel = currentModal?.entityLabel || ""; - const openModal = ( - contentType: string, - entityId?: string, - entityLabel?: string, - ) => { - const newModal: ModalState = { contentType, entityId, entityLabel }; - setModalStack((prev) => [...prev, newModal]); - }; + const openModal = useCallback( + (contentType: string, entityId?: string, entityLabel?: string) => { + const newModal: ModalState = { contentType, entityId, entityLabel }; + setModalStack((prev) => [...prev, newModal]); + }, + [], + ); - const closeModal = () => { + const closeModal = useCallback(() => { setModalStack((prev) => { if (prev.length <= 1) { return []; } return prev.slice(0, -1); }); - }; + }, []); - const closeModals = (count: number) => { - setModalStack(prev => { + const closeModals = useCallback((count: number) => { + setModalStack((prev) => { const newLength = Math.max(0, prev.length - count); return prev.slice(0, newLength); }); - }; + }, []); - const clearAllModals = () => { + const clearAllModals = useCallback(() => { setModalStack([]); - }; + }, []); - const setModalState = (modalType: string, state: any) => { + const setModalState = useCallback((modalType: string, state: any) => { setModalStates((prev) => ({ ...prev, [modalType]: state, })); - }; + }, []); - const getModalState = (modalType: string) => { - return modalStates[modalType]; - }; + const getModalState = useCallback( + (modalType: string) => { + return modalStates[modalType]; + }, + [modalStates], + ); - const clearModalState = (modalType: string) => { + const clearModalState = useCallback((modalType: string) => { setModalStates((prev) => { const newStates = { ...prev }; delete newStates[modalType]; return newStates; }); - }; + }, []); - const clearAllModalStates = () => { + const clearAllModalStates = useCallback(() => { setModalStates({}); - }; + }, []); return ( = ({ const [hasLoaded, setHasLoaded] = useState(false); const { data, isLoading } = api.workspace.all.useQuery(); + const utils = api.useUtils(); const switchWorkspace = (_workspace: Workspace) => { localStorage.setItem("workspacePublicId", _workspace.publicId); setWorkspace(_workspace); + // Refetch workspace data to ensure availableWorkspaces is up to date + void utils.workspace.all.refetch(); + router.push(`/boards`); }; @@ -66,21 +70,15 @@ export const WorkspaceProvider: React.FC<{ children: ReactNode }> = ({ localStorage.getItem("workspacePublicId"); if (data.length) { - const workspaces = data - .map(({ workspace, role }) => { - if (!workspace) return; - - return { - role, - publicId: workspace.publicId, - name: workspace.name, - slug: workspace.slug, - description: workspace.description, - plan: workspace.plan, - hasLoaded: true, - }; - }) - .filter((workspace) => workspace !== null) as Workspace[]; + const workspaces = data.map(({ workspace, role }) => ({ + role, + publicId: workspace.publicId, + name: workspace.name, + slug: workspace.slug, + description: workspace.description, + plan: workspace.plan, + hasLoaded: true, + })) as Workspace[]; if (workspaces.length) setAvailableWorkspaces(workspaces); } @@ -116,7 +114,7 @@ export const WorkspaceProvider: React.FC<{ children: ReactNode }> = ({ role: primaryWorkspaceRole, }); } - }, [data]); + }, [data, isLoading]); return ( { + if (hasLoaded && availableWorkspaces.length === 0) { + openModal("NEW_WORKSPACE"); + } + }, [hasLoaded, availableWorkspaces.length, openModal]); return ( <>