Open History to see the last commit
1import {
2 DESKTOP_APP_ACTIVATION_PROTOCOL_VERSION,
3 DesktopAppActivationPlatform,
4} from "@t3tools/contracts";
5import * as Schema from "effect/Schema";
6import { FolderPlusIcon } from "lucide-react";
7import { useGT } from "gt-react";
8import { useState, type DragEvent, type ReactNode } from "react";
9
10import { useOpenLocalWorkspace } from "../../hooks/useOpenLocalWorkspace";
11import { randomUUID } from "../../lib/utils";
12import { resolveDroppedFolderPath } from "../chat/folderDrop";
13import { splitTransferredFiles } from "../chat/workspaceFileDrop";
14import { setFileDragActive } from "../Sidebar.logic";
15import { toastManager } from "../ui/toast";
16import { stackedThreadToast } from "../ui/toastHelpers";
17
18const isActivationPlatform = Schema.is(DesktopAppActivationPlatform);
19
20function 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 */
30export 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`Browsers do not share folder paths. Use Add project to choose a folder.`,
44 }),
45 );
46 return;
47 }
48 const platform = bridge.getClientPlatform();
49 if (!openLocalWorkspace.ready || !isActivationPlatform(platform)) {
50 toastManager.add(
51 stackedThreadToast({
52 type: "error",
53 title: gt`This computer's server is not connected yet`,
54 description: gt`Try again when it connects.`,
55 }),
56 );
57 return;
58 }
59 // One at a time, so the last folder is the one left open.
60 for (const folder of folders) {
61 const workspaceRoot = resolveDroppedFolderPath(folder, bridge.getPathForFile);
62 const response =
63 workspaceRoot === null
64 ? null
65 : await openLocalWorkspace.open({
66 version: DESKTOP_APP_ACTIVATION_PROTOCOL_VERSION,
67 requestId: randomUUID(),
68 type: "open-workspace",
69 workspaceRoot,
70 platform,
71 });
72 if (response?.ok) continue;
73 toastManager.add(
74 stackedThreadToast({
75 type: "error",
76 title: gt`Could not add "${{ name: folder.name }}"`,
77 description: response?.message ?? gt`T3 Code could not read the folder's path.`,
78 }),
79 );
80 }
81 };
82
83 const onDragOver = (event: DragEvent) => {
84 if (!isFileDrag(event)) return;
85 if (event.defaultPrevented) {
86 setActive(false);
87 return;
88 }
89 event.preventDefault();
90 event.dataTransfer.dropEffect = "copy";
91 setActive(true);
92 };
93
94 return (
95 <div
96 className="relative flex min-h-0 flex-1 flex-col"
97 onDragEnter={onDragOver}
98 onDragOver={onDragOver}
99 onDragLeave={(event) => {
100 if (!isFileDrag(event)) return;
101 if (event.currentTarget.contains(event.relatedTarget as Node | null)) return;
102 setActive(false);
103 }}
104 onDrop={(event) => {
105 if (!isFileDrag(event)) return;
106 const claimedByRow = event.defaultPrevented;
107 event.preventDefault();
108 setActive(false);
109 const { files, folders } = splitTransferredFiles(event.dataTransfer);
110 if (folders.length > 0) void addFolders(folders);
111 if (files.length > 0 && !claimedByRow) {
112 toastManager.add(
113 stackedThreadToast({
114 type: "info",
115 title: gt`Only folders can be added as projects`,
116 description: gt`Drop files on a thread to attach them.`,
117 }),
118 );
119 }
120 }}
121 >
122 {props.children}
123 {isActive ? (
124 <div
125 aria-hidden
126 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"
127 >
128 <FolderPlusIcon className="size-5 text-sidebar-muted-foreground" />
129 {bridge === undefined
130 ? gt`Dropping folders needs the desktop app`
131 : gt`Drop folders to add projects`}
132 </div>
133 ) : null}
134 </div>
135 );
136}