|
1 | 1 | import { SettingsGroup } from "./SettingsGroup"; |
2 | 2 | import { Spinner } from "~/components/ui/spinner"; |
3 | 3 | import { NotificationSettings } from "./NotificationSettings"; |
4 | | -import { ArchiveIcon, ArchiveX, CheckIcon, ChevronRightIcon, SettingsIcon } from "lucide-react"; |
| 4 | +import { |
| 5 | + ArchiveIcon, |
| 6 | + ArchiveX, |
| 7 | + CheckIcon, |
| 8 | + ChevronRightIcon, |
| 9 | + EllipsisIcon, |
| 10 | + SettingsIcon, |
| 11 | + Trash2Icon, |
| 12 | +} from "lucide-react"; |
5 | 13 | import { Link, useNavigate } from "@tanstack/react-router"; |
6 | 14 | import type { CSSProperties, ReactNode } from "react"; |
7 | 15 | import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
8 | 16 | import { |
9 | 17 | type BackgroundActivityProfile, |
10 | 18 | type DesktopUpdateChannel, |
| 19 | + type EnvironmentId, |
11 | 20 | ProviderDriverKind, |
12 | 21 | type ProviderInstanceId, |
13 | 22 | type ScopedThreadRef, |
14 | 23 | type SidebarProjectGroupingMode, |
| 24 | + type ThreadId, |
15 | 25 | } from "@t3tools/contracts"; |
16 | 26 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
17 | 27 | import { |
@@ -107,6 +117,7 @@ import { |
107 | 117 | AlertDialogTitle, |
108 | 118 | } from "../ui/alert-dialog"; |
109 | 119 | import { Button } from "../ui/button"; |
| 120 | +import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
110 | 121 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
111 | 122 | import { |
112 | 123 | Dialog, |
@@ -3281,7 +3292,9 @@ export function GeneralSettingsPanel() { |
3281 | 3292 |
|
3282 | 3293 | export function ArchivedThreadsPanel() { |
3283 | 3294 | const { scope } = useSettingsScope(); |
3284 | | - const { unarchiveThread, confirmAndDeleteThread } = useThreadActions(); |
| 3295 | + const { unarchiveThread, unarchiveThreads, confirmAndDeleteThread, deleteArchivedThreads } = |
| 3296 | + useThreadActions(); |
| 3297 | + const [isBulkActionPending, setIsBulkActionPending] = useState(false); |
3285 | 3298 | const { |
3286 | 3299 | snapshots: archivedSnapshots, |
3287 | 3300 | error: archiveError, |
@@ -3388,8 +3401,98 @@ export function ArchivedThreadsPanel() { |
3388 | 3401 | [confirmAndDeleteThread, refreshArchivedThreads, unarchiveThread], |
3389 | 3402 | ); |
3390 | 3403 |
|
| 3404 | + const archivedThreadCount = useMemo( |
| 3405 | + () => archivedGroups.reduce((count, group) => count + group.threads.length, 0), |
| 3406 | + [archivedGroups], |
| 3407 | + ); |
| 3408 | + |
| 3409 | + /** Unarchives or deletes every listed thread; `scopeLabel` names the set in the delete prompt. */ |
| 3410 | + const handleBulkArchivedThreadAction = useCallback( |
| 3411 | + async ( |
| 3412 | + action: "unarchive" | "delete", |
| 3413 | + threads: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ThreadId }>, |
| 3414 | + scopeLabel: string, |
| 3415 | + ) => { |
| 3416 | + if (threads.length === 0) return; |
| 3417 | + if (action === "delete") { |
| 3418 | + const api = readLocalApi(); |
| 3419 | + if (!api) return; |
| 3420 | + // Always confirm: one click here can erase many threads at once. |
| 3421 | + const confirmed = await settlePromise(() => |
| 3422 | + api.dialogs.confirm( |
| 3423 | + [ |
| 3424 | + `Delete ${threads.length} archived thread${threads.length === 1 ? "" : "s"}${scopeLabel}?`, |
| 3425 | + "This permanently clears conversation history for these threads.", |
| 3426 | + ].join("\n"), |
| 3427 | + { variant: "destructive" }, |
| 3428 | + ), |
| 3429 | + ); |
| 3430 | + if (confirmed._tag === "Failure" || !confirmed.value) return; |
| 3431 | + } |
| 3432 | + setIsBulkActionPending(true); |
| 3433 | + const targets = threads.map((thread) => scopeThreadRef(thread.environmentId, thread.id)); |
| 3434 | + const failure = await (action === "unarchive" |
| 3435 | + ? unarchiveThreads(targets) |
| 3436 | + : deleteArchivedThreads(targets)); |
| 3437 | + setIsBulkActionPending(false); |
| 3438 | + if (failure !== null) { |
| 3439 | + const error = squashAtomCommandFailure(failure); |
| 3440 | + toastManager.add( |
| 3441 | + stackedThreadToast({ |
| 3442 | + type: "error", |
| 3443 | + title: |
| 3444 | + action === "unarchive" ? "Failed to unarchive threads" : "Failed to delete threads", |
| 3445 | + description: error instanceof Error ? error.message : "An error occurred.", |
| 3446 | + }), |
| 3447 | + ); |
| 3448 | + } |
| 3449 | + }, |
| 3450 | + [deleteArchivedThreads, unarchiveThreads], |
| 3451 | + ); |
| 3452 | + |
3391 | 3453 | return ( |
3392 | 3454 | <SettingsPageContainer> |
| 3455 | + {archivedGroups.length > 0 ? ( |
| 3456 | + <div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2 px-3 sm:px-4"> |
| 3457 | + <p className="text-sm text-muted-foreground"> |
| 3458 | + {archivedThreadCount} archived thread{archivedThreadCount === 1 ? "" : "s"} |
| 3459 | + </p> |
| 3460 | + <div className="flex items-center gap-1.5"> |
| 3461 | + <Button |
| 3462 | + type="button" |
| 3463 | + variant="outline" |
| 3464 | + size="xs" |
| 3465 | + disabled={isBulkActionPending} |
| 3466 | + onClick={() => { |
| 3467 | + void handleBulkArchivedThreadAction( |
| 3468 | + "unarchive", |
| 3469 | + archivedGroups.flatMap((group) => group.threads), |
| 3470 | + "", |
| 3471 | + ); |
| 3472 | + }} |
| 3473 | + > |
| 3474 | + <ArchiveX className="size-3.5" /> |
| 3475 | + <span>Unarchive all</span> |
| 3476 | + </Button> |
| 3477 | + <Button |
| 3478 | + type="button" |
| 3479 | + variant="destructive-outline" |
| 3480 | + size="xs" |
| 3481 | + disabled={isBulkActionPending} |
| 3482 | + onClick={() => { |
| 3483 | + void handleBulkArchivedThreadAction( |
| 3484 | + "delete", |
| 3485 | + archivedGroups.flatMap((group) => group.threads), |
| 3486 | + archivedGroups.length === 1 ? ` in ${archivedGroups[0]!.project.title}` : "", |
| 3487 | + ); |
| 3488 | + }} |
| 3489 | + > |
| 3490 | + <Trash2Icon className="size-3.5" /> |
| 3491 | + <span>Delete all</span> |
| 3492 | + </Button> |
| 3493 | + </div> |
| 3494 | + </div> |
| 3495 | + ) : null} |
3393 | 3496 | {archivedGroups.length === 0 ? ( |
3394 | 3497 | <SettingsSection |
3395 | 3498 | id={isLoadingArchive ? undefined : searchableSetting("archive").id} |
@@ -3424,6 +3527,46 @@ export function ArchivedThreadsPanel() { |
3424 | 3527 | id={index === 0 ? searchableSetting("archive").id : undefined} |
3425 | 3528 | title={project.title} |
3426 | 3529 | icon={<ProjectFavicon project={project} />} |
| 3530 | + headerAction={ |
| 3531 | + <Menu> |
| 3532 | + <MenuTrigger |
| 3533 | + render={ |
| 3534 | + <Button |
| 3535 | + type="button" |
| 3536 | + variant="ghost-muted" |
| 3537 | + size="icon-xs" |
| 3538 | + disabled={isBulkActionPending} |
| 3539 | + aria-label={`Archived thread actions for ${project.title}`} |
| 3540 | + /> |
| 3541 | + } |
| 3542 | + > |
| 3543 | + <EllipsisIcon className="size-3.5" /> |
| 3544 | + </MenuTrigger> |
| 3545 | + <MenuPopup align="end"> |
| 3546 | + <MenuItem |
| 3547 | + onClick={() => { |
| 3548 | + void handleBulkArchivedThreadAction("unarchive", projectThreads, ""); |
| 3549 | + }} |
| 3550 | + > |
| 3551 | + <ArchiveX /> |
| 3552 | + Unarchive all in project |
| 3553 | + </MenuItem> |
| 3554 | + <MenuItem |
| 3555 | + variant="destructive" |
| 3556 | + onClick={() => { |
| 3557 | + void handleBulkArchivedThreadAction( |
| 3558 | + "delete", |
| 3559 | + projectThreads, |
| 3560 | + ` in ${project.title}`, |
| 3561 | + ); |
| 3562 | + }} |
| 3563 | + > |
| 3564 | + <Trash2Icon /> |
| 3565 | + Delete all in project |
| 3566 | + </MenuItem> |
| 3567 | + </MenuPopup> |
| 3568 | + </Menu> |
| 3569 | + } |
3427 | 3570 | > |
3428 | 3571 | {projectThreads.map((thread) => ( |
3429 | 3572 | <SettingsRow |
|
0 commit comments