Skip to content

fix: remember last active sidebar tab per agent session - #24631

Merged
jaaydenh merged 9 commits into
mainfrom
tab-state-g9dc
Apr 27, 2026
Merged

jaaydenh merged 9 commits into
mainfrom
tab-state-g9dc

Conversation

@jaaydenh

@jaaydenh jaaydenh commented Apr 22, 2026 •

Copy link
Copy Markdown
Collaborator

closes CODAGT-142

The Agents right-panel tabs (Git, Terminal, Desktop) reset to the
default tab (Git) every time the user switches between agent sessions.
This happens because KeyedAgentChatPage forces a full remount on
agentId change, discarding the useState that holds the active tab.

Persist the active tab per agent session in localStorage, following
the existing agents.draft-input.<chatID> pattern. When the user
returns to a session, the last tab they selected is restored. If the
stored tab is temporarily unavailable (e.g. Desktop while the workspace
is stopped), SidebarTabView's existing fallback to the first
available tab applies, and the stored value survives so it can be
honoured once that tab reappears.

Archiving a chat clears its stored tab entry so unarchiving starts
fresh with the default tab.

@jaaydenh jaaydenh changed the title fix(site/pages/AgentsPage): remember last active sidebar tab per agent session fix(site/src/pages/AgentsPage): remember last active sidebar tab per agent session Apr 22, 2026
@jaaydenh

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Solid, focused fix. The localStorage persistence follows the existing agents.draft-input.<chatID> pattern exactly, the lifecycle is complete (create on click, restore on mount, clear on archive), and the test strategy splits correctly between vitest for pure helpers and Storybook for integration. The getEffectiveTabId fallback handles stale/unavailable tabs gracefully without clobbering the stored value. Tests pass (46/46).

Two P3s, five nits. The P3s are a naming inconsistency that makes the archive call site look like a bug, and a missing story for the PR description's claimed "tab survives unavailability" behavior.

"A developer reading that line has to chase the function signature, see agentID, wonder if the wrong ID type was passed, then trace through to learn that agentId and chatId are the same value in this codebase." (Leorio)

🤖 This review was automatically generated with Coder Agents.

@jaaydenh jaaydenh changed the title fix(site/src/pages/AgentsPage): remember last active sidebar tab per agent session fix: remember last active sidebar tab per agent session Apr 23, 2026
@jaaydenh

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7550b73bf6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Add localStorage-backed helpers to save, restore, and clear the
active right-panel tab per agent session (keyed by agent ID).

- AgentChatPage.tsx: new exported helpers getPersistedSidebarTabId,
  savePersistedSidebarTabId, clearPersistedSidebarTabId, and the
  lastActiveSidebarTabStorageKeyPrefix constant.
- AgentChatPageView.tsx: initialize sidebarTabId from localStorage
  via getPersistedSidebarTabId; wrap setter in useCallback that
  persists on every tab change via savePersistedSidebarTabId.
- AgentsPage.tsx: call clearPersistedSidebarTabId in both
  archiveAgentMutation and archiveAndDeleteMutation onSuccess
  handlers to clean up stale entries.
- AgentChatPage.test.ts: 15 unit tests covering get/save/clear
  helpers including edge cases (undefined, empty string, no-op,
  round-trip, cross-agent isolation).
… persistence

Add two play-function stories to AgentChatPageView.stories.tsx:

- RestoresPersistedSidebarTab: Seeds localStorage with a persisted
  'terminal' tab ID before mount and verifies the Terminal tab is
  selected instead of the default Git tab on render.

- PersistsSidebarTabClick: Starts with empty localStorage, clicks the
  Terminal tab, and asserts both the aria-selected attribute and the
  localStorage value are updated.

Both stories use beforeEach/cleanup to manage localStorage state and
provide workspace + workspaceAgent props so the Terminal tab is
available in the sidebar.

Archive cleanup story was intentionally skipped because the archive
flow in AgentsPage.tsx requires substantial mock infrastructure, and
the clearPersistedSidebarTabId helper is already covered by unit
tests.
- Rename agentID parameter to chatID across the three new helpers
  to match the sibling getPersistedDraftInputValue and the
  clearPersistedSidebarTabId(chatId) archive call site. (DEREM-1)
- Replace useCallback wrapper around setSidebarTabId with a plain
  arrow function. babel-plugin-react-compiler auto-memoizes closures
  under site/src/pages/AgentsPage. (DEREM-3)
- Add @internal annotation and correct the wording on
  lastActiveSidebarTabStorageKeyPrefix (right-panel -> sidebar,
  per-session -> per-chat). (DEREM-4, DEREM-5)
- Add JSDoc to the three new helpers to match the sibling
  getPersistedDraftInputValue. (DEREM-6)
- Drop the redundant localStorage.getItem assertion that was
  testing the test harness rather than the helper. (DEREM-7)
- Add PreservesUnavailableSidebarTab story that locks down the
  contract that a stored tab ID survives when its tab is not
  currently available, so getEffectiveTabId's read-only fallback
  cannot silently regress. (DEREM-2)
After archiving a chat, archiveAgentMutation.onSuccess calls
clearPersistedSidebarTabId(chatId) so a subsequent unarchive starts
from the default tab. The plain archive flow (via the chat menu) does
not navigate away, so the user remains on the now-read-only chat. If
the user clicks a sidebar tab in that state, savePersistedSidebarTabId
would silently recreate the entry, defeating the archive cleanup.

Gate savePersistedSidebarTabId on !isArchived. React local state still
switches for immediate visual feedback; only the persistence step is
skipped.

Add DoesNotPersistForArchivedChat storybook story that seeds an empty
localStorage, renders with isArchived=true, clicks the Terminal tab,
and asserts the entry stays null.

Addresses the codex P2 comment on #24631.
@jaaydenh
jaaydenh marked this pull request as ready for review April 24, 2026 10:22
@jaaydenh
jaaydenh merged commit 23b30b7 into main Apr 27, 2026
27 checks passed
@jaaydenh
jaaydenh deleted the tab-state-g9dc branch April 27, 2026 09:38
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants