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

_chat.tsx

119 lines3.8 KB

1import { scopeProjectRef } from "@t3tools/client-runtime";
2import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
3import { useEffect } from "react";
4​
5import {
6 ensurePrimaryEnvironmentReady,
7 resolveInitialServerAuthGateState,
8} from "../environments/primary";
9import { useHandleNewThread } from "../hooks/useHandleNewThread";
10import { isTerminalFocused } from "../lib/terminalFocus";
11import { resolveShortcutCommand } from "../keybindings";
12import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
13import { useThreadSelectionStore } from "../threadSelectionStore";
14import { resolveSidebarNewThreadEnvMode } from "~/components/Sidebar.logic";
15import { useSettings } from "~/hooks/useSettings";
16import { useServerKeybindings } from "~/rpc/serverState";
17​
18function ChatRouteGlobalShortcuts() {
19 const clearSelection = useThreadSelectionStore((state) => state.clearSelection);
20 const selectedThreadKeysSize = useThreadSelectionStore((state) => state.selectedThreadKeys.size);
21 const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread, routeThreadRef } =
22 useHandleNewThread();
23 const keybindings = useServerKeybindings();
24 const terminalOpen = useTerminalStateStore((state) =>
25 routeThreadRef
26 ? selectThreadTerminalState(state.terminalStateByThreadKey, routeThreadRef).terminalOpen
27 : false,
28 );
29 const appSettings = useSettings();
30​
31 useEffect(() => {
32 const onWindowKeyDown = (event: KeyboardEvent) => {
33 if (event.defaultPrevented) return;
34​
35 if (event.key === "Escape" && selectedThreadKeysSize > 0) {
36 event.preventDefault();
37 clearSelection();
38 return;
39 }
40​
41 const projectRef = activeThread
42 ? scopeProjectRef(activeThread.environmentId, activeThread.projectId)
43 : activeDraftThread && routeThreadRef
44 ? scopeProjectRef(routeThreadRef.environmentId, activeDraftThread.projectId)
45 : defaultProjectRef;
46 if (!projectRef) return;
47​
48 const command = resolveShortcutCommand(event, keybindings, {
49 context: {
50 terminalFocus: isTerminalFocused(),
51 terminalOpen,
52 },
53 });
54​
55 if (command === "chat.newLocal") {
56 event.preventDefault();
57 event.stopPropagation();
58 void handleNewThread(projectRef, {
59 envMode: resolveSidebarNewThreadEnvMode({
60 defaultEnvMode: appSettings.defaultThreadEnvMode,
61 }),
62 });
63 return;
64 }
65​
66 if (command === "chat.new") {
67 event.preventDefault();
68 event.stopPropagation();
69 void handleNewThread(projectRef, {
70 branch: activeThread?.branch ?? activeDraftThread?.branch ?? null,
71 worktreePath: activeThread?.worktreePath ?? activeDraftThread?.worktreePath ?? null,
72 envMode:
73 activeDraftThread?.envMode ?? (activeThread?.worktreePath ? "worktree" : "local"),
74 });
75 return;
76 }
77 };
78​
79 window.addEventListener("keydown", onWindowKeyDown);
80 return () => {
81 window.removeEventListener("keydown", onWindowKeyDown);
82 };
83 }, [
84 activeDraftThread,
85 activeThread,
86 clearSelection,
87 handleNewThread,
88 keybindings,
89 defaultProjectRef,
90 routeThreadRef,
91 selectedThreadKeysSize,
92 terminalOpen,
93 appSettings.defaultThreadEnvMode,
94 ]);
95​
96 return null;
98​
99function ChatRouteLayout() {
100 return (
101 <>
102 <ChatRouteGlobalShortcuts />
103 <Outlet />
104 </>
105 );
107​
108export const Route = createFileRoute("/_chat")({
109 beforeLoad: async () => {
110 const [, authGateState] = await Promise.all([
111 ensurePrimaryEnvironmentReady(),
112 resolveInitialServerAuthGateState(),
113 ]);
114 if (authGateState.status !== "authenticated") {
115 throw redirect({ to: "/pair", replace: true });
116 }
117 },
118 component: ChatRouteLayout,
119});