Skip to content

Commit bde39d4

Browse files
feat(web): accept file drops into sidebar threads (pingdotgg#7892)
1 parent eb11506 commit bde39d4

7 files changed

Lines changed: 462 additions & 8 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
189189
import { subscribePreviewAction } from "./preview/previewActionBus";
190190
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
191191
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
192+
import {
193+
isSameSidebarThreadRef,
194+
useSidebarPendingFileDropStore,
195+
} from "../sidebarPendingFileDropStore";
192196
import {
193197
selectThreadPreviewMiniPlayer,
194198
usePreviewMiniPlayerStore,
@@ -7867,6 +7871,53 @@ export default function ChatView(props: ChatViewProps) {
78677871
void onRevertToTurnCountRef.current(targetTurnCount);
78687872
}, []);
78697873

7874+
// Files dropped on a sidebar row land here once the dropped-on thread is
7875+
// actually open, then take the exact same path as a workspace drop:
7876+
// validate, compress, focus the composer, never send. Kept above the
7877+
// no-active-thread early return so hook order never changes.
7878+
const pendingSidebarFileDrops = useSidebarPendingFileDropStore((state) => state.pending);
7879+
const consumePendingFileDrop = useSidebarPendingFileDropStore(
7880+
(state) => state.consumePendingFileDrop,
7881+
);
7882+
useEffect(() => {
7883+
if (pendingSidebarFileDrops.length === 0) return;
7884+
// A promoting draft can mount this view with the server thread id while
7885+
// its composer is still draft-keyed; finalization would discard what we
7886+
// attach there. Only the canonical thread target may consume a drop.
7887+
if (
7888+
typeof composerDraftTarget === "string" ||
7889+
!pendingSidebarFileDrops.some((drop) =>
7890+
isSameSidebarThreadRef(composerDraftTarget, drop.threadRef),
7891+
)
7892+
) {
7893+
return;
7894+
}
7895+
if (!activeThread) return;
7896+
if (!composerRef.current) {
7897+
const raf = window.requestAnimationFrame(() => {
7898+
if (!composerRef.current) return;
7899+
if (typeof composerDraftTarget === "string") return;
7900+
// Consume matches by target, so a newer drop that arrived meanwhile
7901+
// is collected too rather than orphaned.
7902+
const files = consumePendingFileDrop(composerDraftTarget);
7903+
if (files !== null) {
7904+
composerRef.current?.addDroppedFiles(files);
7905+
}
7906+
});
7907+
return () => window.cancelAnimationFrame(raf);
7908+
}
7909+
const files = consumePendingFileDrop(composerDraftTarget);
7910+
if (files !== null) {
7911+
composerRef.current.addDroppedFiles(files);
7912+
}
7913+
}, [
7914+
activeThread,
7915+
composerDraftTarget,
7916+
composerRef,
7917+
consumePendingFileDrop,
7918+
pendingSidebarFileDrops,
7919+
]);
7920+
78707921
// Empty state: no active thread
78717922
if (!activeThread) {
78727923
return <NoActiveThreadState />;

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ import { useOpenPrLink } from "../lib/openPullRequestLink";
7878
import { releaseProjectDraftUploads } from "../lib/composerDraftUploads";
7979
import { isTerminalFocused } from "../lib/terminalFocus";
8080
import { isMacPlatform } from "../lib/utils";
81+
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
82+
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
8183
import {
8284
readThreadShell,
8385
useProjects,
@@ -326,7 +328,7 @@ interface SidebarThreadRowProps {
326328
threadRef: ScopedThreadRef,
327329
orderedProjectThreadKeys: readonly string[],
328330
) => void;
329-
navigateToThread: (threadRef: ScopedThreadRef) => void;
331+
navigateToThread: (threadRef: ScopedThreadRef) => Promise<void>;
330332
handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise<void>;
331333
handleThreadContextMenu: (
332334
threadRef: ScopedThreadRef,
@@ -345,6 +347,7 @@ interface SidebarThreadRowProps {
345347
prUrl: string,
346348
threadRef?: ScopedThreadRef,
347349
) => boolean;
350+
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
348351
}
349352

350353
const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {
@@ -372,10 +375,28 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
372375
cancelRename,
373376
attemptArchiveThread,
374377
openPrLink,
378+
onFileDropThreads,
375379
thread,
376380
} = props;
377381
const threadRef = scopeThreadRef(thread.environmentId, thread.id);
378382
const threadKey = scopedThreadKey(threadRef);
383+
const [isFileDragOver, setIsFileDragOver] = useState(false);
384+
const fileDropHandlers = useMemo(
385+
() =>
386+
makeWorkspaceFileDropHandlers({
387+
setDragActive: setIsFileDragOver,
388+
addFiles: (files) => {
389+
onFileDropThreads(threadRef, files);
390+
},
391+
}),
392+
[onFileDropThreads, threadRef],
393+
);
394+
useEffect(() => {
395+
if (!isFileDragOver) return;
396+
const clearFileDrag = () => setIsFileDragOver(false);
397+
window.addEventListener("dragend", clearFileDrag);
398+
return () => window.removeEventListener("dragend", clearFileDrag);
399+
}, [isFileDragOver]);
379400
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(isActive);
380401
const lastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
381402
const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey));
@@ -663,6 +684,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
663684
ref={rowRef}
664685
className="w-full"
665686
data-thread-item
687+
{...fileDropHandlers}
666688
onMouseLeave={handleMouseLeave}
667689
onBlurCapture={handleBlurCapture}
668690
>
@@ -674,7 +696,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
674696
className={`${resolveThreadRowClassName({
675697
isActive,
676698
isSelected,
677-
})} relative isolate`}
699+
})} relative isolate${isFileDragOver ? " ring-1 ring-inset ring-primary/70" : ""}`}
678700
onClick={handleRowClick}
679701
onDoubleClick={handleRowDoubleClick}
680702
onKeyDown={handleRowKeyDown}
@@ -916,7 +938,8 @@ interface SidebarProjectThreadListProps {
916938
threadRef: ScopedThreadRef,
917939
orderedProjectThreadKeys: readonly string[],
918940
) => void;
919-
navigateToThread: (threadRef: ScopedThreadRef) => void;
941+
navigateToThread: (threadRef: ScopedThreadRef) => Promise<void>;
942+
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
920943
handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise<void>;
921944
handleThreadContextMenu: (
922945
threadRef: ScopedThreadRef,
@@ -968,6 +991,7 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
968991
attachThreadListAutoAnimateRef,
969992
handleThreadClick,
970993
navigateToThread,
994+
onFileDropThreads,
971995
handleMultiSelectContextMenu,
972996
handleThreadContextMenu,
973997
clearSelection,
@@ -1019,6 +1043,7 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
10191043
confirmArchiveButtonRefs={confirmArchiveButtonRefs}
10201044
handleThreadClick={handleThreadClick}
10211045
navigateToThread={navigateToThread}
1046+
onFileDropThreads={onFileDropThreads}
10221047
handleMultiSelectContextMenu={handleMultiSelectContextMenu}
10231048
handleThreadContextMenu={handleThreadContextMenu}
10241049
clearSelection={clearSelection}
@@ -1136,6 +1161,8 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
11361161
(settings) => settings.sidebarThreadPreviewCount,
11371162
);
11381163
const router = useRouter();
1164+
const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
1165+
const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
11391166
const { isMobile, setOpenMobile } = useSidebar();
11401167
const markThreadUnread = useUiStateStore((state) => state.markThreadUnread);
11411168
const setProjectExpanded = useUiStateStore((state) => state.setProjectExpanded);
@@ -1740,13 +1767,32 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
17401767
if (isMobile) {
17411768
setOpenMobile(false);
17421769
}
1743-
void router.navigate({
1770+
return router.navigate({
17441771
to: "/$environmentId/$threadId",
17451772
params: buildThreadRouteParams(threadRef),
17461773
});
17471774
},
17481775
[clearSelection, isMobile, router, setOpenMobile, setSelectionAnchor],
17491776
);
1777+
const handleThreadFileDrop = useCallback(
1778+
async (threadRef: ScopedThreadRef, files: File[]) => {
1779+
const dropId = queuePendingFileDrop({ threadRef, files });
1780+
const targetPathname = router.buildLocation({
1781+
to: "/$environmentId/$threadId",
1782+
params: buildThreadRouteParams(threadRef),
1783+
}).pathname;
1784+
if (targetPathname === router.state.location.pathname) return;
1785+
try {
1786+
await navigateToThread(threadRef);
1787+
if (targetPathname !== router.state.location.pathname) {
1788+
clearPendingFileDrop(dropId);
1789+
}
1790+
} catch {
1791+
clearPendingFileDrop(dropId);
1792+
}
1793+
},
1794+
[clearPendingFileDrop, navigateToThread, queuePendingFileDrop, router],
1795+
);
17501796

17511797
const handleThreadClick = useCallback(
17521798
(
@@ -2409,6 +2455,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
24092455
attachThreadListAutoAnimateRef={attachThreadListAutoAnimateRef}
24102456
handleThreadClick={handleThreadClick}
24112457
navigateToThread={navigateToThread}
2458+
onFileDropThreads={handleThreadFileDrop}
24122459
handleMultiSelectContextMenu={handleMultiSelectContextMenu}
24132460
handleThreadContextMenu={handleThreadContextMenu}
24142461
clearSelection={clearSelection}

0 commit comments

Comments
 (0)