The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { m } from "../i18n/messages"; |
| 2 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import { |
| 5 | scopeProjectRef, |
| 6 | scopedThreadKey, |
| 7 | scopeThreadRef, |
| 8 | } from "@t3tools/client-runtime/environment"; |
| 9 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 10 | import { useNavigate, useParams } from "@tanstack/react-router"; |
| 11 | import { useGT } from "gt-react"; |
| 12 | import { useEffect, useEffectEvent, useRef } from "react"; |
| 13 | |
| 14 | import { isCommandPaletteOpen } from "../commandPaletteBus"; |
| 15 | import { useClosedViewStore } from "../closedViewStore"; |
| 16 | import { useComposerDraftStore } from "../composerDraftStore"; |
| 17 | import { environmentCatalog } from "../connection/catalog"; |
| 18 | import { effectiveShortcutsForCommand, resolveShortcutCommand } from "../keybindings"; |
| 19 | import { isEditableFocused } from "../lib/editableFocus"; |
| 20 | import { isPreviewFocused } from "../lib/previewFocus"; |
| 21 | import { isTerminalFocused } from "../lib/terminalFocus"; |
| 22 | import { isModelPickerOpen } from "../modelPickerVisibility"; |
| 23 | import { |
| 24 | planNextReopen, |
| 25 | pullRequestsSearchForRestore, |
| 26 | reopenClosedView, |
| 27 | } from "../reopenClosedView"; |
| 28 | import { |
| 29 | ISSUES_PANEL_REF, |
| 30 | PULL_REQUESTS_PANEL_REF, |
| 31 | selectActiveRightPanel, |
| 32 | selectSelectedRightPanelSurface, |
| 33 | selectThreadRightPanelState, |
| 34 | useRightPanelStore, |
| 35 | } from "../rightPanelStore"; |
| 36 | import { serverEnvironment } from "../state/server"; |
| 37 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 38 | import { readProject, readThreadShell } from "../state/entities"; |
| 39 | import { previewEnvironment } from "../state/preview"; |
| 40 | import { primaryServerKeybindingsAtom } from "../state/server"; |
| 41 | import { environmentShell } from "../state/shell"; |
| 42 | import { useAtomCommand } from "../state/use-atom-command"; |
| 43 | import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; |
| 44 | import { |
| 45 | buildDraftThreadRouteParams, |
| 46 | buildThreadRouteParams, |
| 47 | resolveThreadRouteTarget, |
| 48 | } from "../threadRoutes"; |
| 49 | import { toastManager } from "./ui/toast"; |
| 50 | import { readIssueListPreferences } from "./issue/issueListPreferences"; |
| 51 | |
| 52 | const PREVIEW_FORWARDED_COMMANDS = [ |
| 53 | "workspace.focusNext", |
| 54 | "workspace.focusPrevious", |
| 55 | "sidebar.toggle", |
| 56 | "rightPanel.toggle", |
| 57 | "rightPanel.toggleMaximized", |
| 58 | "rightPanel.new", |
| 59 | "rightPanel.close", |
| 60 | "preview.focusUrl", |
| 61 | "preview.zoomIn", |
| 62 | "preview.zoomOut", |
| 63 | "preview.resetZoom", |
| 64 | "preview.find", |
| 65 | ] as const; |
| 66 | |
| 67 | const isGlobalPullRequests = (ref: ScopedThreadRef) => |
| 68 | scopedThreadKey(ref) === scopedThreadKey(PULL_REQUESTS_PANEL_REF); |
| 69 | const isGlobalIssues = (ref: ScopedThreadRef) => |
| 70 | scopedThreadKey(ref) === scopedThreadKey(ISSUES_PANEL_REF); |
| 71 | |
| 72 | export function ReopenClosedViewShortcut() { |
| 73 | const gt = useGT(); |
| 74 | const navigate = useNavigate(); |
| 75 | const params = useParams({ strict: false }); |
| 76 | const target = resolveThreadRouteTarget(params); |
| 77 | const draft = useComposerDraftStore((state) => |
| 78 | target?.kind === "draft" ? state.getDraftSession(target.draftId) : null, |
| 79 | ); |
| 80 | const threadRef = |
| 81 | target?.kind === "server" |
| 82 | ? target.threadRef |
| 83 | : draft |
| 84 | ? (draft.promotedTo ?? scopeThreadRef(draft.environmentId, draft.threadId)) |
| 85 | : null; |
| 86 | const terminalOpen = useTerminalUiStateStore( |
| 87 | (state) => |
| 88 | selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef).terminalOpen, |
| 89 | ); |
| 90 | const previewOpen = useRightPanelStore( |
| 91 | (state) => selectActiveRightPanel(state.byThreadKey, threadRef) === "preview", |
| 92 | ); |
| 93 | const keybindings = useAtomValue(primaryServerKeybindingsAtom); |
| 94 | const hasHistory = useClosedViewStore((state) => state.entries.length > 0); |
| 95 | const openPreview = useAtomCommand(previewEnvironment.open); |
| 96 | const pending = useRef(Promise.resolve()); |
| 97 | |
| 98 | const reopenNext = useEffectEvent(async () => { |
| 99 | const catalog = appAtomRegistry.get(environmentCatalog.catalogValueAtom); |
| 100 | const drafts = useComposerDraftStore.getState(); |
| 101 | const panelsByThread = useRightPanelStore.getState().byThreadKey; |
| 102 | const { drop, restore } = planNextReopen(useClosedViewStore.getState().entries, (entry) => { |
| 103 | const ref = entry.threadRef; |
| 104 | const panel = selectThreadRightPanelState(panelsByThread, ref); |
| 105 | if (isGlobalPullRequests(ref) || isGlobalIssues(ref)) { |
| 106 | return { |
| 107 | environmentKnown: true, |
| 108 | catalogReady: true, |
| 109 | ownerExists: true, |
| 110 | sideChatExists: true, |
| 111 | shellLive: true, |
| 112 | panel, |
| 113 | }; |
| 114 | } |
| 115 | return { |
| 116 | environmentKnown: catalog.entries.has(ref.environmentId), |
| 117 | catalogReady: catalog.isReady, |
| 118 | ownerExists: readThreadShell(ref) !== null || drafts.getDraftThreadByRef(ref) !== null, |
| 119 | sideChatExists: |
| 120 | entry.kind !== "panel-tab" || |
| 121 | entry.surface.kind !== "thread" || |
| 122 | readThreadShell(scopeThreadRef(ref.environmentId, entry.surface.threadId)) !== null || |
| 123 | // The shell leaves idle subagents out, so one it does not list may still exist. |
| 124 | appAtomRegistry.get(serverEnvironment.configValueAtom(ref.environmentId)) |
| 125 | ?.shellThreadFamilies === true, |
| 126 | shellLive: |
| 127 | appAtomRegistry.get(environmentShell.stateValueAtom(ref.environmentId)).status === "live", |
| 128 | panel, |
| 129 | }; |
| 130 | }); |
| 131 | for (const entry of drop) useClosedViewStore.getState().remove(entry.id); |
| 132 | if (!restore) return; |
| 133 | |
| 134 | const ref = restore.threadRef; |
| 135 | const globalPullRequests = isGlobalPullRequests(ref); |
| 136 | const globalIssues = isGlobalIssues(ref); |
| 137 | const thread = globalPullRequests || globalIssues ? null : readThreadShell(ref); |
| 138 | const owner = thread ?? drafts.getDraftThreadByRef(ref); |
| 139 | const project = owner ? readProject(scopeProjectRef(ref.environmentId, owner.projectId)) : null; |
| 140 | if (!(await reopenClosedView(restore, { openPreview, workspaceAvailable: project !== null }))) { |
| 141 | useClosedViewStore.getState().defer(restore.id); |
| 142 | return; |
| 143 | } |
| 144 | useClosedViewStore.getState().remove(restore.id); |
| 145 | if (globalPullRequests) { |
| 146 | const selected = selectSelectedRightPanelSurface( |
| 147 | useRightPanelStore.getState().byThreadKey, |
| 148 | ref, |
| 149 | ); |
| 150 | await navigate({ |
| 151 | to: "/pull-requests", |
| 152 | search: (previous) => pullRequestsSearchForRestore(previous, selected), |
| 153 | }); |
| 154 | } else if (globalIssues) { |
| 155 | await navigate({ to: "/issues", search: readIssueListPreferences() }); |
| 156 | } else { |
| 157 | const draftId = thread === null ? drafts.getDraftIdByRef(ref) : null; |
| 158 | if (draftId !== null) |
| 159 | await navigate({ to: "/draft/$draftId", params: buildDraftThreadRouteParams(draftId) }); |
| 160 | else await navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams(ref) }); |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | const enqueueReopen = useEffectEvent(() => { |
| 165 | pending.current = pending.current |
| 166 | .then(() => reopenNext()) |
| 167 | .catch((error: unknown) => { |
| 168 | toastManager.add({ |
| 169 | type: "error", |
| 170 | title: gt("Could not reopen view"), |
| 171 | description: thrownErrorMessage(error, m) ?? String(error), |
| 172 | }); |
| 173 | }); |
| 174 | }); |
| 175 | |
| 176 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to window keydown and the desktop menu action for the reopen closed view shortcut |
| 177 | useEffect(() => { |
| 178 | const onKeyDown = (event: KeyboardEvent) => { |
| 179 | if ( |
| 180 | event.defaultPrevented || |
| 181 | isCommandPaletteOpen() || |
| 182 | useClosedViewStore.getState().entries.length === 0 || |
| 183 | (event.target instanceof HTMLElement && event.target.closest("[data-keybinding-capture]")) |
| 184 | ) |
| 185 | return; |
| 186 | const command = resolveShortcutCommand(event, keybindings, { |
| 187 | context: { |
| 188 | terminalFocus: isTerminalFocused(), |
| 189 | terminalOpen, |
| 190 | previewFocus: isPreviewFocused(), |
| 191 | previewOpen, |
| 192 | editableFocus: isEditableFocused(event.target), |
| 193 | modelPickerOpen: isModelPickerOpen(), |
| 194 | }, |
| 195 | }); |
| 196 | if (command !== "view.reopenClosed") return; |
| 197 | event.preventDefault(); |
| 198 | event.stopPropagation(); |
| 199 | if (!event.repeat) enqueueReopen(); |
| 200 | }; |
| 201 | window.addEventListener("keydown", onKeyDown, true); |
| 202 | const unsubscribe = window.desktopBridge?.onMenuAction((action) => { |
| 203 | if (action === "view.reopenClosed" && !isCommandPaletteOpen()) enqueueReopen(); |
| 204 | }); |
| 205 | return () => { |
| 206 | window.removeEventListener("keydown", onKeyDown, true); |
| 207 | unsubscribe?.(); |
| 208 | }; |
| 209 | }, [keybindings, previewOpen, terminalOpen]); |
| 210 | |
| 211 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- registers forwarded shortcut chords with the desktop preview bridge and clears them on cleanup |
| 212 | useEffect(() => { |
| 213 | const preview = window.desktopBridge?.preview; |
| 214 | if (!preview) return; |
| 215 | const options = { |
| 216 | context: { |
| 217 | previewFocus: true, |
| 218 | previewOpen: true, |
| 219 | rightPanelOpen: true, |
| 220 | terminalFocus: false, |
| 221 | terminalOpen, |
| 222 | editableFocus: false, |
| 223 | modelPickerOpen: false, |
| 224 | isDesktop: true, |
| 225 | isWeb: false, |
| 226 | }, |
| 227 | }; |
| 228 | // Guest browser processes miss app keydown listeners; desktop forwards the chords as menu actions. |
| 229 | const commands = [ |
| 230 | ...(hasHistory ? (["view.reopenClosed"] as const) : []), |
| 231 | ...PREVIEW_FORWARDED_COMMANDS, |
| 232 | ]; |
| 233 | void preview |
| 234 | .setForwardedShortcuts( |
| 235 | commands.flatMap((command) => |
| 236 | effectiveShortcutsForCommand(keybindings, command, options).map((shortcut) => ({ |
| 237 | command, |
| 238 | shortcut, |
| 239 | })), |
| 240 | ), |
| 241 | ) |
| 242 | .catch(() => undefined); |
| 243 | return () => { |
| 244 | void preview.setForwardedShortcuts([]).catch(() => undefined); |
| 245 | }; |
| 246 | }, [hasHistory, keybindings, terminalOpen]); |
| 247 | |
| 248 | return null; |
| 249 | } |