Skip to content

feat(workspaces): add recency-based workspace switching and redirect#4131

Merged
waleedlatif1 merged 2 commits intostagingfrom
waleedlatif1/workspace-switcher-redesign
Apr 13, 2026
Merged

feat(workspaces): add recency-based workspace switching and redirect#4131
waleedlatif1 merged 2 commits intostagingfrom
waleedlatif1/workspace-switcher-redesign

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Workspaces in the sidebar dropdown are now ordered by recency (most recently visited first)
  • Opening Sim redirects to the last active workspace using a hybrid localStorage + server-side approach
  • Added lastActiveWorkspaceId column to settings table with debounced server sync
  • Replaced manual useState loading booleans with React Query mutation.isPending
  • Removed explicit refetching in delete/leave handlers — relies on mutation onSuccess cache invalidation
  • Prevent deletion of the last workspace (server-side 400 guard + UI disables the button)
  • Fixed setQueryData cache shape after fetchWorkspaces return type changed to WorkspacesResponse
  • Cleaned up dead code: removed unused hook returns, stale type re-exports, redundant wrapper functions

Type of Change

  • New feature
  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 13, 2026 7:53pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 13, 2026

PR Summary

Medium Risk
Changes workspace list API shape and client-side navigation/redirect logic, which could affect workspace selection and deletion flows if cache or recency syncing behaves unexpectedly. Adds a new settings DB column and server-side guard against deleting the last workspace.

Overview
Adds recency-aware workspace UX: the sidebar workspace dropdown is now ordered by recently visited workspaces, and /workspace redirects to the most appropriate workspace (local localStorage recency, then server-stored lastActiveWorkspaceId, then first workspace).

Persists last active workspace via a new settings.last_active_workspace_id column, exposed through /api/users/me/settings and included in /api/workspaces responses; client syncs it with a debounced PATCH.

Prevents users from deleting their final remaining workspace (server-side 400 + UI disables delete when only one workspace remains), and refactors workspace management UI to rely on React Query mutation pending states and updated cache shape for the new workspaces response metadata.

Reviewed by Cursor Bugbot for commit 08400f5. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 13, 2026

Greptile Summary

Adds recency-based workspace switching using a hybrid localStorage + server-side lastActiveWorkspaceId setting, guarded workspace deletion (server-side 400 + client-side UI disable), and cleans up manual loading state in favour of mutation.isPending. The blank-page regression from the prior review thread is addressed via handleWorkflowRedirect accepting a fallbackWorkspaceId.

Confidence Score: 5/5

Safe to merge — all remaining findings are P2 style suggestions

The feature is correctly implemented end-to-end: redirect logic has proper fallbacks, the delete guard is enforced server-side, cache shape is fixed, and prior review concerns are resolved. The only open finding is a convention-level style issue (direct fetch instead of a React Query mutation for the debounced settings sync) that does not affect correctness.

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workspace-management.ts — direct fetch in touchRecency

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workspace-management.ts Adds recency tracking, debounced server sync, and sorted workspace list; direct fetch in touchRecency violates React Query mutation convention
apps/sim/app/workspace/page.tsx Redirect page correctly resolves target workspace via localStorage recency → server lastActiveWorkspaceId → workspaces[0]; handleWorkflowRedirect now takes a fallback ID, fixing the blank-page regression from the prior review
apps/sim/hooks/queries/workspace.ts Adds useWorkspacesWithMetadata, exposes lastActiveWorkspaceId from API response, and fixes setQueryData shape to match WorkspacesResponse
apps/sim/lib/core/utils/browser-storage.ts Adds WorkspaceRecencyStorage with touch, getMostRecent, remove, prune, and sortByRecency; clean, well-typed, SSR-safe
apps/sim/app/api/workspaces/[id]/route.ts DELETE handler gains a server-side guard preventing deletion of the last workspace; logic is correct and TSDoc comment clarifies the non-admin count intent
apps/sim/app/api/workspaces/route.ts GET handler now fetches lastActiveWorkspaceId from settings in parallel with workspace list and includes it in the response
packages/db/schema.ts Adds nullable lastActiveWorkspaceId text column to the settings table; migration SQL is minimal and safe

Sequence Diagram

sequenceDiagram
    participant U as User
    participant P as workspace/page.tsx
    participant LS as WorkspaceRecencyStorage
    participant API as GET /api/workspaces
    participant WH as WorkspaceHeader
    participant UWM as useWorkspaceManagement
    participant SAPI as PATCH /api/users/me/settings

    U->>P: Navigates to /workspace
    P->>API: fetchWorkspaces (useWorkspacesWithMetadata)
    API-->>P: { workspaces[], lastActiveWorkspaceId }
    P->>LS: getMostRecent()
    LS-->>P: localRecentId
    P->>P: resolve target = localRecentId ?? lastActiveWorkspaceId ?? workspaces[0]
    P->>U: router.replace(/workspace/{target}/home)

    U->>WH: Opens workspace switcher dropdown
    WH->>UWM: onWorkspaceSwitch(workspace)
    UWM->>LS: touch(workspaceId)
    Note over UWM,SAPI: debounced 1s
    UWM->>SAPI: PATCH lastActiveWorkspaceId
    UWM->>UWM: setRecencySortKey(k+1) re-sort dropdown
Loading

Reviews (7): Last reviewed commit: "fix(workspaces): skip prune when workspa..." | Re-trigger Greptile

@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/workspace-switcher-redesign branch from f13c434 to 2309c9d Compare April 13, 2026 19:05
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/workspace-switcher-redesign branch from 2309c9d to f14fe67 Compare April 13, 2026 19:24
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 08400f5. Configure here.

@waleedlatif1 waleedlatif1 merged commit 5ab85c6 into staging Apr 13, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/workspace-switcher-redesign branch April 13, 2026 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant