The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { AuthPreviewOperateScope, type EnvironmentId } from "@t3tools/contracts"; |
| 2 | import type { AnyRouter } from "@tanstack/react-router"; |
| 3 | import { msg } from "gt-react"; |
| 4 | |
| 5 | import { stackedThreadToast, toastManager } from "~/components/ui/toast"; |
| 6 | import { m } from "~/i18n/messages"; |
| 7 | import { isPreviewAvailableFor } from "../browser/previewRuntime"; |
| 8 | import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus"; |
| 9 | import { useComposerDraftStore } from "../composerDraftStore"; |
| 10 | import { dispatchPreviewAction, type PreviewAction } from "../components/preview/previewActionBus"; |
| 11 | import { isAppModalPage } from "../components/sidebar/mainAppLocation"; |
| 12 | import { isBrowserSurfaceFocused } from "../lib/previewFocus"; |
| 13 | import { shouldCreateNewThreadInCurrentProject } from "../components/Sidebar.logic"; |
| 14 | import { |
| 15 | createNewThreadHandler, |
| 16 | readLastUsedProjectKey, |
| 17 | resolveDefaultProjectRef, |
| 18 | } from "../hooks/useHandleNewThread"; |
| 19 | import { createScratchProjectActions } from "../hooks/useScratchProject"; |
| 20 | import { getClientSettings } from "../hooks/useSettings"; |
| 21 | import { startNewThreadFromContext } from "../lib/chatThreadActions"; |
| 22 | import { selectProjectGroupingSettings } from "@t3tools/client-runtime/state/project-grouping"; |
| 23 | import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; |
| 24 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 25 | import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping"; |
| 26 | import { readProjects, readThreadShell } from "../state/entities"; |
| 27 | import { defaultProjectFallbackReadyAtom } from "../state/projects"; |
| 28 | import { environmentPresentations } from "../state/presentation"; |
| 29 | import { primaryEnvironmentIdAtom } from "../state/primaryEnvironment"; |
| 30 | import { readEnvironmentScope } from "../state/session"; |
| 31 | import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; |
| 32 | import { useThreadSelectionStore } from "../threadSelectionStore"; |
| 33 | import { resolveThreadRouteTarget } from "../threadRoutes"; |
| 34 | import { useUiStateStore } from "../uiStateStore"; |
| 35 | import { type CommandHandler, registerCommands } from "./dispatcher"; |
| 36 | |
| 37 | const CHAT_ROUTE_ID = "/_chat"; |
| 38 | |
| 39 | /** |
| 40 | * New-thread and preview commands, live while a chat route is showing and no |
| 41 | * modal page (Settings, Usage) covers it. Everything is read at event time, so |
| 42 | * the scope is registered once instead of re-registering on every store change. |
| 43 | */ |
| 44 | export function installChatRouteCommands(router: AnyRouter): () => void { |
| 45 | const handleNewThread = createNewThreadHandler(router); |
| 46 | const scratch = createScratchProjectActions({ |
| 47 | environments: () => |
| 48 | [...appAtomRegistry.get(environmentPresentations.presentationsAtom)].map( |
| 49 | ([environmentId, presentation]) => ({ ...presentation, environmentId }), |
| 50 | ), |
| 51 | primaryEnvironmentId: () => appAtomRegistry.get(primaryEnvironmentIdAtom), |
| 52 | registry: appAtomRegistry, |
| 53 | handleNewThread, |
| 54 | }); |
| 55 | |
| 56 | const readRouteTarget = () => resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {}); |
| 57 | const readRouteThreadRef = () => { |
| 58 | const target = readRouteTarget(); |
| 59 | return target?.kind === "server" ? target.threadRef : null; |
| 60 | }; |
| 61 | const readRouteEnvironmentId = () => { |
| 62 | const target = readRouteTarget(); |
| 63 | if (!target) return null; |
| 64 | return target.kind === "server" |
| 65 | ? target.threadRef.environmentId |
| 66 | : (useComposerDraftStore.getState().getDraftSession(target.draftId)?.environmentId ?? null); |
| 67 | }; |
| 68 | const readPreviewOpen = () => { |
| 69 | const threadRef = readRouteThreadRef(); |
| 70 | // Store-only on purpose: the URL-aware arbitration lives in ChatView's |
| 71 | // preview toggle, which the action bus reaches. |
| 72 | return threadRef |
| 73 | ? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, threadRef) === "preview" |
| 74 | : false; |
| 75 | }; |
| 76 | const readThreadContext = () => { |
| 77 | const lastUsedProjectKey = readLastUsedProjectKey(); |
| 78 | const target = readRouteTarget(); |
| 79 | const drafts = useComposerDraftStore.getState(); |
| 80 | const activeThread = target?.kind === "server" ? readThreadShell(target.threadRef) : null; |
| 81 | const activeDraftThread = target |
| 82 | ? target.kind === "server" |
| 83 | ? drafts.getDraftThread(target.threadRef) |
| 84 | : drafts.getDraftSession(target.draftId) |
| 85 | : null; |
| 86 | return { |
| 87 | activeDraftThread, |
| 88 | activeThread: activeThread ?? undefined, |
| 89 | defaultProjectRef: resolveDefaultProjectRef( |
| 90 | readProjects(), |
| 91 | useUiStateStore.getState().projectOrder, |
| 92 | lastUsedProjectKey, |
| 93 | appAtomRegistry.get(defaultProjectFallbackReadyAtom(lastUsedProjectKey)), |
| 94 | ), |
| 95 | handleNewThread, |
| 96 | }; |
| 97 | }; |
| 98 | // Grouped like the sidebar, so the shortcut and the New thread button agree. |
| 99 | const readProjectGroups = () => |
| 100 | buildSidebarProjectSnapshots({ |
| 101 | projects: readProjects(), |
| 102 | groupScratchProjects: true, |
| 103 | settings: selectProjectGroupingSettings(getClientSettings()), |
| 104 | primaryEnvironmentId: appAtomRegistry.get(primaryEnvironmentIdAtom), |
| 105 | resolveEnvironmentLabel: () => null, |
| 106 | }); |
| 107 | const canOperatePreview = (environmentId: EnvironmentId) => |
| 108 | readEnvironmentScope(environmentId, AuthPreviewOperateScope); |
| 109 | |
| 110 | const claim = (event: KeyboardEvent) => { |
| 111 | event.preventDefault(); |
| 112 | event.stopPropagation(); |
| 113 | }; |
| 114 | |
| 115 | const startNewLocal: CommandHandler = (event) => { |
| 116 | claim(event); |
| 117 | // With no current or open project, such as when every project is archived, pick one. |
| 118 | void startNewThreadFromContext(readThreadContext()).then((started) => { |
| 119 | if (!started) openCommandPalette({ open: "new-thread-in" }); |
| 120 | }); |
| 121 | }; |
| 122 | |
| 123 | const previewCommand = |
| 124 | (action: PreviewAction): CommandHandler => |
| 125 | (event) => { |
| 126 | claim(event); |
| 127 | const environmentId = readRouteEnvironmentId(); |
| 128 | if (environmentId && canOperatePreview(environmentId)) dispatchPreviewAction(action); |
| 129 | }; |
| 130 | // Unclaimed, zoom keys reach the desktop menu accelerators or the browser's own zoom. |
| 131 | const zoomCommand = |
| 132 | (action: PreviewAction): CommandHandler => |
| 133 | (event) => { |
| 134 | if (!isBrowserSurfaceFocused()) return false; |
| 135 | previewCommand(action)(event); |
| 136 | }; |
| 137 | |
| 138 | return registerCommands( |
| 139 | { |
| 140 | "chat.newLocal": startNewLocal, |
| 141 | "chat.newWithoutProject": (event) => { |
| 142 | const { activeThread, activeDraftThread } = readThreadContext(); |
| 143 | const environmentId = scratch.scratchEnvironmentId( |
| 144 | activeThread?.environmentId ?? activeDraftThread?.environmentId ?? null, |
| 145 | ); |
| 146 | if (environmentId === null) return; |
| 147 | claim(event); |
| 148 | void scratch.startScratchThread(environmentId); |
| 149 | }, |
| 150 | "chat.new": (event) => { |
| 151 | const groups = readProjectGroups(); |
| 152 | if ( |
| 153 | !shouldCreateNewThreadInCurrentProject( |
| 154 | false, |
| 155 | groups.filter((group) => !group.archived).length, |
| 156 | groups.length, |
| 157 | ) |
| 158 | ) { |
| 159 | claim(event); |
| 160 | openCommandPalette({ open: "new-thread-in" }); |
| 161 | return; |
| 162 | } |
| 163 | startNewLocal(event); |
| 164 | }, |
| 165 | "preview.toggle": (event) => { |
| 166 | claim(event); |
| 167 | const threadRef = readRouteThreadRef(); |
| 168 | if (!threadRef || (!canOperatePreview(threadRef.environmentId) && !readPreviewOpen())) { |
| 169 | return; |
| 170 | } |
| 171 | if (!isPreviewAvailableFor(threadRef.environmentId)) { |
| 172 | toastManager.add( |
| 173 | stackedThreadToast({ |
| 174 | type: "info", |
| 175 | title: m(msg("Preview is desktop-only")), |
| 176 | description: m(msg("Open T3 Code in the desktop app to use the in-app preview.")), |
| 177 | }), |
| 178 | ); |
| 179 | return; |
| 180 | } |
| 181 | dispatchPreviewAction("toggle-panel"); |
| 182 | }, |
| 183 | "preview.refresh": previewCommand("refresh"), |
| 184 | "preview.focusUrl": previewCommand("focus-url"), |
| 185 | "preview.zoomIn": zoomCommand("zoom-in"), |
| 186 | "preview.zoomOut": zoomCommand("zoom-out"), |
| 187 | "preview.resetZoom": zoomCommand("reset-zoom"), |
| 188 | "preview.find": previewCommand("find"), |
| 189 | }, |
| 190 | { |
| 191 | when: () => |
| 192 | router.state.matches.some((match) => match.routeId === CHAT_ROUTE_ID) && |
| 193 | !router.state.matches.some((match) => isAppModalPage(match.pathname)) && |
| 194 | !isCommandPaletteOpen(), |
| 195 | context: () => { |
| 196 | const threadRef = readRouteThreadRef(); |
| 197 | return { |
| 198 | terminalOpen: threadRef |
| 199 | ? selectThreadTerminalUiState( |
| 200 | useTerminalUiStateStore.getState().terminalUiStateByThreadKey, |
| 201 | threadRef, |
| 202 | ).terminalOpen |
| 203 | : false, |
| 204 | previewOpen: readPreviewOpen(), |
| 205 | }; |
| 206 | }, |
| 207 | onKeyDown: (event) => { |
| 208 | if (event.key !== "Escape") return false; |
| 209 | const selection = useThreadSelectionStore.getState(); |
| 210 | if (selection.selectedThreadKeys.size === 0) return false; |
| 211 | event.preventDefault(); |
| 212 | selection.clearSelection(); |
| 213 | }, |
| 214 | }, |
| 215 | ); |
| 216 | } |