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

_chat.tsx

119 lines3.5 KB

1import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
2import { useEffect } from "react";
3​
4import { useCommandPaletteStore } from "../commandPaletteStore";
5import { useHandleNewThread } from "../hooks/useHandleNewThread";
6import {
7 startNewLocalThreadFromContext,
8 startNewThreadFromContext,
9} from "../lib/chatThreadActions";
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 const command = resolveShortcutCommand(event, keybindings, {
35 context: {
36 terminalFocus: isTerminalFocused(),
37 terminalOpen,
38 },
39 });
40​
41 if (useCommandPaletteStore.getState().open) {
42 return;
43 }
44​
45 if (event.key === "Escape" && selectedThreadKeysSize > 0) {
46 event.preventDefault();
47 clearSelection();
48 return;
49 }
50​
51 if (command === "chat.newLocal") {
52 event.preventDefault();
53 event.stopPropagation();
54 void startNewLocalThreadFromContext({
55 activeDraftThread,
56 activeThread,
57 defaultProjectRef,
58 defaultThreadEnvMode: resolveSidebarNewThreadEnvMode({
59 defaultEnvMode: appSettings.defaultThreadEnvMode,
60 }),
61 handleNewThread,
62 });
63 return;
64 }
65​
66 if (command === "chat.new") {
67 event.preventDefault();
68 event.stopPropagation();
69 void startNewThreadFromContext({
70 activeDraftThread,
71 activeThread,
72 defaultProjectRef,
73 defaultThreadEnvMode: resolveSidebarNewThreadEnvMode({
74 defaultEnvMode: appSettings.defaultThreadEnvMode,
75 }),
76 handleNewThread,
77 });
78 }
79 };
80​
81 window.addEventListener("keydown", onWindowKeyDown);
82 return () => {
83 window.removeEventListener("keydown", onWindowKeyDown);
84 };
85 }, [
86 activeDraftThread,
87 activeThread,
88 clearSelection,
89 handleNewThread,
90 keybindings,
91 defaultProjectRef,
92 selectedThreadKeysSize,
93 terminalOpen,
94 appSettings.defaultThreadEnvMode,
95 ]);
96​
97 return null;
99​
100function ChatRouteLayout() {
101 return (
102 <>
103 <ChatRouteGlobalShortcuts />
104 <Outlet />
105 </>
106 );
108​
109export const Route = createFileRoute("/_chat")({
110 beforeLoad: async ({ context }) => {
111 if (
112 context.authGateState.status !== "authenticated" &&
113 context.authGateState.status !== "hosted-static"
114 ) {
115 throw redirect({ to: "/pair", replace: true });
116 }
117 },
118 component: ChatRouteLayout,
119});