diff --git a/src/renderer/src/components/CodexRestartChip.tsx b/src/renderer/src/components/CodexRestartChip.tsx index d2455f0c7..7530ee3ff 100644 --- a/src/renderer/src/components/CodexRestartChip.tsx +++ b/src/renderer/src/components/CodexRestartChip.tsx @@ -1,7 +1,15 @@ +import { useEffect, useId, useRef, useState } from 'react' import { RefreshCw } from 'lucide-react' import { useShallow } from 'zustand/react/shallow' +import { Button } from '@/components/ui/button' import { useAppStore } from '../store' import { translate } from '@/i18n/i18n' +import { shouldFocusMobileDriverAction } from './terminal-pane/mobile-driver-overlay-focus' +import { + buildCodexRestartNoticeKey, + createCodexRestartOverlayCollapseState, + getCodexRestartOverlayCollapseState +} from './codex-restart-overlay-collapse' const EMPTY_TABS: { id: string }[] = [] @@ -49,54 +57,205 @@ export function dismissStaleWorktreePtyIds( } } +function isInsideHiddenTree(element: HTMLElement): boolean { + return element.closest('[aria-hidden="true"], [hidden], [inert]') !== null +} + +type RestartNotice = { + previousAccountLabel: string + nextAccountLabel: string +} + export default function CodexRestartChip({ + isVisible = true, worktreeId }: { + isVisible?: boolean worktreeId: string }): React.JSX.Element | null { - const staleWorktreePtyIds = useAppStore( - useShallow((s) => - collectStalePtyIdsForTabs({ + const { staleWorktreePtyIds, restartNotice } = useAppStore( + useShallow((s) => { + const stalePtyIds = collectStalePtyIdsForTabs({ tabs: s.tabsByWorktree[worktreeId] ?? EMPTY_TABS, ptyIdsByTabId: s.ptyIdsByTabId, codexRestartNoticeByPtyId: s.codexRestartNoticeByPtyId }) - ) + const firstStalePtyId = stalePtyIds[0] + return { + staleWorktreePtyIds: stalePtyIds, + restartNotice: firstStalePtyId ? s.codexRestartNoticeByPtyId[firstStalePtyId] : undefined + } + }) ) const queueCodexPaneRestarts = useAppStore((s) => s.queueCodexPaneRestarts) const clearCodexRestartNotice = useAppStore((s) => s.clearCodexRestartNotice) - if (staleWorktreePtyIds.length === 0) { + const noticeKey = restartNotice ? buildCodexRestartNoticeKey(restartNotice) : null + const [collapseState, setCollapseState] = useState(() => + createCodexRestartOverlayCollapseState(noticeKey) + ) + + const currentCollapseState = getCodexRestartOverlayCollapseState(collapseState, noticeKey) + // Why: a new account switch must reopen loud mode even if the prior notice was collapsed. + if (currentCollapseState !== collapseState) { + setCollapseState(currentCollapseState) + } + + if (staleWorktreePtyIds.length === 0 || !restartNotice) { return null } + const handleRestart = (): void => { + queueCodexPaneRestarts(staleWorktreePtyIds) + } + + const handleDismiss = (): void => { + dismissStaleWorktreePtyIds(staleWorktreePtyIds, clearCodexRestartNotice) + } + + if (currentCollapseState.collapsed) { + return ( + setCollapseState(createCodexRestartOverlayCollapseState(noticeKey))} + onRestart={handleRestart} + /> + ) + } + return ( -
-
- + setCollapseState({ noticeKey, collapsed: true })} + onDismiss={handleDismiss} + onRestart={handleRestart} + /> + ) +} + +function LoudRestartOverlay({ + isVisible, + noticeKey, + restartNotice, + onCollapse, + onDismiss, + onRestart +}: { + isVisible: boolean + noticeKey: string | null + restartNotice: RestartNotice + onCollapse: () => void + onDismiss: () => void + onRestart: () => void +}): React.JSX.Element { + const titleId = useId() + const bodyId = useId() + const rootRef = useRef(null) + const restartRef = useRef(null) + + // Why: focus Restart only when the user isn't typing elsewhere; unconditional + // autoFocus would steal keys from an active composer or terminal input. + useEffect(() => { + if (!isVisible) { + return + } + const root = rootRef.current + if (!root || isInsideHiddenTree(root)) { + return + } + const paneScope = root.parentElement + if (shouldFocusMobileDriverAction(document.activeElement, document.body, paneScope)) { + restartRef.current?.focus() + } + }, [isVisible, noticeKey]) + + return ( +
+
+
+
+
+
+
+ {translate('auto.components.CodexRestartChip.d3e8a1f4b2', 'Account switched')} +
+
+ {translate( + 'auto.components.CodexRestartChip.a4c8e1b2f7', + 'Codex is still signed in as {{value0}}', + { value0: restartNotice.previousAccountLabel } + )} +
+
+
+
{translate( - 'auto.components.CodexRestartChip.9263e75f49', - 'Codex is using the previous account' + 'auto.components.CodexRestartChip.e9b2c7d1a5', + 'Restart this session to use {{value0}}. Collapse to keep working with the current account.', + { value0: restartNotice.nextAccountLabel } )} - -
- -
+
+ + + +
) } + +function CollapsedRestartChip({ + restartNotice, + onExpand, + onRestart +}: { + restartNotice: RestartNotice + onExpand: () => void + onRestart: () => void +}): React.JSX.Element { + return ( +
+
+ ) +} diff --git a/src/renderer/src/components/Terminal.tsx b/src/renderer/src/components/Terminal.tsx index 905d18c3b..6614c2789 100644 --- a/src/renderer/src/components/Terminal.tsx +++ b/src/renderer/src/components/Terminal.tsx @@ -1866,7 +1866,7 @@ function Terminal(): React.JSX.Element | null { } aria-hidden={!isVisible} > - + {(tabsByWorktree[workspace.id] ?? []).map((tab) => { const activityTerminalPortal = findActivityTerminalPortal( activityTerminalPortals, @@ -2111,7 +2111,7 @@ const WorktreeSplitSurface = React.memo(function WorktreeSplitSurface({ inert={!isVisible} aria-hidden={!isVisible} > - + { + it('builds a stable notice key from account labels', () => { + expect( + buildCodexRestartNoticeKey({ + previousAccountLabel: 'Account A', + nextAccountLabel: 'Account B' + }) + ).toBe('Account A\u0000Account B') + }) + + it('reopens loud mode when the notice key changes', () => { + const collapsed = { + noticeKey: buildCodexRestartNoticeKey({ + previousAccountLabel: 'Account A', + nextAccountLabel: 'Account B' + }), + collapsed: true + } + + expect( + getCodexRestartOverlayCollapseState( + collapsed, + buildCodexRestartNoticeKey({ + previousAccountLabel: 'Account A', + nextAccountLabel: 'Account C' + }) + ) + ).toEqual({ + noticeKey: 'Account A\u0000Account C', + collapsed: false + }) + }) + + it('preserves collapse state for the same notice key', () => { + const noticeKey = buildCodexRestartNoticeKey({ + previousAccountLabel: 'Account A', + nextAccountLabel: 'Account B' + }) + const collapsed = { noticeKey, collapsed: true } + + expect(getCodexRestartOverlayCollapseState(collapsed, noticeKey)).toBe(collapsed) + }) + + it('creates loud mode for a new notice key', () => { + const noticeKey = buildCodexRestartNoticeKey({ + previousAccountLabel: 'Account A', + nextAccountLabel: 'Account B' + }) + + expect(createCodexRestartOverlayCollapseState(noticeKey)).toEqual({ + noticeKey, + collapsed: false + }) + }) +}) diff --git a/src/renderer/src/components/codex-restart-overlay-collapse.ts b/src/renderer/src/components/codex-restart-overlay-collapse.ts new file mode 100644 index 000000000..edc4e786e --- /dev/null +++ b/src/renderer/src/components/codex-restart-overlay-collapse.ts @@ -0,0 +1,32 @@ +export type CodexRestartOverlayCollapseState = { + noticeKey: string | null + collapsed: boolean +} + +export function buildCodexRestartNoticeKey(args: { + previousAccountLabel: string + nextAccountLabel: string +}): string { + return `${args.previousAccountLabel}\u0000${args.nextAccountLabel}` +} + +export function getCodexRestartOverlayCollapseState( + state: CodexRestartOverlayCollapseState, + noticeKey: string | null +): CodexRestartOverlayCollapseState { + return state.noticeKey === noticeKey + ? state + : { + noticeKey, + collapsed: false + } +} + +export function createCodexRestartOverlayCollapseState( + noticeKey: string | null +): CodexRestartOverlayCollapseState { + return { + noticeKey, + collapsed: false + } +} diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 819a67ac5..7b64de846 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -624,8 +624,12 @@ "components": { "CodexRestartChip": { "9132779820": "Dismiss", + "a4c8e1b2f7": "Codex is still signed in as {{value0}}", "c72a5fb234": "Restart", - "9263e75f49": "Codex is using the previous account" + "9263e75f49": "Codex is using the previous account", + "d3e8a1f4b2": "Account switched", + "e9b2c7d1a5": "Restart this session to use {{value0}}. Collapse to keep working with the current account.", + "f1a6c8e3b4": "Collapse" }, "FirstLaunchBanner": { "b9e1b966c7": "Dismiss notice", diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index 02cb085c3..1e4269e3d 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -625,7 +625,11 @@ "CodexRestartChip": { "9132779820": "Despedir", "c72a5fb234": "Reanudar", - "9263e75f49": "Codex está usando la cuenta anterior." + "9263e75f49": "Codex está usando la cuenta anterior.", + "a4c8e1b2f7": "Codex is still signed in as {{value0}}", + "d3e8a1f4b2": "Account switched", + "e9b2c7d1a5": "Restart this session to use {{value0}}. Collapse to keep working with the current account.", + "f1a6c8e3b4": "Collapse" }, "FirstLaunchBanner": { "b9e1b966c7": "Descartar aviso", diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 4fa27852a..2d7ba3f7c 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -625,7 +625,11 @@ "CodexRestartChip": { "9132779820": "閉じる", "c72a5fb234": "再起動", - "9263e75f49": "Codex は前のアカウントを使用しています" + "9263e75f49": "Codex は前のアカウントを使用しています", + "a4c8e1b2f7": "Codex is still signed in as {{value0}}", + "d3e8a1f4b2": "Account switched", + "e9b2c7d1a5": "Restart this session to use {{value0}}. Collapse to keep working with the current account.", + "f1a6c8e3b4": "Collapse" }, "FirstLaunchBanner": { "b9e1b966c7": "通知を閉じる", diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index 6e00dafd6..39c2753f8 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -625,7 +625,11 @@ "CodexRestartChip": { "9132779820": "닫기", "c72a5fb234": "다시 시작", - "9263e75f49": "Codex는 이전 계정을 사용하고 있습니다" + "9263e75f49": "Codex는 이전 계정을 사용하고 있습니다", + "a4c8e1b2f7": "Codex is still signed in as {{value0}}", + "d3e8a1f4b2": "Account switched", + "e9b2c7d1a5": "Restart this session to use {{value0}}. Collapse to keep working with the current account.", + "f1a6c8e3b4": "Collapse" }, "FirstLaunchBanner": { "b9e1b966c7": "알림 닫기", diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index a9e9101b4..2a98060f3 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -625,7 +625,11 @@ "CodexRestartChip": { "9132779820": "关闭", "c72a5fb234": "重新启动", - "9263e75f49": "Codex 正在使用以前的账户" + "9263e75f49": "Codex 正在使用以前的账户", + "a4c8e1b2f7": "Codex is still signed in as {{value0}}", + "d3e8a1f4b2": "Account switched", + "e9b2c7d1a5": "Restart this session to use {{value0}}. Collapse to keep working with the current account.", + "f1a6c8e3b4": "Collapse" }, "FirstLaunchBanner": { "b9e1b966c7": "关闭通知",