From 7819a2034ede13f1b081e1e253dc2ccc8a004f8e Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 18 Jun 2026 15:53:18 +0800 Subject: [PATCH] fix(webui): clear external drag state synchronously on folder drops --- .../components/file-browser/file-browser.tsx | 72 ++++++++++--------- .../components/file-browser/folder-card.tsx | 4 +- .../components/file-browser/list-view.tsx | 7 +- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/packages/webui/components/file-browser/file-browser.tsx b/packages/webui/components/file-browser/file-browser.tsx index e0b50a9d..a85cc2e1 100644 --- a/packages/webui/components/file-browser/file-browser.tsx +++ b/packages/webui/components/file-browser/file-browser.tsx @@ -2,46 +2,49 @@ import { client } from '@/ui/api/client' import type { InferRequestType, InferResponseType } from 'hono/client' -import type { AssetInfo } from '@shumai/dtos' -import type { CollectionInfo } from '@shumai/dtos' -import type { SearchCondition, SearchSort } from '@shumai/dtos' -import type { ShareLinkInfo } from '@shumai/dtos' -import type { CreateUploadTaskRequest } from '@shumai/dtos' -import { formatSize } from '@/ui/lib/format' -import { ulid } from 'ulid' import { usePermissions } from '@/ui/hooks/use-permissions' +import { getAllFilesFromEntries } from '@/ui/lib/dnd-utils' +import { formatSize } from '@/ui/lib/format' import { useFieldStore } from '@/ui/stores/fields' import { useUploadStore } from '@/ui/stores/upload' +import type { + AssetInfo, + CollectionInfo, + CreateUploadTaskRequest, + SearchCondition, + SearchSort, + ShareLinkInfo, +} from '@shumai/dtos' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' -import { Download, AlertTriangle, Loader2, UploadCloud } from 'lucide-react' +import { AlertTriangle, Download, Loader2 } from 'lucide-react' import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' -import { getAllFilesFromEntries } from '@/ui/lib/dnd-utils' import { toast } from 'sonner' +import { ulid } from 'ulid' import type { DragState } from '../dnd-types' import { MoveCopyDialog } from '../move-copy-dialog' import { FileBrowserContextMenu } from './context-menu' import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, } from '../ui/alert-dialog' -import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle, -} from '../ui/dialog' import { Button } from '../ui/button' import { ContextMenu, ContextMenuTrigger } from '../ui/context-menu' +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from '../ui/dialog' import { FileCard } from './file-card' import { FileListItem } from './file-list-item' import { FolderCard } from './folder-card' @@ -143,6 +146,12 @@ export function FileBrowser({ const [externalOverFolderId, setExternalOverFolderId] = useState(null) const dragCounter = useRef(0) + const resetExternalDragState = useCallback(() => { + setIsExternalDragging(false) + setExternalOverFolderId(null) + dragCounter.current = 0 + }, []) + // Prevent default window behavior to avoid browser replacing page on accidental drops useEffect(() => { const preventDefault = (e: DragEvent) => { @@ -190,9 +199,7 @@ export function FileBrowser({ const files = await getAllFilesFromEntries(e.dataTransfer) - setIsExternalDragging(false) - setExternalOverFolderId(null) - dragCounter.current = 0 + resetExternalDragState() if (files.length > 0) { processAndUploadFiles(files) @@ -614,6 +621,7 @@ export function FileBrowser({ isExternalDragging, externalOverFolderId, setExternalOverFolderId, + resetExternalDragState, onExternalDrop: (files: File[], folderId: string) => { processAndUploadFiles(files, folderId) }, @@ -960,6 +968,7 @@ export function FileBrowser({ isExternalDragging={isExternalDragging} externalOverFolderId={externalOverFolderId} setExternalOverFolderId={setExternalOverFolderId} + resetExternalDragState={resetExternalDragState} onExternalDrop={(files, folderId) => processAndUploadFiles(files, folderId)} /> ) : ( @@ -1047,11 +1056,8 @@ export function FileBrowser({ )} {isExternalDragging && !externalOverFolderId && (
-
- - - Drop here to upload files to the current folder - +
+ Drop files here to upload
)} diff --git a/packages/webui/components/file-browser/folder-card.tsx b/packages/webui/components/file-browser/folder-card.tsx index d6e6372a..73898a3c 100644 --- a/packages/webui/components/file-browser/folder-card.tsx +++ b/packages/webui/components/file-browser/folder-card.tsx @@ -42,6 +42,7 @@ interface FolderCardProps { isExternalDragging?: boolean externalOverFolderId?: string | null setExternalOverFolderId?: (id: string | null) => void + resetExternalDragState?: () => void onExternalDrop?: (files: File[], folderId: string) => void } @@ -122,6 +123,7 @@ export function FolderCard({ isExternalDragging, externalOverFolderId, setExternalOverFolderId, + resetExternalDragState, onExternalDrop, }: FolderCardProps) { const [name, setName] = useState(item.name || '') @@ -262,7 +264,7 @@ export function FolderCard({ ? async (e) => { e.preventDefault() e.stopPropagation() - setExternalOverFolderId?.(null) + resetExternalDragState?.() const files = await getAllFilesFromEntries(e.dataTransfer) onExternalDrop?.(files, item.id!) } diff --git a/packages/webui/components/file-browser/list-view.tsx b/packages/webui/components/file-browser/list-view.tsx index 23d2b588..5b2f37c4 100644 --- a/packages/webui/components/file-browser/list-view.tsx +++ b/packages/webui/components/file-browser/list-view.tsx @@ -27,6 +27,7 @@ interface ListRowProps { isExternalDragging?: boolean externalOverFolderId?: string | null setExternalOverFolderId?: (id: string | null) => void + resetExternalDragState?: () => void onExternalDrop?: (files: File[], folderId: string) => void } @@ -42,6 +43,7 @@ function ListRow({ isExternalDragging, externalOverFolderId, setExternalOverFolderId, + resetExternalDragState, onExternalDrop, }: ListRowProps) { const isDraggingItem = dragState?.draggedIds.has(item.id!) @@ -130,7 +132,7 @@ function ListRow({ ? async (e) => { e.preventDefault() e.stopPropagation() - setExternalOverFolderId?.(null) + resetExternalDragState?.() const files = await getAllFilesFromEntries(e.dataTransfer) onExternalDrop?.(files, item.id!) } @@ -193,6 +195,7 @@ interface FileBrowserListViewProps { isExternalDragging?: boolean externalOverFolderId?: string | null setExternalOverFolderId?: (id: string | null) => void + resetExternalDragState?: () => void onExternalDrop?: (files: File[], folderId: string) => void } @@ -225,6 +228,7 @@ export function FileBrowserListView({ isExternalDragging, externalOverFolderId, setExternalOverFolderId, + resetExternalDragState, onExternalDrop, }: FileBrowserListViewProps) { const [columnSizing, setColumnSizing] = useState>({}) @@ -514,6 +518,7 @@ export function FileBrowserListView({ isExternalDragging={isExternalDragging} externalOverFolderId={externalOverFolderId} setExternalOverFolderId={setExternalOverFolderId} + resetExternalDragState={resetExternalDragState} onExternalDrop={onExternalDrop} /> )