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

SidebarThreadUndoNotice.tsx

62 lines2.6 KB

1import { useAtomValue } from "@effect/atom-react";
2import { Undo2Icon } from "lucide-react";
3import { useState } from "react";
4​
5import {
6 threadUndoLabel,
7 undoLatestThreadAction,
8 useThreadUndoNotice,
9} from "../../hooks/showThreadUndoNotice";
10import { cn } from "../../lib/utils";
11import { primaryServerKeybindingsAtom } from "../../state/server";
12import { InlineButton } from "../ui/button";
13import { SidebarMenuItem } from "../ui/sidebar";
14​
15/** Fills the free space in the sidebar footer's utility row, so it never moves or covers the list. */
16export function SidebarThreadUndoNotice() {
17 const notice = useThreadUndoNotice((state) => state.notice);
18 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
19 // Short enough to fit beside the utility icons; screen readers hear the full summary.
20 const summary = notice
21 ? notice.count === 1
22 ? notice.action
23 : `${notice.action} ${notice.count}`
24 : null;
25​
26 // Keeps the last text on screen while it fades out, then unmounts on transitionend.
27 const [shownSummary, setShownSummary] = useState(summary);
28 if (summary && summary !== shownSummary) setShownSummary(summary);
29 const text = summary ?? shownSummary;
30 if (!text) return null;
31​
32 return (
33 <SidebarMenuItem
34 className="@container flex min-w-0 flex-1"
35 aria-hidden={!summary}
36 inert={!summary}
37 >
38 <div
39 onTransitionEnd={(event) => {
40 if (!summary && event.target === event.currentTarget) setShownSummary(null);
41 }}
42 // Reversed wrap: a summary that does not fit beside the undo button drops to a
43 // second line that overflow hides, instead of truncating mid-word. Browse mode
44 // reads the button first; the live announcement carries the full sentence.
45 className={cn(
46 "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",
47 !summary && "opacity-0",
48 )}
49 >
50 <InlineButton className="min-w-0 shrink" onClick={undoLatestThreadAction}>
51 {/* The icon's negative margin cancels its width, so beside the label it sits in
52 clipped space; once the label is too wide it wraps away and the icon shows. */}
53 <span className="flex h-6 min-w-0 flex-wrap justify-end overflow-hidden">
54 <Undo2Icon className="-ml-4 h-6 w-4 @max-[1rem]:invisible" />
55 <span>{threadUndoLabel(keybindings)}</span>
56 </span>
57 </InlineButton>
58 <span>{text},</span>
59 </div>
60 </SidebarMenuItem>
61 );