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