|
1 | 1 | import { settingsScopeEnvironmentLabels } from "./settingsScopeAxis"; |
2 | 2 | import { Spinner } from "~/components/ui/spinner"; |
3 | | -import { ArchiveIcon, ArchiveX } from "lucide-react"; |
4 | | -import { type ScopedThreadRef } from "@t3tools/contracts"; |
5 | | -import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | +import { ArchiveIcon, ArchiveX, Trash2Icon } from "lucide-react"; |
| 4 | +import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; |
6 | 5 | import { presentThreadShell } from "@t3tools/client-runtime/state/shell"; |
7 | 6 | import { |
| 7 | + type AtomCommandResult, |
8 | 8 | isAtomCommandInterrupted, |
9 | 9 | settlePromise, |
10 | 10 | squashAtomCommandFailure, |
@@ -20,9 +20,20 @@ import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsL |
20 | 20 | import { searchableSetting } from "./settingsSearch"; |
21 | 21 | import { ProjectFavicon } from "../ProjectFavicon"; |
22 | 22 |
|
| 23 | +import { useRef, useState } from "react"; |
| 24 | +import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip"; |
| 25 | + |
| 26 | +const ARCHIVE_ACTIONS = [ |
| 27 | + { action: "unarchive", label: "Unarchive", Icon: ArchiveX }, |
| 28 | + { action: "delete", label: "Delete", Icon: Trash2Icon }, |
| 29 | +] as const; |
| 30 | +type ArchiveAction = (typeof ARCHIVE_ACTIONS)[number]["action"]; |
| 31 | + |
23 | 32 | export function ArchivedThreadsPanel() { |
24 | 33 | const { scope, groups: projectGroups, environments } = useSettingsScope(); |
25 | 34 | const { unarchiveThread, confirmAndDeleteThread } = useThreadActions(); |
| 35 | + const pendingActionsRef = useRef(new Set<string>()); |
| 36 | + const [pendingKeys, setPendingKeys] = useState<ReadonlySet<string>>(() => new Set()); |
26 | 37 | const { |
27 | 38 | snapshots: archivedSnapshots, |
28 | 39 | error: archiveError, |
@@ -122,52 +133,57 @@ export function ArchivedThreadsPanel() { |
122 | 133 | return { groups: sortedGroups, hasMultipleEnvironments }; |
123 | 134 | })(); |
124 | 135 |
|
125 | | - const handleArchivedThreadContextMenu = async ( |
126 | | - threadRef: ScopedThreadRef, |
127 | | - position: { x: number; y: number }, |
128 | | - ) => { |
129 | | - const api = readLocalApi(); |
130 | | - if (!api) return; |
131 | | - const clicked = await api.contextMenu.show( |
132 | | - [ |
133 | | - { id: "unarchive", label: "Unarchive" }, |
134 | | - { id: "delete", label: "Delete", destructive: true }, |
135 | | - ], |
136 | | - position, |
| 136 | + type ArchivedThread = (typeof archivedGroups)[number]["threads"][number]; |
| 137 | + |
| 138 | + const reportFailure = (result: AtomCommandResult<unknown, unknown> | null, title: string) => { |
| 139 | + if (result?._tag !== "Failure" || isAtomCommandInterrupted(result)) return; |
| 140 | + const error = squashAtomCommandFailure(result); |
| 141 | + toastManager.add( |
| 142 | + stackedThreadToast({ |
| 143 | + type: "error", |
| 144 | + title, |
| 145 | + description: error instanceof Error ? error.message : "An error occurred.", |
| 146 | + }), |
137 | 147 | ); |
| 148 | + }; |
138 | 149 |
|
139 | | - if (clicked === "unarchive") { |
140 | | - const result = await unarchiveThread(threadRef); |
141 | | - if (result._tag === "Success") { |
142 | | - refreshArchivedThreads(); |
143 | | - } else if (!isAtomCommandInterrupted(result)) { |
144 | | - const error = squashAtomCommandFailure(result); |
145 | | - toastManager.add( |
146 | | - stackedThreadToast({ |
147 | | - type: "error", |
148 | | - title: "Failed to unarchive thread", |
149 | | - description: error instanceof Error ? error.message : "An error occurred.", |
150 | | - }), |
151 | | - ); |
152 | | - } |
153 | | - return; |
154 | | - } |
| 150 | + const handleThreadAction = async (action: ArchiveAction, thread: ArchivedThread) => { |
| 151 | + const target = scopeThreadRef(thread.environmentId, thread.id); |
| 152 | + const key = scopedThreadKey(target); |
| 153 | + // Context menus can outlive their render, so checks and cleanup use the live set. |
| 154 | + const pending = pendingActionsRef.current; |
| 155 | + if (pending.has(key)) return; |
| 156 | + pending.add(key); |
| 157 | + setPendingKeys(new Set(pending)); |
| 158 | + const result = await settlePromise(async () => { |
| 159 | + const result = await (action === "unarchive" |
| 160 | + ? unarchiveThread(target) |
| 161 | + : confirmAndDeleteThread(target, thread.title)); |
| 162 | + if (result._tag === "Success") refreshArchivedThreads(); |
| 163 | + return result; |
| 164 | + }).finally(() => { |
| 165 | + pending.delete(key); |
| 166 | + setPendingKeys(new Set(pending)); |
| 167 | + }); |
| 168 | + reportFailure(result._tag === "Failure" ? result : result.value, `Failed to ${action} thread`); |
| 169 | + }; |
155 | 170 |
|
156 | | - if (clicked === "delete") { |
157 | | - const result = await confirmAndDeleteThread(threadRef); |
158 | | - if (result._tag === "Success") { |
159 | | - refreshArchivedThreads(); |
160 | | - } else if (!isAtomCommandInterrupted(result)) { |
161 | | - const error = squashAtomCommandFailure(result); |
162 | | - toastManager.add( |
163 | | - stackedThreadToast({ |
164 | | - type: "error", |
165 | | - title: "Failed to delete thread", |
166 | | - description: error instanceof Error ? error.message : "An error occurred.", |
167 | | - }), |
168 | | - ); |
169 | | - } |
170 | | - } |
| 171 | + const handleContextMenu = async (thread: ArchivedThread, position: { x: number; y: number }) => { |
| 172 | + const pending = pendingActionsRef.current; |
| 173 | + if (pending.has(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)))) return; |
| 174 | + const result = await settlePromise(async () => { |
| 175 | + const clicked = await readLocalApi()?.contextMenu.show( |
| 176 | + ARCHIVE_ACTIONS.map(({ action, label }) => ({ |
| 177 | + id: action, |
| 178 | + label, |
| 179 | + destructive: action === "delete", |
| 180 | + })), |
| 181 | + position, |
| 182 | + ); |
| 183 | + if (clicked === "unarchive" || clicked === "delete") |
| 184 | + await handleThreadAction(clicked, thread); |
| 185 | + }); |
| 186 | + reportFailure(result, "Archived thread action failed"); |
171 | 187 | }; |
172 | 188 |
|
173 | 189 | return ( |
@@ -207,78 +223,50 @@ export function ArchivedThreadsPanel() { |
207 | 223 | title={title} |
208 | 224 | icon={<ProjectFavicon project={project} />} |
209 | 225 | > |
210 | | - {projectThreads.map((thread) => ( |
211 | | - <SettingsRow |
212 | | - key={`${thread.environmentId}:${thread.id}`} |
213 | | - onContextMenu={(event) => { |
214 | | - event.preventDefault(); |
215 | | - void (async () => { |
216 | | - const result = await settlePromise(() => |
217 | | - handleArchivedThreadContextMenu( |
218 | | - scopeThreadRef(thread.environmentId, thread.id), |
219 | | - { |
220 | | - x: event.clientX, |
221 | | - y: event.clientY, |
222 | | - }, |
223 | | - ), |
224 | | - ); |
225 | | - if (result._tag === "Failure") { |
226 | | - const error = squashAtomCommandFailure(result); |
227 | | - toastManager.add( |
228 | | - stackedThreadToast({ |
229 | | - type: "error", |
230 | | - title: "Archived thread action failed", |
231 | | - description: |
232 | | - error instanceof Error ? error.message : "An error occurred.", |
233 | | - }), |
234 | | - ); |
235 | | - } |
236 | | - })(); |
237 | | - }} |
238 | | - title={thread.title} |
239 | | - description={ |
240 | | - <span className={hasMultipleEnvironments ? "block truncate" : undefined}> |
241 | | - {hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null} |
242 | | - Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} /> |
243 | | - {" \u00b7 Created "} |
244 | | - <RelativeTime timestamp={thread.createdAt} /> |
245 | | - </span> |
246 | | - } |
247 | | - control={ |
248 | | - <Button |
249 | | - type="button" |
250 | | - variant="outline" |
251 | | - size="sm" |
252 | | - className="shrink-0" |
253 | | - onClick={() => { |
254 | | - void (async () => { |
255 | | - const result = await unarchiveThread( |
256 | | - scopeThreadRef(thread.environmentId, thread.id), |
257 | | - ); |
258 | | - if (result._tag === "Success") { |
259 | | - refreshArchivedThreads(); |
260 | | - return; |
261 | | - } |
262 | | - if (!isAtomCommandInterrupted(result)) { |
263 | | - const error = squashAtomCommandFailure(result); |
264 | | - toastManager.add( |
265 | | - stackedThreadToast({ |
266 | | - type: "error", |
267 | | - title: "Failed to unarchive thread", |
268 | | - description: |
269 | | - error instanceof Error ? error.message : "An error occurred.", |
270 | | - }), |
271 | | - ); |
272 | | - } |
273 | | - })(); |
274 | | - }} |
275 | | - > |
276 | | - <ArchiveX className="size-3.5" /> |
277 | | - <span>Unarchive</span> |
278 | | - </Button> |
279 | | - } |
280 | | - /> |
281 | | - ))} |
| 226 | + {projectThreads.map((thread) => { |
| 227 | + const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); |
| 228 | + return ( |
| 229 | + <SettingsRow |
| 230 | + key={`${thread.environmentId}:${thread.id}`} |
| 231 | + onContextMenu={(event) => { |
| 232 | + event.preventDefault(); |
| 233 | + void handleContextMenu(thread, { x: event.clientX, y: event.clientY }); |
| 234 | + }} |
| 235 | + title={thread.title} |
| 236 | + description={ |
| 237 | + <span className={hasMultipleEnvironments ? "block truncate" : undefined}> |
| 238 | + {hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null} |
| 239 | + Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} /> |
| 240 | + {" \u00b7 Created "} |
| 241 | + <RelativeTime timestamp={thread.createdAt} /> |
| 242 | + </span> |
| 243 | + } |
| 244 | + control={ |
| 245 | + <div className="flex shrink-0 items-center gap-2"> |
| 246 | + {ARCHIVE_ACTIONS.map(({ action, label, Icon }) => ( |
| 247 | + <Tooltip key={action}> |
| 248 | + <TooltipTrigger |
| 249 | + render={ |
| 250 | + <Button |
| 251 | + type="button" |
| 252 | + variant={action === "delete" ? "destructive-outline" : "outline"} |
| 253 | + size="icon-sm" |
| 254 | + aria-label={`${label} ${thread.title}`} |
| 255 | + disabled={pendingKeys.has(threadKey)} |
| 256 | + onClick={() => void handleThreadAction(action, thread)} |
| 257 | + /> |
| 258 | + } |
| 259 | + > |
| 260 | + <Icon className="size-3.5" /> |
| 261 | + </TooltipTrigger> |
| 262 | + <TooltipPopup>{label}</TooltipPopup> |
| 263 | + </Tooltip> |
| 264 | + ))} |
| 265 | + </div> |
| 266 | + } |
| 267 | + /> |
| 268 | + ); |
| 269 | + })} |
282 | 270 | </SettingsSection> |
283 | 271 | )) |
284 | 272 | )} |
|