feat(web): accept file drops into sidebar threads (#7892)
This commit is contained in:
1 parent
eb11506363
commit
bde39d4d79
7 files changed
+462
-8
No files matched your search
@@ -189,6 +189,10 @@ import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
|
||||
import { subscribePreviewAction } from "./preview/previewActionBus";
|
||||
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
|
||||
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
|
||||
import {
|
||||
isSameSidebarThreadRef,
|
||||
useSidebarPendingFileDropStore,
|
||||
} from "../sidebarPendingFileDropStore";
|
||||
import {
|
||||
selectThreadPreviewMiniPlayer,
|
||||
usePreviewMiniPlayerStore,
|
||||
@@ -7867,6 +7871,53 @@ export default function ChatView(props: ChatViewProps) {
|
||||
void onRevertToTurnCountRef.current(targetTurnCount);
|
||||
}, []);
|
||||
|
||||
// Files dropped on a sidebar row land here once the dropped-on thread is
|
||||
// actually open, then take the exact same path as a workspace drop:
|
||||
// validate, compress, focus the composer, never send. Kept above the
|
||||
// no-active-thread early return so hook order never changes.
|
||||
const pendingSidebarFileDrops = useSidebarPendingFileDropStore((state) => state.pending);
|
||||
const consumePendingFileDrop = useSidebarPendingFileDropStore(
|
||||
(state) => state.consumePendingFileDrop,
|
||||
);
|
||||
useEffect(() => {
|
||||
if (pendingSidebarFileDrops.length === 0) return;
|
||||
// A promoting draft can mount this view with the server thread id while
|
||||
// its composer is still draft-keyed; finalization would discard what we
|
||||
// attach there. Only the canonical thread target may consume a drop.
|
||||
if (
|
||||
typeof composerDraftTarget === "string" ||
|
||||
!pendingSidebarFileDrops.some((drop) =>
|
||||
isSameSidebarThreadRef(composerDraftTarget, drop.threadRef),
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (!activeThread) return;
|
||||
if (!composerRef.current) {
|
||||
const raf = window.requestAnimationFrame(() => {
|
||||
if (!composerRef.current) return;
|
||||
if (typeof composerDraftTarget === "string") return;
|
||||
// Consume matches by target, so a newer drop that arrived meanwhile
|
||||
// is collected too rather than orphaned.
|
||||
const files = consumePendingFileDrop(composerDraftTarget);
|
||||
if (files !== null) {
|
||||
composerRef.current?.addDroppedFiles(files);
|
||||
}
|
||||
});
|
||||
return () => window.cancelAnimationFrame(raf);
|
||||
}
|
||||
const files = consumePendingFileDrop(composerDraftTarget);
|
||||
if (files !== null) {
|
||||
composerRef.current.addDroppedFiles(files);
|
||||
}
|
||||
}, [
|
||||
activeThread,
|
||||
composerDraftTarget,
|
||||
composerRef,
|
||||
consumePendingFileDrop,
|
||||
pendingSidebarFileDrops,
|
||||
]);
|
||||
|
||||
// Empty state: no active thread
|
||||
if (!activeThread) {
|
||||
return <NoActiveThreadState />;
|
||||
|
||||
@@ -78,6 +78,8 @@ import { useOpenPrLink } from "../lib/openPullRequestLink";
|
||||
import { releaseProjectDraftUploads } from "../lib/composerDraftUploads";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { isMacPlatform } from "../lib/utils";
|
||||
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
|
||||
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
|
||||
import {
|
||||
readThreadShell,
|
||||
useProjects,
|
||||
@@ -326,7 +328,7 @@ interface SidebarThreadRowProps {
|
||||
threadRef: ScopedThreadRef,
|
||||
orderedProjectThreadKeys: readonly string[],
|
||||
) => void;
|
||||
navigateToThread: (threadRef: ScopedThreadRef) => void;
|
||||
navigateToThread: (threadRef: ScopedThreadRef) => Promise<void>;
|
||||
handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise<void>;
|
||||
handleThreadContextMenu: (
|
||||
threadRef: ScopedThreadRef,
|
||||
@@ -345,6 +347,7 @@ interface SidebarThreadRowProps {
|
||||
prUrl: string,
|
||||
threadRef?: ScopedThreadRef,
|
||||
) => boolean;
|
||||
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
|
||||
}
|
||||
|
||||
const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {
|
||||
@@ -372,10 +375,28 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
|
||||
cancelRename,
|
||||
attemptArchiveThread,
|
||||
openPrLink,
|
||||
onFileDropThreads,
|
||||
thread,
|
||||
} = props;
|
||||
const threadRef = scopeThreadRef(thread.environmentId, thread.id);
|
||||
const threadKey = scopedThreadKey(threadRef);
|
||||
const [isFileDragOver, setIsFileDragOver] = useState(false);
|
||||
const fileDropHandlers = useMemo(
|
||||
() =>
|
||||
makeWorkspaceFileDropHandlers({
|
||||
setDragActive: setIsFileDragOver,
|
||||
addFiles: (files) => {
|
||||
onFileDropThreads(threadRef, files);
|
||||
},
|
||||
}),
|
||||
[onFileDropThreads, threadRef],
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!isFileDragOver) return;
|
||||
const clearFileDrag = () => setIsFileDragOver(false);
|
||||
window.addEventListener("dragend", clearFileDrag);
|
||||
return () => window.removeEventListener("dragend", clearFileDrag);
|
||||
}, [isFileDragOver]);
|
||||
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(isActive);
|
||||
const lastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
|
||||
const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey));
|
||||
@@ -663,6 +684,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
|
||||
ref={rowRef}
|
||||
className="w-full"
|
||||
data-thread-item
|
||||
{...fileDropHandlers}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onBlurCapture={handleBlurCapture}
|
||||
>
|
||||
@@ -674,7 +696,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
|
||||
className={`${resolveThreadRowClassName({
|
||||
isActive,
|
||||
isSelected,
|
||||
})} relative isolate`}
|
||||
})} relative isolate${isFileDragOver ? " ring-1 ring-inset ring-primary/70" : ""}`}
|
||||
onClick={handleRowClick}
|
||||
onDoubleClick={handleRowDoubleClick}
|
||||
onKeyDown={handleRowKeyDown}
|
||||
@@ -916,7 +938,8 @@ interface SidebarProjectThreadListProps {
|
||||
threadRef: ScopedThreadRef,
|
||||
orderedProjectThreadKeys: readonly string[],
|
||||
) => void;
|
||||
navigateToThread: (threadRef: ScopedThreadRef) => void;
|
||||
navigateToThread: (threadRef: ScopedThreadRef) => Promise<void>;
|
||||
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
|
||||
handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise<void>;
|
||||
handleThreadContextMenu: (
|
||||
threadRef: ScopedThreadRef,
|
||||
@@ -968,6 +991,7 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
|
||||
attachThreadListAutoAnimateRef,
|
||||
handleThreadClick,
|
||||
navigateToThread,
|
||||
onFileDropThreads,
|
||||
handleMultiSelectContextMenu,
|
||||
handleThreadContextMenu,
|
||||
clearSelection,
|
||||
@@ -1019,6 +1043,7 @@ const SidebarProjectThreadList = memo(function SidebarProjectThreadList(
|
||||
confirmArchiveButtonRefs={confirmArchiveButtonRefs}
|
||||
handleThreadClick={handleThreadClick}
|
||||
navigateToThread={navigateToThread}
|
||||
onFileDropThreads={onFileDropThreads}
|
||||
handleMultiSelectContextMenu={handleMultiSelectContextMenu}
|
||||
handleThreadContextMenu={handleThreadContextMenu}
|
||||
clearSelection={clearSelection}
|
||||
@@ -1136,6 +1161,8 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
|
||||
(settings) => settings.sidebarThreadPreviewCount,
|
||||
);
|
||||
const router = useRouter();
|
||||
const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
|
||||
const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
|
||||
const { isMobile, setOpenMobile } = useSidebar();
|
||||
const markThreadUnread = useUiStateStore((state) => state.markThreadUnread);
|
||||
const setProjectExpanded = useUiStateStore((state) => state.setProjectExpanded);
|
||||
@@ -1740,13 +1767,32 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
|
||||
if (isMobile) {
|
||||
setOpenMobile(false);
|
||||
}
|
||||
void router.navigate({
|
||||
return router.navigate({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(threadRef),
|
||||
});
|
||||
},
|
||||
[clearSelection, isMobile, router, setOpenMobile, setSelectionAnchor],
|
||||
);
|
||||
const handleThreadFileDrop = useCallback(
|
||||
async (threadRef: ScopedThreadRef, files: File[]) => {
|
||||
const dropId = queuePendingFileDrop({ threadRef, files });
|
||||
const targetPathname = router.buildLocation({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(threadRef),
|
||||
}).pathname;
|
||||
if (targetPathname === router.state.location.pathname) return;
|
||||
try {
|
||||
await navigateToThread(threadRef);
|
||||
if (targetPathname !== router.state.location.pathname) {
|
||||
clearPendingFileDrop(dropId);
|
||||
}
|
||||
} catch {
|
||||
clearPendingFileDrop(dropId);
|
||||
}
|
||||
},
|
||||
[clearPendingFileDrop, navigateToThread, queuePendingFileDrop, router],
|
||||
);
|
||||
|
||||
const handleThreadClick = useCallback(
|
||||
(
|
||||
@@ -2409,6 +2455,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
|
||||
attachThreadListAutoAnimateRef={attachThreadListAutoAnimateRef}
|
||||
handleThreadClick={handleThreadClick}
|
||||
navigateToThread={navigateToThread}
|
||||
onFileDropThreads={handleThreadFileDrop}
|
||||
handleMultiSelectContextMenu={handleMultiSelectContextMenu}
|
||||
handleThreadContextMenu={handleThreadContextMenu}
|
||||
clearSelection={clearSelection}
|
||||
|
||||
@@ -94,6 +94,10 @@ import { isMacPlatform } from "~/lib/utils";
|
||||
import { useOpenPrLink } from "../lib/openPullRequestLink";
|
||||
import { releaseComposerDraftUploads } from "../lib/composerDraftUploads";
|
||||
import { readLocalApi } from "../localApi";
|
||||
import {
|
||||
isSameSidebarThreadRef,
|
||||
useSidebarPendingFileDropStore,
|
||||
} from "../sidebarPendingFileDropStore";
|
||||
import { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject";
|
||||
import {
|
||||
buildSidebarProjectSnapshots,
|
||||
@@ -195,6 +199,7 @@ import {
|
||||
type SnoozePreset,
|
||||
} from "./Sidebar.snooze";
|
||||
import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
|
||||
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
|
||||
import { ProviderInstanceIcon } from "./chat/ProviderInstanceIcon";
|
||||
import { getTriggerDisplayModelLabel } from "./chat/providerIconUtils";
|
||||
import {
|
||||
@@ -980,6 +985,12 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
onUnsnooze: (threadRef: ScopedThreadRef) => void;
|
||||
onUnpin: (threadRef: ScopedThreadRef) => void;
|
||||
onAcknowledgeWoke: (threadRef: ScopedThreadRef, visitedAt: string) => void;
|
||||
/**
|
||||
* External files dropped onto this row. The row highlights while the drag
|
||||
* is over it; the callback opens the thread and hands the files to its
|
||||
* composer. Absent when the sidebar cannot open server threads.
|
||||
*/
|
||||
onFileDropThreads?: ((threadRef: ScopedThreadRef, files: File[]) => void) | undefined;
|
||||
}) {
|
||||
const {
|
||||
isRenaming,
|
||||
@@ -987,6 +998,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
onCommitRename,
|
||||
onContextMenu,
|
||||
onAcknowledgeWoke,
|
||||
onFileDropThreads,
|
||||
onRenameTitleChange,
|
||||
onSettle,
|
||||
onSnooze,
|
||||
@@ -1224,6 +1236,27 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
},
|
||||
[isRenaming, onStartRename, thread.title, threadRef],
|
||||
);
|
||||
const [isFileDragOver, setIsFileDragOver] = useState(false);
|
||||
const fileDropHandlers = useMemo(
|
||||
() =>
|
||||
onFileDropThreads
|
||||
? makeWorkspaceFileDropHandlers({
|
||||
setDragActive: setIsFileDragOver,
|
||||
addFiles: (files) => {
|
||||
onFileDropThreads(threadRef, files);
|
||||
},
|
||||
})
|
||||
: null,
|
||||
[onFileDropThreads, threadRef],
|
||||
);
|
||||
// A drop lands on a child or outside the window entirely, so dragend is
|
||||
// the reset of last resort for the row's highlight.
|
||||
useEffect(() => {
|
||||
if (!isFileDragOver) return;
|
||||
const clearFileDrag = () => setIsFileDragOver(false);
|
||||
window.addEventListener("dragend", clearFileDrag);
|
||||
return () => window.removeEventListener("dragend", clearFileDrag);
|
||||
}, [isFileDragOver]);
|
||||
const renameCommittedRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (isRenaming) renameCommittedRef.current = false;
|
||||
@@ -1338,6 +1371,9 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
!props.isActive &&
|
||||
!isSelected &&
|
||||
"opacity-70 transition-opacity hover:opacity-100",
|
||||
isFileDragOver && "ring-1 ring-inset ring-primary/70",
|
||||
// The hover tint must not clobber an active/selected row's own surface.
|
||||
isFileDragOver && !props.isActive && !isSelected && "bg-sidebar-row-hover",
|
||||
// The lifted row is an opaque card so the rows beneath it never show
|
||||
// through. The row tint is translucent in dark themes and the pointer
|
||||
// keeps the hover color applied, so both the tint and the solid sidebar
|
||||
@@ -1505,6 +1541,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
<li
|
||||
data-thread-item
|
||||
{...sortableRootProps}
|
||||
{...(fileDropHandlers ?? {})}
|
||||
className={cn(
|
||||
// Matches the h-9 row so unrendered rows never shift the list when they paint.
|
||||
"list-none [content-visibility:auto] [contain-intrinsic-size:auto_36px]",
|
||||
@@ -1657,6 +1694,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
|
||||
<li
|
||||
data-thread-item
|
||||
{...sortableRootProps}
|
||||
{...(fileDropHandlers ?? {})}
|
||||
className={cn(
|
||||
// Matches the h-[4.875rem] content box; the py-0.5 padding is added on top.
|
||||
"list-none py-0.5 [content-visibility:auto] [contain-intrinsic-size:auto_78px]",
|
||||
@@ -1919,8 +1957,13 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
|
||||
resultId: string;
|
||||
onHighlight: () => void;
|
||||
onSelect: () => void;
|
||||
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
|
||||
}) {
|
||||
const { thread } = props;
|
||||
const threadRef = useMemo(
|
||||
() => scopeThreadRef(thread.environmentId, thread.id),
|
||||
[thread.environmentId, thread.id],
|
||||
);
|
||||
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(
|
||||
props.isHighlighted || props.isRouteActive,
|
||||
);
|
||||
@@ -1961,8 +2004,25 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
|
||||
threadId: thread.id,
|
||||
});
|
||||
const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
|
||||
const [isFileDragOver, setIsFileDragOver] = useState(false);
|
||||
const fileDropHandlers = useMemo(
|
||||
() =>
|
||||
makeWorkspaceFileDropHandlers({
|
||||
setDragActive: setIsFileDragOver,
|
||||
addFiles: (files) => {
|
||||
props.onFileDropThreads(threadRef, files);
|
||||
},
|
||||
}),
|
||||
[props.onFileDropThreads, threadRef],
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!isFileDragOver) return;
|
||||
const clearFileDrag = () => setIsFileDragOver(false);
|
||||
window.addEventListener("dragend", clearFileDrag);
|
||||
return () => window.removeEventListener("dragend", clearFileDrag);
|
||||
}, [isFileDragOver]);
|
||||
return (
|
||||
<li role="presentation" className="list-none">
|
||||
<li role="presentation" className="list-none" {...fileDropHandlers}>
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
@@ -1988,6 +2048,8 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
|
||||
props.isHighlighted || props.isRouteActive
|
||||
? "bg-sidebar-row-active text-sidebar-foreground"
|
||||
: "text-sidebar-muted-foreground/75 hover:bg-sidebar-row-hover hover:text-sidebar-foreground",
|
||||
isFileDragOver && "ring-1 ring-inset ring-primary/70",
|
||||
isFileDragOver && !props.isRouteActive && "bg-sidebar-row-hover",
|
||||
)}
|
||||
/>
|
||||
}
|
||||
@@ -2707,7 +2769,7 @@ export default function Sidebar() {
|
||||
if (isMobile) {
|
||||
setOpenMobile(false);
|
||||
}
|
||||
void router.navigate({
|
||||
return router.navigate({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(threadRef),
|
||||
});
|
||||
@@ -2715,6 +2777,48 @@ export default function Sidebar() {
|
||||
[clearSelection, isMobile, router, setOpenMobile, setSelectionAnchor],
|
||||
);
|
||||
|
||||
// Dropping files on a row opens that thread and attaches the files there.
|
||||
// The composer only accepts drops for its OWN thread, so when the row is
|
||||
// not the open thread we stash the files and let ChatView hand them over
|
||||
// once the navigation actually lands; if the route bounced (thread gone),
|
||||
// nothing will consume them, so clear instead of surprising the user later.
|
||||
const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
|
||||
const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
|
||||
const handleThreadFileDrop = useCallback(
|
||||
async (threadRef: ScopedThreadRef, files: File[]) => {
|
||||
// Queued, not replaced: a second drop before the thread opens keeps
|
||||
// both files, and the id lets cleanup below touch only this drop.
|
||||
const dropId = queuePendingFileDrop({ threadRef, files });
|
||||
// Key match alone is not "already there": during draft promotion the
|
||||
// resolved route key is the server thread while the URL is still the
|
||||
// draft route, and its composer would swallow the drop then discard it.
|
||||
const landedBefore =
|
||||
router.buildLocation({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(threadRef),
|
||||
}).pathname === router.state.location.pathname;
|
||||
if (landedBefore) return;
|
||||
try {
|
||||
await navigateToThread(threadRef);
|
||||
// A newer drop may have arrived while the navigation was in flight;
|
||||
// clearing by id leaves those files untouched.
|
||||
const landed =
|
||||
router.buildLocation({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(threadRef),
|
||||
}).pathname === router.state.location.pathname;
|
||||
if (!landed) {
|
||||
clearPendingFileDrop(dropId);
|
||||
}
|
||||
} catch {
|
||||
// Navigation failed outright; nothing will consume this drop, but a
|
||||
// newer drop for the same thread may still be deliverable.
|
||||
clearPendingFileDrop(dropId);
|
||||
}
|
||||
},
|
||||
[clearPendingFileDrop, navigateToThread, queuePendingFileDrop, router],
|
||||
);
|
||||
|
||||
const navigateToDraft = useCallback(
|
||||
(draftId: DraftId) => {
|
||||
// Unconditional: also drops a stale selection anchor left by
|
||||
@@ -4467,6 +4571,7 @@ export default function Sidebar() {
|
||||
resultId={`sidebar-thread-search-result-${index}`}
|
||||
onHighlight={() => setActiveSearchResultIndex(index)}
|
||||
onSelect={() => selectThreadSearchResult(thread)}
|
||||
onFileDropThreads={handleThreadFileDrop}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
@@ -4612,6 +4717,7 @@ export default function Sidebar() {
|
||||
onUnsnooze={attemptUnsnooze}
|
||||
onUnpin={attemptUnpin}
|
||||
onAcknowledgeWoke={acknowledgeWoke}
|
||||
onFileDropThreads={handleThreadFileDrop}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@ import { threadHasStarted } from "../components/ChatView.logic";
|
||||
import { finalizePromotedDraftThreadByRef, useComposerDraftStore } from "../composerDraftStore";
|
||||
import { resolveThreadRouteRef, resolveThreadRouteRenderState } from "../threadRoutes";
|
||||
import { resolveThreadSyncPhase } from "../threadSync";
|
||||
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
|
||||
import { SidebarInset } from "~/components/ui/sidebar";
|
||||
import {
|
||||
useEnvironmentThreadRefs,
|
||||
@@ -62,8 +63,15 @@ function ChatThreadRouteView() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (renderState === "missing" && environmentHasAnyThreads) {
|
||||
void navigate({ to: "/", replace: true });
|
||||
// Navigation already resolved onto this path, so a drop aimed here
|
||||
// passed its landing check; once the thread reads as missing it can
|
||||
// never be attached, release it even when there is nowhere to redirect.
|
||||
if (renderState === "missing") {
|
||||
const { clearPendingFileDropsForThread } = useSidebarPendingFileDropStore.getState();
|
||||
clearPendingFileDropsForThread(threadRef);
|
||||
if (environmentHasAnyThreads) {
|
||||
void navigate({ to: "/", replace: true });
|
||||
}
|
||||
}
|
||||
}, [bootstrapComplete, environmentHasAnyThreads, navigate, renderState, threadRef]);
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
import { beforeEach, describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
||||
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
|
||||
|
||||
import {
|
||||
isSameSidebarThreadRef,
|
||||
useSidebarPendingFileDropStore,
|
||||
type SidebarPendingFileDrop,
|
||||
} from "./sidebarPendingFileDropStore";
|
||||
|
||||
function makeFiles(...names: string[]): File[] {
|
||||
return names.map((name) => new File(["x"], name));
|
||||
}
|
||||
|
||||
function makeEntry(
|
||||
environmentId: string,
|
||||
threadId: string,
|
||||
files: File[],
|
||||
): Omit<SidebarPendingFileDrop, "id"> {
|
||||
return {
|
||||
threadRef: scopeThreadRef(environmentId as EnvironmentId, ThreadId.make(threadId)),
|
||||
files,
|
||||
};
|
||||
}
|
||||
|
||||
function fileNames(files: File[]): string[] {
|
||||
return files.map((file) => file.name);
|
||||
}
|
||||
|
||||
function refOf(environmentId: string, threadId: string) {
|
||||
return makeEntry(environmentId, threadId, []).threadRef;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
useSidebarPendingFileDropStore.setState({ pending: [] });
|
||||
});
|
||||
|
||||
describe("sidebarPendingFileDropStore", () => {
|
||||
it("starts empty", () => {
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
|
||||
});
|
||||
|
||||
it("stashes and consumes a drop for the matching thread", () => {
|
||||
const files = makeFiles("a.png", "b.png");
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", files));
|
||||
|
||||
expect(
|
||||
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-1")),
|
||||
).toEqual(files);
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
|
||||
});
|
||||
|
||||
it("accumulates repeat drops onto the same thread instead of replacing", () => {
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
|
||||
|
||||
expect(
|
||||
fileNames(
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.consumePendingFileDrop(refOf("env-1", "thread-1")) ?? [],
|
||||
),
|
||||
).toEqual(["a.png", "b.png"]);
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps drops for other threads when consuming one thread", () => {
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-2", makeFiles("b.png")));
|
||||
|
||||
expect(
|
||||
fileNames(
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.consumePendingFileDrop(refOf("env-1", "thread-2")) ?? [],
|
||||
),
|
||||
).toEqual(["b.png"]);
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("clears only the drop matching a stale navigation's id", () => {
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
const firstId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
|
||||
|
||||
useSidebarPendingFileDropStore.getState().clearPendingFileDrop(firstId);
|
||||
expect(
|
||||
fileNames(
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.consumePendingFileDrop(refOf("env-1", "thread-1")) ?? [],
|
||||
),
|
||||
).toEqual(["b.png"]);
|
||||
});
|
||||
|
||||
it("keeps a newer drop deliverable after the first navigation fails", () => {
|
||||
// Mirrors handleThreadFileDrop: two drops queued for the same unopened
|
||||
// thread, then the first navigation fails (or lands elsewhere) and cleans
|
||||
// up by its own drop id. The newer drop must survive with its files
|
||||
// intact so the thread opening still attaches them.
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
const firstId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
|
||||
const secondId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
|
||||
|
||||
// First navigation fails: handler clears only its own drop.
|
||||
useSidebarPendingFileDropStore.getState().clearPendingFileDrop(firstId);
|
||||
const remaining = useSidebarPendingFileDropStore.getState().pending;
|
||||
expect(remaining).toHaveLength(1);
|
||||
expect(remaining[0]?.id).toBe(secondId);
|
||||
expect(fileNames(remaining[0]?.files ?? [])).toEqual(["b.png"]);
|
||||
|
||||
// Thread opens: the surviving drop is still attached.
|
||||
expect(
|
||||
fileNames(
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.consumePendingFileDrop(refOf("env-1", "thread-1")) ?? [],
|
||||
),
|
||||
).toEqual(["b.png"]);
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
|
||||
});
|
||||
|
||||
it("clears every drop for a missing thread", () => {
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
|
||||
store.queuePendingFileDrop(makeEntry("env-1", "thread-2", makeFiles("c.png")));
|
||||
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.clearPendingFileDropsForThread(refOf("env-1", "thread-1"));
|
||||
expect(
|
||||
fileNames(
|
||||
useSidebarPendingFileDropStore
|
||||
.getState()
|
||||
.consumePendingFileDrop(refOf("env-1", "thread-2")) ?? [],
|
||||
),
|
||||
).toEqual(["c.png"]);
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
|
||||
});
|
||||
|
||||
it("does not confuse refs whose joined keys collide on colons", () => {
|
||||
const store = useSidebarPendingFileDropStore.getState();
|
||||
store.queuePendingFileDrop(makeEntry("a", "b:c", makeFiles("a.png")));
|
||||
|
||||
expect(
|
||||
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("a:b", "c")),
|
||||
).toBeNull();
|
||||
expect(useSidebarPendingFileDropStore.getState().pending).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isSameSidebarThreadRef", () => {
|
||||
it("compares fields, not joined keys", () => {
|
||||
expect(isSameSidebarThreadRef(refOf("a", "b:c"), refOf("a", "b:c"))).toBe(true);
|
||||
expect(isSameSidebarThreadRef(refOf("a", "b:c"), refOf("a:b", "c"))).toBe(false);
|
||||
expect(isSameSidebarThreadRef(refOf("a", "b"), refOf("a", "c"))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
import type { ScopedThreadRef } from "@t3tools/contracts";
|
||||
|
||||
/**
|
||||
* Field-wise ref equality. `scopedThreadKey` joins with `:`, so two distinct
|
||||
* refs can collide when an id itself contains one; drops must never cross
|
||||
* threads on that account.
|
||||
*/
|
||||
export function isSameSidebarThreadRef(a: ScopedThreadRef, b: ScopedThreadRef): boolean {
|
||||
return a.environmentId === b.environmentId && a.threadId === b.threadId;
|
||||
}
|
||||
|
||||
/**
|
||||
* One sidebar row drop. Drops queue up instead of replacing each other, so a
|
||||
* second drop onto the same thread before it opens keeps both files; each
|
||||
* entry carries its own id so a stale navigation can only ever clear the drop
|
||||
* that started it.
|
||||
*/
|
||||
export interface SidebarPendingFileDrop {
|
||||
id: string;
|
||||
threadRef: ScopedThreadRef;
|
||||
files: File[];
|
||||
}
|
||||
|
||||
interface SidebarPendingFileDropStoreState {
|
||||
pending: SidebarPendingFileDrop[];
|
||||
/**
|
||||
* Appends a drop to the queue and returns its id, for later
|
||||
* identity-checked cleanup.
|
||||
*/
|
||||
queuePendingFileDrop: (entry: Omit<SidebarPendingFileDrop, "id">) => string;
|
||||
/** Removes the single drop with this id, leaving newer drops untouched. */
|
||||
clearPendingFileDrop: (id: string) => void;
|
||||
/** Removes every queued drop aimed at this thread (e.g. it went missing). */
|
||||
clearPendingFileDropsForThread: (threadRef: ScopedThreadRef) => void;
|
||||
/**
|
||||
* Returns every queued drop's files for `threadRef`, oldest first, and
|
||||
* removes them; returns null (leaving state untouched) when none match.
|
||||
*/
|
||||
consumePendingFileDrop: (threadRef: ScopedThreadRef) => File[] | null;
|
||||
}
|
||||
|
||||
let nextPendingFileDropId = 0;
|
||||
|
||||
export const useSidebarPendingFileDropStore = create<SidebarPendingFileDropStoreState>()(
|
||||
(set, get) => ({
|
||||
pending: [],
|
||||
queuePendingFileDrop: (entry) => {
|
||||
const id = `sidebar-file-drop-${(nextPendingFileDropId += 1)}`;
|
||||
set((state) => ({ pending: [...state.pending, { ...entry, id }] }));
|
||||
return id;
|
||||
},
|
||||
clearPendingFileDrop: (id) => {
|
||||
set((state) => ({ pending: state.pending.filter((drop) => drop.id !== id) }));
|
||||
},
|
||||
clearPendingFileDropsForThread: (threadRef) => {
|
||||
set((state) => ({
|
||||
pending: state.pending.filter((drop) => !isSameSidebarThreadRef(drop.threadRef, threadRef)),
|
||||
}));
|
||||
},
|
||||
consumePendingFileDrop: (threadRef) => {
|
||||
const matches = get().pending.filter((drop) =>
|
||||
isSameSidebarThreadRef(drop.threadRef, threadRef),
|
||||
);
|
||||
if (matches.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const matchedIds = new Set(matches.map((drop) => drop.id));
|
||||
set((state) => ({ pending: state.pending.filter((drop) => !matchedIds.has(drop.id)) }));
|
||||
return matches.flatMap((drop) => drop.files);
|
||||
},
|
||||
}),
|
||||
);
|
||||
@@ -24,6 +24,11 @@ worktree**, each background submission creates its own worktree.
|
||||
|
||||
Pin a thread from its menu to keep it above your active work.
|
||||
|
||||
On web and desktop, you can also drag files from your computer onto any thread row:
|
||||
the thread opens and the files are attached in its composer, ready for
|
||||
your next message. The same per-message file limits apply as when attaching
|
||||
files directly; see [Attach files](./composer.md#attach-files).
|
||||
|
||||
Pinning does not prevent automatic settlement. Settling a thread removes its pin.
|
||||
|
||||
On web and desktop, drag a thread between sections to change its state. Drag a thread up into
|
||||
|
||||
Reference in new issue
Block a user