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

SidebarFolderDrop.tsx

134 lines4.6 KB

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