Skip to content

Commit 6a1aa67

Browse files
committed
refactor(web): simplify archived thread controls
1 parent 46d5cf1 commit 6a1aa67

1 file changed

Lines changed: 103 additions & 153 deletions

File tree

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 103 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import {
2020
type DesktopUpdateChannel,
2121
ProviderDriverKind,
2222
type ProviderInstanceId,
23-
type ScopedThreadRef,
2423
type SidebarProjectGroupingMode,
2524
} from "@t3tools/contracts";
2625
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
2726
import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
2827
import {
28+
type AtomCommandResult,
2929
isAtomCommandInterrupted,
3030
settlePromise,
3131
squashAtomCommandFailure,
@@ -3359,10 +3359,18 @@ export function GeneralSettingsPanel() {
33593359
);
33603360
}
33613361

3362+
const ARCHIVE_ACTIONS = [
3363+
{ action: "unarchive", label: "Unarchive", Icon: ArchiveX },
3364+
{ action: "delete", label: "Delete", Icon: Trash2Icon },
3365+
] as const;
3366+
3367+
type ArchiveAction = (typeof ARCHIVE_ACTIONS)[number]["action"];
3368+
33623369
export function ArchivedThreadsPanel() {
33633370
const { scope, groups: projectGroups, environments } = useSettingsScope();
33643371
const { unarchiveThread, confirmAndDeleteThread } = useThreadActions();
3365-
const [pendingThreadKeys, setPendingThreadKeys] = useState<ReadonlySet<string>>(() => new Set());
3372+
const pendingActionsRef = useRef(new Set<string>());
3373+
const [pendingKeys, setPendingKeys] = useState<ReadonlySet<string>>(() => new Set());
33663374
const {
33673375
snapshots: archivedSnapshots,
33683376
error: archiveError,
@@ -3462,75 +3470,64 @@ export function ArchivedThreadsPanel() {
34623470
return { groups: sortedGroups, hasMultipleEnvironments };
34633471
})();
34643472

3465-
// A row stays locked until its action settles, so a repeated click can't queue
3466-
// a second confirmation or command for the same thread.
3467-
const withPendingThread = async (threadRef: ScopedThreadRef, action: () => Promise<void>) => {
3468-
const threadKey = scopedThreadKey(threadRef);
3469-
if (pendingThreadKeys.has(threadKey)) return;
3470-
setPendingThreadKeys((current) => new Set(current).add(threadKey));
3471-
await action().finally(() =>
3472-
setPendingThreadKeys((current) => {
3473-
const next = new Set(current);
3474-
next.delete(threadKey);
3475-
return next;
3473+
type ArchivedThread = (typeof archivedGroups)[number]["threads"][number];
3474+
3475+
const reportFailure = (result: AtomCommandResult<unknown, unknown> | null, title: string) => {
3476+
if (result?._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
3477+
const error = squashAtomCommandFailure(result);
3478+
toastManager.add(
3479+
stackedThreadToast({
3480+
type: "error",
3481+
title,
3482+
description: error instanceof Error ? error.message : "An error occurred.",
34763483
}),
34773484
);
34783485
};
34793486

3480-
const handleUnarchiveThread = (threadRef: ScopedThreadRef) =>
3481-
withPendingThread(threadRef, async () => {
3482-
const result = await unarchiveThread(threadRef);
3483-
if (result._tag === "Success") {
3484-
refreshArchivedThreads();
3485-
} else if (!isAtomCommandInterrupted(result)) {
3486-
const error = squashAtomCommandFailure(result);
3487-
toastManager.add(
3488-
stackedThreadToast({
3489-
type: "error",
3490-
title: "Failed to unarchive thread",
3491-
description: error instanceof Error ? error.message : "An error occurred.",
3492-
}),
3493-
);
3494-
}
3487+
const runAction = async (
3488+
key: string,
3489+
action: ArchiveAction,
3490+
run: () => Promise<AtomCommandResult<unknown, unknown> | null>,
3491+
) => {
3492+
// Context menus can outlive their render, so checks and cleanup use the live set.
3493+
const pending = pendingActionsRef.current;
3494+
if (pending.has(key)) return;
3495+
pending.add(key);
3496+
setPendingKeys(new Set(pending));
3497+
const result = await settlePromise(run).finally(() => {
3498+
pending.delete(key);
3499+
setPendingKeys(new Set(pending));
34953500
});
3501+
reportFailure(result._tag === "Failure" ? result : result.value, `Failed to ${action} thread`);
3502+
};
34963503

3497-
const handleDeleteThread = (threadRef: ScopedThreadRef, title: string) =>
3498-
withPendingThread(threadRef, async () => {
3499-
const result = await confirmAndDeleteThread(threadRef, title);
3500-
if (result._tag === "Success") {
3501-
refreshArchivedThreads();
3502-
} else if (!isAtomCommandInterrupted(result)) {
3503-
const error = squashAtomCommandFailure(result);
3504-
toastManager.add(
3505-
stackedThreadToast({
3506-
type: "error",
3507-
title: "Failed to delete thread",
3508-
description: error instanceof Error ? error.message : "An error occurred.",
3509-
}),
3510-
);
3511-
}
3504+
const handleThreadAction = (action: ArchiveAction, thread: ArchivedThread) => {
3505+
const target = scopeThreadRef(thread.environmentId, thread.id);
3506+
return runAction(scopedThreadKey(target), action, async () => {
3507+
const result = await (action === "unarchive"
3508+
? unarchiveThread(target)
3509+
: confirmAndDeleteThread(target, thread.title));
3510+
if (result._tag === "Success") refreshArchivedThreads();
3511+
return result;
35123512
});
3513+
};
35133514

3514-
const handleArchivedThreadContextMenu = async (
3515-
threadRef: ScopedThreadRef,
3516-
title: string,
3517-
position: { x: number; y: number },
3518-
) => {
3519-
const api = readLocalApi();
3520-
if (!api) return;
3521-
const clicked = await api.contextMenu.show(
3522-
[
3523-
{ id: "unarchive", label: "Unarchive" },
3524-
{ id: "delete", label: "Delete", destructive: true },
3525-
],
3526-
position,
3527-
);
3528-
3529-
if (clicked === "unarchive") {
3530-
await handleUnarchiveThread(threadRef);
3531-
} else if (clicked === "delete") {
3532-
await handleDeleteThread(threadRef, title);
3533-
}
3515+
const handleContextMenu = async (thread: ArchivedThread, position: { x: number; y: number }) => {
3516+
const pending = pendingActionsRef.current;
3517+
if (pending.has(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)))) return;
3518+
const result = await settlePromise(async () => {
3519+
const clicked = await readLocalApi()?.contextMenu.show(
3520+
ARCHIVE_ACTIONS.map(({ action, label }) => ({
3521+
id: action,
3522+
label,
3523+
destructive: action === "delete",
3524+
})),
3525+
position,
3526+
);
3527+
if (clicked === "unarchive" || clicked === "delete")
3528+
await handleThreadAction(clicked, thread);
3529+
});
3530+
reportFailure(result, "Archived thread action failed");
35343531
};
35353532

35363533
return (
@@ -3570,97 +3567,50 @@ export function ArchivedThreadsPanel() {
35703567
title={title}
35713568
icon={<ProjectFavicon project={project} />}
35723569
>
3573-
{projectThreads.map((thread) => (
3574-
<SettingsRow
3575-
key={`${thread.environmentId}:${thread.id}`}
3576-
onContextMenu={(event) => {
3577-
event.preventDefault();
3578-
void (async () => {
3579-
const result = await settlePromise(() =>
3580-
handleArchivedThreadContextMenu(
3581-
scopeThreadRef(thread.environmentId, thread.id),
3582-
thread.title,
3583-
{
3584-
x: event.clientX,
3585-
y: event.clientY,
3586-
},
3587-
),
3588-
);
3589-
if (result._tag === "Failure") {
3590-
const error = squashAtomCommandFailure(result);
3591-
toastManager.add(
3592-
stackedThreadToast({
3593-
type: "error",
3594-
title: "Archived thread action failed",
3595-
description:
3596-
error instanceof Error ? error.message : "An error occurred.",
3597-
}),
3598-
);
3599-
}
3600-
})();
3601-
}}
3602-
title={thread.title}
3603-
description={
3604-
<span className={hasMultipleEnvironments ? "block truncate" : undefined}>
3605-
{hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null}
3606-
Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} />
3607-
{" \u00b7 Created "}
3608-
<RelativeTime timestamp={thread.createdAt} />
3609-
</span>
3610-
}
3611-
control={
3612-
<div className="flex shrink-0 items-center gap-2">
3613-
<Tooltip>
3614-
<TooltipTrigger
3615-
render={
3616-
<Button
3617-
type="button"
3618-
variant="outline"
3619-
size="icon-sm"
3620-
aria-label={`Unarchive ${thread.title}`}
3621-
disabled={pendingThreadKeys.has(
3622-
scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
3623-
)}
3624-
onClick={() => {
3625-
void handleUnarchiveThread(
3626-
scopeThreadRef(thread.environmentId, thread.id),
3627-
);
3628-
}}
3629-
>
3630-
<ArchiveX className="size-3.5" />
3631-
</Button>
3632-
}
3633-
/>
3634-
<TooltipPopup>Unarchive</TooltipPopup>
3635-
</Tooltip>
3636-
<Tooltip>
3637-
<TooltipTrigger
3638-
render={
3639-
<Button
3640-
type="button"
3641-
variant="destructive-outline"
3642-
size="icon-sm"
3643-
aria-label={`Delete ${thread.title}`}
3644-
disabled={pendingThreadKeys.has(
3645-
scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
3646-
)}
3647-
onClick={() => {
3648-
void handleDeleteThread(
3649-
scopeThreadRef(thread.environmentId, thread.id),
3650-
thread.title,
3651-
);
3652-
}}
3570+
{projectThreads.map((thread) => {
3571+
const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
3572+
return (
3573+
<SettingsRow
3574+
key={`${thread.environmentId}:${thread.id}`}
3575+
onContextMenu={(event) => {
3576+
event.preventDefault();
3577+
void handleContextMenu(thread, { x: event.clientX, y: event.clientY });
3578+
}}
3579+
title={thread.title}
3580+
description={
3581+
<span className={hasMultipleEnvironments ? "block truncate" : undefined}>
3582+
{hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null}
3583+
Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} />
3584+
{" \u00b7 Created "}
3585+
<RelativeTime timestamp={thread.createdAt} />
3586+
</span>
3587+
}
3588+
control={
3589+
<div className="flex shrink-0 items-center gap-2">
3590+
{ARCHIVE_ACTIONS.map(({ action, label, Icon }) => (
3591+
<Tooltip key={action}>
3592+
<TooltipTrigger
3593+
render={
3594+
<Button
3595+
type="button"
3596+
variant={action === "delete" ? "destructive-outline" : "outline"}
3597+
size="icon-sm"
3598+
aria-label={`${label} ${thread.title}`}
3599+
disabled={pendingKeys.has(threadKey)}
3600+
onClick={() => void handleThreadAction(action, thread)}
3601+
/>
3602+
}
36533603
>
3654-
<Trash2Icon className="size-3.5" />
3655-
</Button>
3656-
}
3657-
/>
3658-
<TooltipPopup>Delete</TooltipPopup>
3659-
</Tooltip>
3660-
</div>
3661-
}
3662-
/>
3663-
))}
3604+
<Icon className="size-3.5" />
3605+
</TooltipTrigger>
3606+
<TooltipPopup>{label}</TooltipPopup>
3607+
</Tooltip>
3608+
))}
3609+
</div>
3610+
}
3611+
/>
3612+
);
3613+
})}
36643614
</SettingsSection>
36653615
))
36663616
)}

0 commit comments

Comments
 (0)