Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SidebarThreadUndoNotice.tsx

51 lines2.2 KB

1import { useAtomValue } from "@effect/atom-react";
2import { useState } from "react";
3​
4import {
5 threadUndoLabel,
6 undoLatestThreadAction,
7 useThreadUndoNotice,
8} from "../../hooks/showThreadUndoNotice";
9import { cn } from "../../lib/utils";
10import { primaryServerKeybindingsAtom } from "../../state/server";
11import { InlineButton } from "../ui/button";
12import { SidebarMenuItem } from "../ui/sidebar";
13​
14/** Fills the free space in the sidebar footer's utility row, so it never moves or covers the list. */
15export function SidebarThreadUndoNotice() {
16 const notice = useThreadUndoNotice((state) => state.notice);
17 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
18 // Short enough to fit beside the utility icons; screen readers hear the full summary.
19 const summary = notice
20 ? notice.count === 1
21 ? notice.action
22 : `${notice.action} ${notice.count}`
23 : null;
24​
25 // Keeps the last text on screen while it fades out, then unmounts on transitionend.
26 const [shownSummary, setShownSummary] = useState(summary);
27 if (summary && summary !== shownSummary) setShownSummary(summary);
28 const text = summary ?? shownSummary;
29 if (!text) return null;
30​
31 return (
32 // No min-w-0: the undo button sets this item's minimum, so a shared row squeezes its neighbour.
33 <SidebarMenuItem className="flex flex-1" aria-hidden={!summary} inert={!summary}>
34 <div
35 onTransitionEnd={(event) => {
36 if (!summary && event.target === event.currentTarget) setShownSummary(null);
37 }}
38 // Reversed wrap: a summary that does not fit beside the undo button drops to a
39 // second line that overflow hides, instead of truncating mid-word. Browse mode
40 // reads the button first; the live announcement carries the full sentence.
41 className={cn(
42 "flex h-6 min-w-0 flex-1 flex-row-reverse flex-wrap gap-x-1 overflow-hidden text-2xs leading-6 whitespace-nowrap text-sidebar-muted-foreground transition-opacity duration-200 ease-out starting:opacity-0",
43 !summary && "opacity-0",
44 )}
45 >
46 <InlineButton onClick={undoLatestThreadAction}>{threadUndoLabel(keybindings)}</InlineButton>
47 <span>{text},</span>
48 </div>
49 </SidebarMenuItem>
50 );