Compare commits

..

14 Commits

3 changed files with 42 additions and 47 deletions

View File

@@ -1,4 +1,4 @@
import { createContext, useCallback, useContext, useState } from "react"; import { createContext, useContext, useState } from "react";
interface ModalState { interface ModalState {
contentType: string; contentType: string;
@@ -42,59 +42,57 @@ export const ModalProvider: React.FC<Props> = ({ children }) => {
const entityId = currentModal?.entityId || ""; const entityId = currentModal?.entityId || "";
const entityLabel = currentModal?.entityLabel || ""; const entityLabel = currentModal?.entityLabel || "";
const openModal = useCallback( const openModal = (
(contentType: string, entityId?: string, entityLabel?: string) => { contentType: string,
const newModal: ModalState = { contentType, entityId, entityLabel }; entityId?: string,
setModalStack((prev) => [...prev, newModal]); entityLabel?: string,
}, ) => {
[], const newModal: ModalState = { contentType, entityId, entityLabel };
); setModalStack((prev) => [...prev, newModal]);
};
const closeModal = useCallback(() => { const closeModal = () => {
setModalStack((prev) => { setModalStack((prev) => {
if (prev.length <= 1) { if (prev.length <= 1) {
return []; return [];
} }
return prev.slice(0, -1); return prev.slice(0, -1);
}); });
}, []); };
const closeModals = useCallback((count: number) => { const closeModals = (count: number) => {
setModalStack((prev) => { setModalStack(prev => {
const newLength = Math.max(0, prev.length - count); const newLength = Math.max(0, prev.length - count);
return prev.slice(0, newLength); return prev.slice(0, newLength);
}); });
}, []); };
const clearAllModals = useCallback(() => { const clearAllModals = () => {
setModalStack([]); setModalStack([]);
}, []); };
const setModalState = useCallback((modalType: string, state: any) => { const setModalState = (modalType: string, state: any) => {
setModalStates((prev) => ({ setModalStates((prev) => ({
...prev, ...prev,
[modalType]: state, [modalType]: state,
})); }));
}, []); };
const getModalState = useCallback( const getModalState = (modalType: string) => {
(modalType: string) => { return modalStates[modalType];
return modalStates[modalType]; };
},
[modalStates],
);
const clearModalState = useCallback((modalType: string) => { const clearModalState = (modalType: string) => {
setModalStates((prev) => { setModalStates((prev) => {
const newStates = { ...prev }; const newStates = { ...prev };
delete newStates[modalType]; delete newStates[modalType];
return newStates; return newStates;
}); });
}, []); };
const clearAllModalStates = useCallback(() => { const clearAllModalStates = () => {
setModalStates({}); setModalStates({});
}, []); };
return ( return (
<ModalContext.Provider <ModalContext.Provider

View File

@@ -47,16 +47,12 @@ export const WorkspaceProvider: React.FC<{ children: ReactNode }> = ({
const [hasLoaded, setHasLoaded] = useState(false); const [hasLoaded, setHasLoaded] = useState(false);
const { data, isLoading } = api.workspace.all.useQuery(); const { data, isLoading } = api.workspace.all.useQuery();
const utils = api.useUtils();
const switchWorkspace = (_workspace: Workspace) => { const switchWorkspace = (_workspace: Workspace) => {
localStorage.setItem("workspacePublicId", _workspace.publicId); localStorage.setItem("workspacePublicId", _workspace.publicId);
setWorkspace(_workspace); setWorkspace(_workspace);
// Refetch workspace data to ensure availableWorkspaces is up to date
void utils.workspace.all.refetch();
router.push(`/boards`); router.push(`/boards`);
}; };
@@ -70,15 +66,21 @@ export const WorkspaceProvider: React.FC<{ children: ReactNode }> = ({
localStorage.getItem("workspacePublicId"); localStorage.getItem("workspacePublicId");
if (data.length) { if (data.length) {
const workspaces = data.map(({ workspace, role }) => ({ const workspaces = data
role, .map(({ workspace, role }) => {
publicId: workspace.publicId, if (!workspace) return;
name: workspace.name,
slug: workspace.slug, return {
description: workspace.description, role,
plan: workspace.plan, publicId: workspace.publicId,
hasLoaded: true, name: workspace.name,
})) as Workspace[]; slug: workspace.slug,
description: workspace.description,
plan: workspace.plan,
hasLoaded: true,
};
})
.filter((workspace) => workspace !== null) as Workspace[];
if (workspaces.length) setAvailableWorkspaces(workspaces); if (workspaces.length) setAvailableWorkspaces(workspaces);
} }
@@ -114,7 +116,7 @@ export const WorkspaceProvider: React.FC<{ children: ReactNode }> = ({
role: primaryWorkspaceRole, role: primaryWorkspaceRole,
}); });
} }
}, [data, isLoading]); }, [data]);
return ( return (
<WorkspaceContext.Provider <WorkspaceContext.Provider

View File

@@ -1,5 +1,4 @@
import { t } from "@lingui/core/macro"; import { t } from "@lingui/core/macro";
import { useEffect } from "react";
import { HiArrowDownTray, HiOutlinePlusSmall } from "react-icons/hi2"; import { HiArrowDownTray, HiOutlinePlusSmall } from "react-icons/hi2";
import Button from "~/components/Button"; import Button from "~/components/Button";
@@ -17,11 +16,7 @@ export default function BoardsPage() {
const { openModal, modalContentType, isOpen } = useModal(); const { openModal, modalContentType, isOpen } = useModal();
const { availableWorkspaces, workspace, hasLoaded } = useWorkspace(); const { availableWorkspaces, workspace, hasLoaded } = useWorkspace();
useEffect(() => { if (hasLoaded && availableWorkspaces.length === 0) openModal("NEW_WORKSPACE");
if (hasLoaded && availableWorkspaces.length === 0) {
openModal("NEW_WORKSPACE");
}
}, [hasLoaded, availableWorkspaces.length, openModal]);
return ( return (
<> <>