feat(webui): add compact batch archive actions

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-27 09:36:22 +08:00
1 parent 23c4acc216
commit d1451e30e9
19 files changed
+442 -82

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
import { Archive, ArchiveRestore } from "lucide-react";
import { useTranslation } from "react-i18next";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
export function ArchiveConfirm({
count,
archive,
onCancel,
onConfirm,
}: {
count: number;
archive: boolean;
onCancel: () => void;
onConfirm: () => void;
}) {
const { t } = useTranslation();
const Icon = archive ? Archive : ArchiveRestore;
const titleKey = archive
? count > 1 ? "archiveConfirm.titleMany" : "archiveConfirm.title"
: count > 1 ? "archiveConfirm.unarchiveTitleMany" : "archiveConfirm.unarchiveTitle";
return (
<AlertDialog open onOpenChange={(open) => { if (!open) onCancel(); }}>
<AlertDialogContent
className="w-[min(calc(100vw-2rem),24rem)] gap-0 p-5 text-center"
onEscapeKeyDown={(event) => event.stopPropagation()}
>
<AlertDialogHeader className="items-center space-y-0 text-center">
<Icon className="mb-4 h-6 w-6 text-muted-foreground" strokeWidth={1.8} aria-hidden />
<AlertDialogTitle className="text-center text-[20px] font-semibold leading-tight tracking-[-0.02em] text-foreground">
{t(titleKey, { count })}
</AlertDialogTitle>
<AlertDialogDescription className="mt-3 max-w-[17rem] text-center text-[14px] leading-6 text-muted-foreground">
{t(archive ? "archiveConfirm.description" : "archiveConfirm.unarchiveDescription")}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter className="mt-6 !grid grid-cols-1 gap-2 sm:grid-cols-2">
<AlertDialogCancel onClick={onCancel} className="mt-0 w-full min-w-0">
{t("common.cancel")}
</AlertDialogCancel>
<AlertDialogAction
onClick={onConfirm}
className="w-full min-w-0 !whitespace-normal bg-foreground text-center text-background hover:bg-foreground/90"
>
{t(archive ? "chat.archive" : "chat.unarchive")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
+95 -41
View File
@@ -48,6 +48,7 @@ import {
} from "@/components/ui/tooltip";
import { MAX_WORKBENCH_PANES } from "@/components/workbench/workbench-model";
import { SidebarTopicRename } from "@/components/SidebarTopicRename";
import { ArchiveConfirm } from "@/components/ArchiveConfirm";
import { SkeletonStatus } from "@/components/settings/shared/SkeletonStatus";
import { LoadingRing } from "@/components/ui/loading-ring";
import {
@@ -189,6 +190,8 @@ export interface SidebarDeleteItem {
label: string;
}
export type SidebarDeleteDecision = (confirmed: boolean) => void;
function droppablePaneKey(
dataTransfer: DataTransfer,
group: SidebarPaneGroup,
@@ -225,12 +228,13 @@ interface ChatListProps {
activeKey: string | null;
onSelect: (key: string) => void;
onCloseTemporaryChat?: (key: string) => void;
onRequestDelete: (key: string, label: string) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[]) => void;
onRequestDelete: (key: string, label: string, onDecision?: SidebarDeleteDecision) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[], onDecision?: SidebarDeleteDecision) => void;
onTogglePin: (key: string) => void;
onRequestRename: (key: string, title: string) => Promise<void>;
onRequestRenameTab?: (key: string, label: string) => void;
onToggleArchive: (key: string) => void;
onArchiveMany?: (keys: string[], archive: boolean) => void;
paneGroups?: Record<string, SidebarPaneGroup>;
onSelectPane?: (tabKey: string, paneKey: string) => void;
onCreateTab?: (paneKey: string) => void;
@@ -276,6 +280,7 @@ export const ChatList = memo(function ChatList({
onRequestRename,
onRequestRenameTab,
onToggleArchive,
onArchiveMany,
paneGroups = {},
onSelectPane,
onCreateTab,
@@ -308,6 +313,7 @@ export const ChatList = memo(function ChatList({
const { t } = useTranslation();
const [visibleLimit, setVisibleLimit] = useState(INITIAL_VISIBLE_SESSIONS);
const scrollViewportRef = useRef<HTMLDivElement>(null);
const selectionBarRef = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
const viewport = scrollViewportRef.current;
if (!viewport) return;
@@ -343,6 +349,11 @@ export const ChatList = memo(function ChatList({
);
const [paneDropTarget, setPaneDropTarget] = useState<string | null>(null);
const [deleteSelectionMode, setDeleteSelectionMode] = useState(false);
const [deleteConfirmationPending, setDeleteConfirmationPending] = useState(false);
const [pendingArchive, setPendingArchive] = useState<{
keys: string[];
archive: boolean;
} | null>(null);
const [openActionMenuId, setOpenActionMenuId] = useState<string | null>(null);
const [renamingTopic, setRenamingTopic] = useState<{ key: string } | null>(null);
const renamingKey = renamingTopic?.key ?? null;
@@ -364,6 +375,12 @@ export const ChatList = memo(function ChatList({
() => new Set(),
);
const deleteSelectionAnchorRef = useRef<string | null>(null);
const selectionConfirmationPending = pendingArchive !== null || deleteConfirmationPending;
const closeDeleteSelection = useCallback(() => {
setDeleteSelectionMode(false);
setSelectedDeleteKeys(new Set());
deleteSelectionAnchorRef.current = null;
}, []);
useEffect(() => {
const clearPaneDropTarget = () => setPaneDropTarget(null);
@@ -484,14 +501,26 @@ export const ChatList = memo(function ChatList({
if (!deleteSelectionMode) return;
setOpenActionMenuId(null);
const onKeyDown = (event: KeyboardEvent) => {
if (event.key !== "Escape") return;
setDeleteSelectionMode(false);
setSelectedDeleteKeys(new Set());
deleteSelectionAnchorRef.current = null;
if (event.key !== "Escape" || selectionConfirmationPending) return;
closeDeleteSelection();
};
const onClick = (event: MouseEvent) => {
if (selectionConfirmationPending) return;
const target = event.target;
if (target instanceof Node && selectionBarRef.current?.contains(target)) return;
const row = target instanceof Element
? target.closest("[data-chat-row], [data-workbench-tab]")
: null;
if (row && scrollViewportRef.current?.contains(row)) return;
closeDeleteSelection();
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [deleteSelectionMode]);
document.addEventListener("click", onClick, true);
return () => {
window.removeEventListener("keydown", onKeyDown);
document.removeEventListener("click", onClick, true);
};
}, [closeDeleteSelection, deleteSelectionMode, selectionConfirmationPending]);
useEffect(() => {
writeCollapsedPaneGroups(collapsedPaneGroups);
@@ -631,6 +660,7 @@ export const ChatList = memo(function ChatList({
shiftKey = false,
targetKey = keys[0],
) => {
if (selectionConfirmationPending) return;
const validKeys = keys.filter((key) => deleteItemsByKey.has(key));
const anchorKey = deleteSelectionAnchorRef.current;
const range = shiftKey && anchorKey && targetKey
@@ -652,25 +682,25 @@ export const ChatList = memo(function ChatList({
});
if (!range) deleteSelectionAnchorRef.current = targetKey ?? null;
};
const closeDeleteSelection = () => {
setDeleteSelectionMode(false);
setSelectedDeleteKeys(new Set());
deleteSelectionAnchorRef.current = null;
};
const requestDeleteItems = (items: SidebarDeleteItem[]) => {
const requestDeleteItems = (items: SidebarDeleteItem[], onDecision?: SidebarDeleteDecision) => {
if (items.length === 0) return;
if (onRequestDeleteMany) onRequestDeleteMany(items);
else if (items.length === 1) onRequestDelete(items[0].key, items[0].label);
if (onRequestDeleteMany) onRequestDeleteMany(items, onDecision);
else if (items.length === 1) onRequestDelete(items[0].key, items[0].label, onDecision);
};
const requestDeleteKeys = (keys: string[]) => {
const requestDeleteKeys = (keys: string[], onDecision?: SidebarDeleteDecision) => {
requestDeleteItems(keys
.map((key) => deleteItemsByKey.get(key))
.filter((item): item is SidebarDeleteItem => item !== undefined));
.filter((item): item is SidebarDeleteItem => item !== undefined), onDecision);
};
const confirmDeleteSelection = () => {
requestDeleteKeys(Array.from(selectedDeleteKeys));
closeDeleteSelection();
setDeleteConfirmationPending(true);
requestDeleteKeys(Array.from(selectedDeleteKeys), (confirmed) => {
setDeleteConfirmationPending(false);
if (confirmed) closeDeleteSelection();
});
};
const allSelectedArchived = selectedDeleteKeys.size > 0
&& Array.from(selectedDeleteKeys).every((key) => archivedPanes.has(key) || archived.has(key));
return (
<div ref={scrollViewportRef} className="sidebar-scroll-fade sidebar-scrollbar h-full min-h-0 min-w-0 overflow-x-hidden overflow-y-auto overscroll-contain pb-2">
<SidebarSelectionHighlight
@@ -1161,36 +1191,60 @@ export const ChatList = memo(function ChatList({
) : null}
{deleteSelectionMode ? (
<div
ref={selectionBarRef}
data-testid="delete-selection-bar"
className="sticky bottom-2 z-30 mx-1 mt-3 flex min-h-11 items-center gap-2 rounded-floating border border-sidebar-border/80 bg-popover/95 p-1.5 pl-2 shadow-[0_10px_30px_rgba(15,23,42,0.14)] backdrop-blur-xl"
aria-busy={deleteConfirmationPending}
className="sticky bottom-2 z-30 mx-1 mt-3 flex min-h-11 items-center gap-1 rounded-floating border border-sidebar-border/80 bg-popover/95 p-1.5 pl-3 shadow-[0_10px_30px_rgba(15,23,42,0.14)] backdrop-blur-xl"
>
<button
type="button"
onClick={closeDeleteSelection}
aria-label={t("chat.cancelSelection", {
defaultValue: "Cancel selection",
})}
className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-sidebar-muted-foreground transition-colors media-hover:hover:text-foreground"
>
<X className="h-4 w-4" aria-hidden />
</button>
<span className="min-w-0 flex-1 truncate px-1 text-[12.5px] font-medium text-foreground/85">
<span className="min-w-0 flex-1 truncate pr-2 text-[12.5px] font-medium text-foreground/85">
{t("chat.selectedCount", {
defaultValue: "{{count}} selected",
count: selectedDeleteKeys.size,
})}
</span>
<button
type="button"
disabled={selectedDeleteKeys.size === 0}
onClick={confirmDeleteSelection}
className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full bg-destructive px-3 text-[12px] font-semibold text-destructive-foreground transition-colors media-hover:hover:bg-destructive/90 disabled:pointer-events-none disabled:opacity-40"
>
<Trash2 className="h-3.5 w-3.5" aria-hidden />
{t("chat.deleteSelected", { defaultValue: "Delete" })}
</button>
{onArchiveMany ? (
<HoverHint content={t(allSelectedArchived ? "chat.unarchive" : "chat.archive")}>
<button
type="button"
aria-label={t(allSelectedArchived ? "chat.unarchive" : "chat.archive")}
disabled={selectedDeleteKeys.size === 0 || deleteConfirmationPending}
onClick={() => setPendingArchive({
keys: Array.from(selectedDeleteKeys),
archive: !allSelectedArchived,
})}
className="touch-target inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-foreground transition-colors media-hover:hover:bg-sidebar-accent disabled:pointer-events-none disabled:opacity-40"
>
{allSelectedArchived
? <ArchiveRestore className="h-4 w-4" aria-hidden />
: <Archive className="h-4 w-4" aria-hidden />}
</button>
</HoverHint>
) : null}
<HoverHint content={t("chat.delete")}>
<button
type="button"
aria-label={t("chat.delete")}
disabled={selectedDeleteKeys.size === 0 || deleteConfirmationPending}
onClick={confirmDeleteSelection}
className="touch-target inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-destructive transition-colors media-hover:hover:bg-destructive/10 disabled:pointer-events-none disabled:opacity-40"
>
<Trash2 className="h-4 w-4" aria-hidden />
</button>
</HoverHint>
</div>
) : null}
{pendingArchive ? (
<ArchiveConfirm
count={pendingArchive.keys.length}
archive={pendingArchive.archive}
onCancel={() => setPendingArchive(null)}
onConfirm={() => {
onArchiveMany?.(pendingArchive.keys, pendingArchive.archive);
setPendingArchive(null);
closeDeleteSelection();
}}
/>
) : null}
</SidebarSelectionHighlight>
</div>
);
+1
View File
@@ -42,6 +42,7 @@ export function DeleteConfirm({
<AlertDialog open={open} onOpenChange={(o) => (!o ? onCancel() : undefined)}>
<AlertDialogContent
className="w-[min(calc(100vw-2rem),24rem)] gap-0 p-5 text-center"
onEscapeKeyDown={(event) => event.stopPropagation()}
>
<AlertDialogHeader className="items-center space-y-0 text-center">
<Trash2
+5 -2
View File
@@ -20,6 +20,7 @@ import { useTranslation } from "react-i18next";
import {
ChatList,
type SidebarDeleteDecision,
type SidebarDeleteItem,
type SidebarPaneGroup,
} from "@/components/ChatList";
@@ -49,12 +50,13 @@ interface SidebarProps {
onNewChat: () => void;
onSelect: (key: string) => void;
onCloseTemporaryChat?: (key: string) => void;
onRequestDelete: (key: string, label: string) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[]) => void;
onRequestDelete: (key: string, label: string, onDecision?: SidebarDeleteDecision) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[], onDecision?: SidebarDeleteDecision) => void;
onTogglePin: (key: string) => void;
onRequestRename: (key: string, title: string) => Promise<void>;
onRequestRenameTab?: (key: string, label: string) => void;
onToggleArchive: (key: string) => void;
onArchiveMany?: (keys: string[], archive: boolean) => void;
paneGroups?: Record<string, SidebarPaneGroup>;
onSelectPane?: (tabKey: string, paneKey: string) => void;
onCreateTab?: (paneKey: string) => void;
@@ -289,6 +291,7 @@ export function Sidebar(props: SidebarProps) {
onRequestRename={props.onRequestRename}
onRequestRenameTab={props.onRequestRenameTab}
onToggleArchive={props.onToggleArchive}
onArchiveMany={props.onArchiveMany}
paneGroups={props.paneGroups}
onSelectPane={props.onSelectPane}
onCreateTab={props.onCreateTab}