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

SidebarThreadUndoNotice.tsx

103 lines3.5 KB

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