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

WorkLog.tsx

192 lines5.1 KB

1import {
2 createContext,
3 use,
4 useId,
5 useLayoutEffect,
6 useRef,
7 type ComponentProps,
8 type ReactNode,
9} from "react";
10import { cn } from "../../lib/utils";
11​
12const GroupedRows = createContext(false);
13​
14/** Groups may span virtualized timeline items. Each part owns its trailing space. */
15export function WorkLogBlock({
16 layout = "standalone",
17 continues = false,
18 children,
19}: {
20 layout?: "standalone" | "group-header" | "group-content";
21 continues?: boolean | undefined;
22 children: ReactNode;
23}) {
24 return (
25 <div
26 className={
27 continues || layout === "group-header"
28 ? "pb-0"
29 : layout === "group-content"
30 ? "pb-1"
31 : "pb-2"
32 }
33 >
34 {children}
35 </div>
36 );
38​
39export function WorkLogList({ children }: { children: ReactNode }) {
40 return (
41 <GroupedRows value>
42 <div className="flex min-w-0 flex-col">{children}</div>
43 </GroupedRows>
44 );
46​
47type RowContent = {
48 icon?: ReactNode;
49 label: ReactNode;
50 trailing?: ReactNode;
51 wrapLabel?: boolean;
52};
53​
54const focusRingClassName =
55 "rounded-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70";
56​
57type LineProps = Omit<ComponentProps<"div">, "className" | "style" | "children">;
58​
59function WorkLogLine({ icon, label, trailing, wrapLabel, ...lineProps }: RowContent & LineProps) {
60 return (
61 <div
62 {...lineProps}
63 className={cn(
64 "flex min-h-6 min-w-0 gap-1.5 text-sm leading-relaxed select-none [&_*]:select-none",
65 wrapLabel ? "items-start" : "items-center",
66 lineProps.role === "button" && focusRingClassName,
67 )}
68 >
69 <div className="flex min-w-0 flex-1 items-baseline gap-1.5">
70 {icon ? (
71 <span className="relative w-6 shrink-0 leading-0">
72 <span className="inline-block size-6 overflow-hidden align-[calc(0.5cap_-_0.75rem)]">
73 <span className="flex size-full items-center justify-center">{icon}</span>
74 </span>
75 </span>
76 ) : null}
77 <div
78 className={cn(
79 "min-w-0 flex-1 text-secondary-label",
80 !wrapLabel && "truncate [&_*]:whitespace-nowrap",
81 )}
82 >
83 {label}
84 </div>
85 </div>
86 {trailing}
87 </div>
88 );
90​
91function useRowClassName(interactive: boolean, focusable = interactive) {
92 const grouped = use(GroupedRows);
93 return cn(
94 "group/timeline-row relative w-full min-w-0 rounded-md px-0.5 text-left transition-colors",
95 grouped ? "py-0" : "py-0.5",
96 interactive && "cursor-pointer",
97 focusable && focusRingClassName,
98 );
100​
101export function WorkLogButton({
102 icon,
103 label,
104 trailing,
105 wrapLabel = false,
106 ...buttonProps
107}: RowContent & Omit<ComponentProps<"button">, "className" | "style" | "children">) {
108 const className = useRowClassName(true);
109 return (
110 <button {...buttonProps} type="button" className={className}>
111 <WorkLogLine icon={icon} label={label} trailing={trailing} wrapLabel={wrapLabel} />
112 </button>
113 );
115​
116type RowProps = Omit<ComponentProps<"div">, "className" | "style"> & {
117 toggle?: LineProps | undefined;
118};
119​
120/**
121 * Div disclosure permits selectable detail and nested actions. `toggle` goes on
122 * the header line only: assistive tech hides a button's children.
123 */
124export function WorkLogRow({
125 icon,
126 label,
127 trailing,
128 wrapLabel = false,
129 toggle,
130 children,
131 ...rowProps
132}: RowContent & RowProps) {
133 const className = useRowClassName(rowProps.onClick !== undefined, false);
134 const detailsId = useId();
135 return (
136 <div {...rowProps} className={className}>
137 <WorkLogLine
138 aria-controls={toggle ? detailsId : undefined}
139 {...toggle}
140 icon={icon}
141 label={label}
142 trailing={trailing}
143 wrapLabel={wrapLabel}
144 />
145 {toggle ? <div id={detailsId}>{children}</div> : children}
146 </div>
147 );
149​
150const LIVE_EDGE_PX = 4;
151​
152export function WorkLogDetails({
153 children,
154 kind = "text",
155 followLive = false,
156}: {
157 children: ReactNode;
158 kind?: "text" | "panel" | "media";
159​
160 followLive?: boolean;
161}) {
162 const scrollerRef = useRef<HTMLDivElement>(null);
163 const atLiveEdgeRef = useRef(true);
164 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pins the scroller's scrollTop to the bottom after each commit while following live output
165 useLayoutEffect(() => {
166 const scroller = scrollerRef.current;
167 if (followLive && atLiveEdgeRef.current && scroller) {
168 scroller.scrollTop = scroller.scrollHeight;
169 }
170 });
171 return (
172 <div
173 ref={scrollerRef}
174 onScroll={(event) => {
175 const { scrollHeight, scrollTop, clientHeight } = event.currentTarget;
176 atLiveEdgeRef.current = scrollHeight - scrollTop - clientHeight <= LIVE_EDGE_PX;
177 }}
178 className={cn(
179 "cursor-auto",
180 kind === "text"
181 ? "ms-7 flex max-h-96 flex-col gap-3 overflow-auto px-0.5 py-1 select-text"
182 : kind === "panel"
183 ? "mt-0.5 mb-1.5 pb-2"
184 : "mt-1",
185 )}
186 onClick={(event) => event.stopPropagation()}
187 onPointerDown={(event) => event.stopPropagation()}
188 >
189 {children}
190 </div>
191 );