Skip to content

Commit 6b92347

Browse files
committed
fix worksapce switch
1 parent de9e833 commit 6b92347

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

apps/sim/app/api/workflows/route.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ vi.mock('@/lib/core/telemetry', () => ({
8787
},
8888
}))
8989

90+
vi.mock('@/lib/workflows/defaults', () => ({
91+
buildDefaultWorkflowArtifacts: vi.fn().mockReturnValue({
92+
workflowState: { blocks: {}, edges: [], loops: {}, parallels: {} },
93+
subBlockValues: {},
94+
startBlockId: 'start-block-id',
95+
}),
96+
}))
97+
98+
vi.mock('@/lib/workflows/persistence/utils', () => ({
99+
saveWorkflowToNormalizedTables: vi.fn().mockResolvedValue({ success: true }),
100+
}))
101+
90102
import { POST } from '@/app/api/workflows/route'
91103

92104
describe('Workflows API Route - POST ordering', () => {

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workspace-management.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function useWorkspaceManagement({
3939
const {
4040
data: workspaces = [],
4141
isLoading: isWorkspacesLoading,
42+
isFetching: isWorkspacesFetching,
4243
refetch: refetchWorkspaces,
4344
} = useWorkspacesQuery(Boolean(sessionUserId))
4445

@@ -71,14 +72,17 @@ export function useWorkspaceManagement({
7172
const matchingWorkspace = workspaces.find((w) => w.id === currentWorkspaceId)
7273

7374
if (!matchingWorkspace) {
75+
if (isWorkspacesFetching) {
76+
return
77+
}
7478
logger.warn(`Workspace ${currentWorkspaceId} not found in user's workspaces`)
7579
const fallbackWorkspace = workspaces[0]
7680
logger.info(`Redirecting to fallback workspace: ${fallbackWorkspace.id}`)
7781
routerRef.current?.push(`/workspace/${fallbackWorkspace.id}/home`)
7882
}
7983

8084
hasValidatedRef.current = true
81-
}, [workspaces, isWorkspacesLoading])
85+
}, [workspaces, isWorkspacesLoading, isWorkspacesFetching])
8286

8387
const refreshWorkspaceList = useCallback(async () => {
8488
await queryClient.invalidateQueries({ queryKey: workspaceKeys.lists() })

apps/sim/hooks/queries/workspace.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ interface CreateWorkspaceParams {
6565

6666
/**
6767
* Creates a new workspace.
68-
* Automatically invalidates the workspace list cache on success.
68+
* Merges the created row into the active list cache before invalidation so navigation
69+
* cannot race a stale list (see workspace validation fallback in use-workspace-management).
6970
*/
7071
export function useCreateWorkspace() {
7172
const queryClient = useQueryClient()
@@ -86,7 +87,16 @@ export function useCreateWorkspace() {
8687
const data = await response.json()
8788
return data.workspace as Workspace
8889
},
89-
onSuccess: () => {
90+
onSuccess: (newWorkspace) => {
91+
queryClient.setQueryData<Workspace[]>(workspaceKeys.list('active'), (previous) => {
92+
if (!previous?.length) {
93+
return [newWorkspace]
94+
}
95+
if (previous.some((w) => w.id === newWorkspace.id)) {
96+
return previous
97+
}
98+
return [newWorkspace, ...previous]
99+
})
90100
queryClient.invalidateQueries({ queryKey: workspaceKeys.lists() })
91101
queryClient.invalidateQueries({ queryKey: workspaceKeys.adminLists() })
92102
},

0 commit comments

Comments
 (0)