Skip to content

Commit 520d29a

Browse files
fix(web): support file drops on every sidebar thread row
1 parent 7b8873b commit 520d29a

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎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}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,8 +1957,13 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
19571957
resultId: string;
19581958
onHighlight: () => void;
19591959
onSelect: () => void;
1960+
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
19601961
}) {
19611962
const { thread } = props;
1963+
const threadRef = useMemo(
1964+
() => scopeThreadRef(thread.environmentId, thread.id),
1965+
[thread.environmentId, thread.id],
1966+
);
19621967
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(
19631968
props.isHighlighted || props.isRouteActive,
19641969
);
@@ -1999,8 +2004,25 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
19992004
threadId: thread.id,
20002005
});
20012006
const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
2007+
const [isFileDragOver, setIsFileDragOver] = useState(false);
2008+
const fileDropHandlers = useMemo(
2009+
() =>
2010+
makeWorkspaceFileDropHandlers({
2011+
setDragActive: setIsFileDragOver,
2012+
addFiles: (files) => {
2013+
props.onFileDropThreads(threadRef, files);
2014+
},
2015+
}),
2016+
[props.onFileDropThreads, threadRef],
2017+
);
2018+
useEffect(() => {
2019+
if (!isFileDragOver) return;
2020+
const clearFileDrag = () => setIsFileDragOver(false);
2021+
window.addEventListener("dragend", clearFileDrag);
2022+
return () => window.removeEventListener("dragend", clearFileDrag);
2023+
}, [isFileDragOver]);
20022024
return (
2003-
<li role="presentation" className="list-none">
2025+
<li role="presentation" className="list-none" {...fileDropHandlers}>
20042026
<Tooltip>
20052027
<TooltipTrigger
20062028
render={
@@ -2026,6 +2048,8 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
20262048
props.isHighlighted || props.isRouteActive
20272049
? "bg-sidebar-row-active text-sidebar-foreground"
20282050
: "text-sidebar-muted-foreground/75 hover:bg-sidebar-row-hover hover:text-sidebar-foreground",
2051+
isFileDragOver && "ring-1 ring-inset ring-primary/70",
2052+
isFileDragOver && !props.isRouteActive && "bg-sidebar-row-hover",
20292053
)}
20302054
/>
20312055
}
@@ -4547,6 +4571,7 @@ export default function Sidebar() {
45474571
resultId={`sidebar-thread-search-result-${index}`}
45484572
onHighlight={() => setActiveSearchResultIndex(index)}
45494573
onSelect={() => selectThreadSearchResult(thread)}
4574+
onFileDropThreads={handleThreadFileDrop}
45504575
/>
45514576
);
45524577
})}

0 commit comments

Comments
 (0)