Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

_chat.tsx

170 lines5.5 KB

1import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
2import { useAtomValue } from "@effect/atom-react";
3import { useEffect } from "react";
4​
5import { isCommandPaletteOpen } from "../commandPaletteContext";
6import { dispatchPreviewAction } from "../components/preview/previewActionBus";
7import { useHandleNewThread } from "../hooks/useHandleNewThread";
8import {
9 startNewLocalThreadFromContext,
10 startNewThreadFromContext,
11} from "../lib/chatThreadActions";
12import { isPreviewFocused } from "../lib/previewFocus";
13import { isTerminalFocused } from "../lib/terminalFocus";
14import { resolveShortcutCommand } from "../keybindings";
15import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
16import { isPreviewSupportedInRuntime } from "../previewStateStore";
17import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
18import { useThreadSelectionStore } from "../threadSelectionStore";
19import { stackedThreadToast, toastManager } from "~/components/ui/toast";
20import { primaryServerKeybindingsAtom } from "~/state/server";
21​
22function ChatRouteGlobalShortcuts() {
23 const clearSelection = useThreadSelectionStore((state) => state.clearSelection);
24 const selectedThreadKeysSize = useThreadSelectionStore((state) => state.selectedThreadKeys.size);
25 const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread, routeThreadRef } =
26 useHandleNewThread();
27 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
28 const terminalOpen = useTerminalUiStateStore((state) =>
29 routeThreadRef
30 ? selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef).terminalOpen
31 : false,
32 );
33 // The `previewOpen` shortcut-context flag here uses the store-only value;
34 // the URL-aware arbitration lives inside ChatView's `onTogglePreview`,
35 // which we invoke via the action bus to avoid duplicating the rule.
36 const previewOpen = useRightPanelStore((state) =>
37 routeThreadRef
38 ? selectActiveRightPanel(state.byThreadKey, routeThreadRef) === "preview"
39 : false,
40 );
41 useEffect(() => {
42 const onWindowKeyDown = (event: KeyboardEvent) => {
43 if (event.defaultPrevented) return;
44 const command = resolveShortcutCommand(event, keybindings, {
45 context: {
46 terminalFocus: isTerminalFocused(),
47 terminalOpen,
48 previewFocus: isPreviewFocused(),
49 previewOpen,
50 },
51 });
52​
53 if (isCommandPaletteOpen()) {
54 return;
55 }
56​
57 if (event.key === "Escape" && selectedThreadKeysSize > 0) {
58 event.preventDefault();
59 clearSelection();
60 return;
61 }
62​
63 if (command === "chat.newLocal") {
64 event.preventDefault();
65 event.stopPropagation();
66 void startNewLocalThreadFromContext({
67 activeDraftThread,
68 activeThread: activeThread ?? undefined,
69 defaultProjectRef,
70 handleNewThread,
71 });
72 return;
73 }
74​
75 if (command === "chat.new") {
76 event.preventDefault();
77 event.stopPropagation();
78 void startNewThreadFromContext({
79 activeDraftThread,
80 activeThread: activeThread ?? undefined,
81 defaultProjectRef,
82 handleNewThread,
83 });
84 return;
85 }
86​
87 if (command === "preview.toggle") {
88 event.preventDefault();
89 event.stopPropagation();
90 if (!routeThreadRef) return;
91 if (!isPreviewSupportedInRuntime()) {
92 toastManager.add(
93 stackedThreadToast({
94 type: "info",
95 title: "Preview is desktop-only",
96 description: "Open T3 Code in the desktop app to use the in-app preview.",
97 }),
98 );
99 return;
100 }
101 dispatchPreviewAction("toggle-panel");
102 return;
103 }
104​
105 // The remaining preview commands only fire when the panel is the
106 // currently-focused tenant. The `when: previewFocus` rule already
107 // gates this, but defend against the keybinding being misconfigured.
108 if (
109 command === "preview.refresh" ||
110 command === "preview.focusUrl" ||
111 command === "preview.zoomIn" ||
112 command === "preview.zoomOut" ||
113 command === "preview.resetZoom"
114 ) {
115 event.preventDefault();
116 event.stopPropagation();
117 const action =
118 command === "preview.refresh"
119 ? "refresh"
120 : command === "preview.focusUrl"
121 ? "focus-url"
122 : command === "preview.zoomIn"
123 ? "zoom-in"
124 : command === "preview.zoomOut"
125 ? "zoom-out"
126 : "reset-zoom";
127 dispatchPreviewAction(action);
128 }
129 };
130​
131 window.addEventListener("keydown", onWindowKeyDown);
132 return () => {
133 window.removeEventListener("keydown", onWindowKeyDown);
134 };
135 }, [
136 activeDraftThread,
137 activeThread,
138 clearSelection,
139 handleNewThread,
140 keybindings,
141 defaultProjectRef,
142 previewOpen,
143 routeThreadRef,
144 selectedThreadKeysSize,
145 terminalOpen,
146 ]);
147​
148 return null;
150​
151function ChatRouteLayout() {
152 return (
153 <>
154 <ChatRouteGlobalShortcuts />
155 <Outlet />
156 </>
157 );
159​
160export const Route = createFileRoute("/_chat")({
161 beforeLoad: async ({ context }) => {
162 if (
163 context.authGateState.status !== "authenticated" &&
164 context.authGateState.status !== "hosted-static"
165 ) {
166 throw redirect({ to: "/pair", replace: true });
167 }
168 },
169 component: ChatRouteLayout,
170});