fix(desktop): route project/session selection through single draft-store writers

Merged
KI
4mo ago
+67-78

Problem

Clicking sidebar Sessions "+" and then a project's "+" left the new-workspace draft in a stale state: the picker showed the project (e.g. Superset) but isSession stayed true, so submitting with a linked PR failed with "Checking out a PR requires a project" until the project was re-picked from the dropdown.

Root cause

The selectedProjectId/isSession pair in new-workspace-draft.ts had four copy-pasted writers (picker callback + preselection effect, in both the screen and modal variants) that must stay in sync, and they drifted: the screen's project-preselection branch set the id without clearing isSession. The pill renders the project name whenever the id resolves, so the inconsistency was invisible until submit.

Fix

  • Add selectProject(projectId) / selectSession() actions to the draft store — the only writers of the pair. Selecting a project always leaves session mode; selecting a session always clears the repo-scoped inputs (linked PR, base branch).
  • Convert all call sites: both variants' pickers and preselection effects, plus the four PR/issue "Add to workspace" pages.
  • Re-arm the screen's preselection ref when the projectId search param clears: the screen stays mounted across search-param changes (unlike the modal, which resets refs on close), so Sessions "+" → the same project "+" previously skipped re-applying and stayed stuck in session mode.
  • Simplify resetDraft to rely on zustand's merge semantics instead of hand-preserving each action.

CDP verification (dev app from this worktree, real mouse/keyboard input, evidence gate)

CaseBefore fixAfter fix
Sessions "+" → project "+" → link PR → submit❌ toast "Checking out a PR requires a project", draft {selectedProjectId: set, isSession: true}✅ creates PR-checkout workspace under the project, navigates to it
Sessions "+" → same project "+" (re-arm)stuck in session mode✅ {p: set, sess: false}, pill shows project
Dropdown "No project" with PR linked—✅ clears linked PR + base branch, PR pill disappears
Session create (Sessions "+" → prompt → submit)—✅ project-less session workspace created, no toast
Manual project switch after "+" preselect—✅ no snap-back after 2s (ref guard intact)
PR list "Add to workspace"—✅ draft {p: set, sess: false, pr: 8883}, resetDraft cleared prior prompt

Not UI-tested: the modal (control) variant — it's behind the new-workspace-screen experiment (test arm active in dev); its call sites got the identical mechanical conversion. Test workspaces created during verification were deleted.

🤖 Generated with Claude Code


Summary by cubic

Fixes a stale draft when creating a workspace where the picker showed a project but the draft stayed in session mode, causing PR checkout to fail with "Checking out a PR requires a project." All project/session selection now goes through single store actions to keep state consistent across the screen, modal, and "Add to workspace" flows.

  • Bug Fixes

    • Centralized selection with selectProject(projectId) and selectSession() to keep selectedProjectId/isSession in sync and clear repo-scoped inputs when choosing a session.
    • Updated all pickers and preselection effects; re-arms project preselection when the projectId search param clears so Sessions "+" → same project "+" applies correctly.
  • Refactors

    • Simplified resetDraft using zustand merge semantics so actions persist across resets.
    • Replaced ad-hoc updates with the new actions across the screen, modal, and PR/issue "Add to workspace" pages.

Summary by CodeRabbit

  • Improvements
    • Improved new workspace setup when starting from pull requests or issues.
    • Project and session selections now update consistently and automatically clear conflicting workspace details.
    • Switching between project and session modes now prevents stale pull request, branch, or repository information from carrying over.