From 6b347072947de6dc28487a06c81818ea293d3a0e Mon Sep 17 00:00:00 2001 From: adambarito Date: Tue, 17 Dec 2024 18:19:43 -0500 Subject: [PATCH] Improve/app/workspace switcher (#263) * optimistically set current workspace w/ switcher - standardized infinite item context - when workspace is switched, immediately switch to existing query data for that workspace (if it exists, even if stale) or set to empty (if that query hasn't been run yet). then invalidate route. this is currently done for all {item}.byWorkspace routers - use GridSkeleton for empty placeholder if query is actively fetching * close workspace switcher before async invalidate * remove console.logs from useSetWorkspace * return early when pushing to currentPath w/ new handle - if currentWorkspace.handle === workspace.handle, return before any currentPath nonsense - accidentally lost the 'return' when deleting the console.log --- packages/lib/hooks/use-workspace.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/lib/hooks/use-workspace.ts b/packages/lib/hooks/use-workspace.ts index 48b8dedf..6fd2cc48 100644 --- a/packages/lib/hooks/use-workspace.ts +++ b/packages/lib/hooks/use-workspace.ts @@ -28,6 +28,8 @@ export function useSetWorkspace({ onBeginSet }: { onBeginSet?: () => void }) { onBeginSet?.(); + if (currentWorkspace.handle === workspace.handle) return; + if (currentPath?.endsWith('carts')) { apiUtils.cartFunnel.byWorkspace.setInfiniteData( { handle: workspace.handle }, @@ -311,13 +313,12 @@ export function useSetWorkspace({ onBeginSet }: { onBeginSet?: () => void }) { await apiUtils.track.byWorkspace.invalidate(); } - if (currentWorkspace.handle === workspace.handle) return; if (currentPath) { - router.push(currentPath.replace(currentWorkspace.handle, workspace.handle)); - // return console.log('pushed @', Date.now()); + return router.push( + currentPath.replace(currentWorkspace.handle, workspace.handle), + ); } router.push(`/${workspace.handle}`); - // console.log('pushed @', Date.now()); }, [apiUtils, currentPath, currentWorkspace, router, onBeginSet], );