Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useEffect } from "react";
3
4import { undoLatestThreadAction, useThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
5import { shortcutLabelForCommand } from "../../keybindings";
6import { announce, cancelAnnouncement } from "../../lib/announce";
7import { primaryServerKeybindingsAtom } from "../../state/server";
8import { Alert, AlertDescription } from "../ui/alert";
9import { InlineButton } from "../ui/button";
10
11export function SidebarThreadUndoNotice() {
12 const notice = useThreadUndoNotice((state) => state.notice);
13 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
14
15 const shortcut = shortcutLabelForCommand(keybindings, "thread.undo");
16 const undoLabel = shortcut ? `${shortcut} to undo` : "Undo";
17 const summary = notice
18 ? `${notice.action} ${notice.count} ${notice.action === "Discarded" ? "draft" : "thread"}${notice.count === 1 ? "" : "s"}`
19 : null;
20
21 // A live region that mounts already filled is skipped by screen readers, and settling
22 // usually moves focus to the next thread at the same moment.
23 useEffect(() => {
24 if (summary) announce(`${summary}, ${undoLabel}`, "thread-undo-notice");
25 else cancelAnnouncement("thread-undo-notice");
26 }, [summary, undoLabel]);
27
28 if (!summary) return null;
29
30 return (
31 <Alert role="group" variant="sidebar">
32 <AlertDescription>
33 {summary}, <InlineButton onClick={undoLatestThreadAction}>{undoLabel}</InlineButton>
34 </AlertDescription>
35 </Alert>
36 );
37}