The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 138 lines 4.78 KB
Raw
1 import {
2 DESKTOP_APP_ACTIVATION_PROTOCOL_VERSION,
3 DesktopAppActivationPlatform,
4 } from "@t3tools/contracts";
5 import * as Schema from "effect/Schema";
6 import { FolderPlusIcon } from "lucide-react";
7 import { useGT } from "gt-react";
8 import { useState, type DragEvent, type ReactNode } from "react";
9
10 import { useOpenLocalWorkspace } from "../../hooks/useOpenLocalWorkspace";
11 import { randomUUID } from "../../lib/utils";
12 import { resolveDroppedFolderPath } from "../chat/folderDrop";
13 import { splitTransferredFiles } from "../chat/workspaceFileDrop";
14 import { setFileDragActive } from "../Sidebar.logic";
15 import { toastManager } from "../ui/toast";
16 import { stackedThreadToast } from "../ui/toastHelpers";
17
18 const isActivationPlatform = Schema.is(DesktopAppActivationPlatform);
19
20 function isFileDrag(event: DragEvent) {
21 return event.dataTransfer.types.includes("Files");
22 }
23
24 /**
25 * Folders dropped from the OS file manager open as projects, like `t3 <dir>`.
26 * Thread rows take file drops for themselves by calling preventDefault first;
27 * this zone then stays hidden and only picks up folders from the drop.
28 * Browsers expose no absolute paths, so only the desktop app can add them.
29 */
30 export function SidebarFolderDrop(props: { children: ReactNode }) {
31 const gt = useGT();
32 const [isActive, setIsActive] = useState(false);
33 const openLocalWorkspace = useOpenLocalWorkspace();
34 const bridge = window.desktopBridge;
35 const setActive = (active: boolean) => setFileDragActive(setIsActive, active);
36
37 const addFolders = async (folders: File[]) => {
38 if (bridge === undefined) {
39 toastManager.add(
40 stackedThreadToast({
41 type: "info",
42 title: gt("Dropping folders needs the desktop app"),
43 description: gt(
44 "Browsers do not share folder paths. Use Add project to choose a folder.",
45 ),
46 }),
47 );
48 return;
49 }
50 const platform = bridge.getClientPlatform();
51 if (!openLocalWorkspace.ready || !isActivationPlatform(platform)) {
52 toastManager.add(
53 stackedThreadToast({
54 type: "error",
55 title: gt("This computer's server is not connected yet"),
56 description: gt("Try again when it connects."),
57 }),
58 );
59 return;
60 }
61 // One at a time, so the last folder is the one left open.
62 for (const folder of folders) {
63 const workspaceRoot = resolveDroppedFolderPath(folder, bridge.getPathForFile);
64 const response =
65 workspaceRoot === null
66 ? null
67 : await openLocalWorkspace.open({
68 version: DESKTOP_APP_ACTIVATION_PROTOCOL_VERSION,
69 requestId: randomUUID(),
70 type: "open-workspace",
71 workspaceRoot,
72 platform,
73 });
74 if (response?.ok) continue;
75 toastManager.add(
76 stackedThreadToast({
77 type: "error",
78 title: gt('Could not add "{name}"', { name: folder.name }),
79 description: response?.message ?? gt("T3 Code could not read the folder's path."),
80 }),
81 );
82 }
83 };
84
85 const onDragOver = (event: DragEvent) => {
86 if (!isFileDrag(event)) return;
87 if (event.defaultPrevented) {
88 setActive(false);
89 return;
90 }
91 event.preventDefault();
92 event.dataTransfer.dropEffect = "copy";
93 setActive(true);
94 };
95
96 return (
97 <div
98 className="relative flex min-h-0 flex-1 flex-col"
99 onDragEnter={onDragOver}
100 onDragOver={onDragOver}
101 onDragLeave={(event) => {
102 if (!isFileDrag(event)) return;
103 if (event.currentTarget.contains(event.relatedTarget as Node | null)) return;
104 setActive(false);
105 }}
106 onDrop={(event) => {
107 if (!isFileDrag(event)) return;
108 const claimedByRow = event.defaultPrevented;
109 event.preventDefault();
110 setActive(false);
111 const { files, folders } = splitTransferredFiles(event.dataTransfer);
112 if (folders.length > 0) void addFolders(folders);
113 if (files.length > 0 && !claimedByRow) {
114 toastManager.add(
115 stackedThreadToast({
116 type: "info",
117 title: gt("Only folders can be added as projects"),
118 description: gt("Drop files on a thread to attach them."),
119 }),
120 );
121 }
122 }}
123 >
124 {props.children}
125 {isActive ? (
126 <div
127 aria-hidden
128 className="pointer-events-none absolute inset-1.5 z-30 flex flex-col items-center justify-center gap-2 rounded-lg px-4 text-center bg-sidebar/90 text-sm text-sidebar-foreground ring-1 ring-inset ring-primary/70"
129 >
130 <FolderPlusIcon className="size-5 text-sidebar-muted-foreground" />
131 {bridge === undefined
132 ? gt("Dropping folders needs the desktop app")
133 : gt("Drop folders to add projects")}
134 </div>
135 ) : null}
136 </div>
137 );
138 }