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

WorkLog.tsx

148 lines4.1 KB

1import { createContext, use, type ComponentProps, type ReactNode } from "react";
2import { cn } from "../../lib/utils";
3​
4const GroupedRows = createContext(false);
5​
6/** Groups may span virtualized timeline items. Each part owns its trailing space. */
7export function WorkLogBlock({
8 layout = "standalone",
9 continues = false,
10 children,
11}: {
12 layout?: "standalone" | "group-header" | "group-content";
13 continues?: boolean | undefined;
14 children: ReactNode;
15}) {
16 return (
17 <div
18 className={
19 continues || layout === "group-header"
20 ? "pb-0"
21 : layout === "group-content"
22 ? "pb-1"
23 : "pb-2"
24 }
25 >
26 {children}
27 </div>
28 );
30​
31/** Expanded members align with the header and use the same compact row geometry. */
32export function WorkLogList({ children }: { children: ReactNode }) {
33 return (
34 <GroupedRows value>
35 <div className="flex min-w-0 flex-col">{children}</div>
36 </GroupedRows>
37 );
39​
40type RowContent = {
41 icon?: ReactNode;
42 label: ReactNode;
43 trailing?: ReactNode;
44 wrapLabel?: boolean;
45};
46​
47function WorkLogLine({ icon, label, trailing, wrapLabel }: RowContent) {
48 return (
49 <div
50 className={cn(
51 "flex min-h-6 min-w-0 gap-1.5 text-sm leading-relaxed select-none [&_*]:select-none",
52 wrapLabel ? "items-start" : "items-center",
53 )}
54 >
55 <div className="flex min-w-0 flex-1 items-baseline gap-1.5">
56 {icon ? (
57 // Align with the first line's capital letters. Centering line boxes drifts
58 // when font ascent/descent rounding changes at fractional browser zoom.
59 <span className="relative w-6 shrink-0 leading-0">
60 <span className="inline-block size-6 overflow-hidden align-[calc(0.5cap_-_0.75rem)]">
61 <span className="flex size-full items-center justify-center">{icon}</span>
62 </span>
63 </span>
64 ) : null}
65 <div
66 className={cn(
67 "min-w-0 flex-1 text-secondary-label",
68 !wrapLabel && "truncate [&_*]:whitespace-nowrap",
69 )}
70 >
71 {label}
72 </div>
73 </div>
74 {trailing}
75 </div>
76 );
78​
79const interactionClassName =
80 "cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70";
81​
82function useRowClassName(interactive: boolean) {
83 const grouped = use(GroupedRows);
84 return cn(
85 "group/timeline-row relative w-full min-w-0 rounded-md px-0.5 text-left transition-colors",
86 grouped ? "py-0" : "py-0.5",
87 interactive && interactionClassName,
88 );
90​
91// No className/style escape hatch: consumers supply content and behavior, not geometry.
92export function WorkLogButton({
93 icon,
94 label,
95 trailing,
96 wrapLabel = false,
97 ...buttonProps
98}: RowContent & Omit<ComponentProps<"button">, "className" | "style" | "children">) {
99 const className = useRowClassName(true);
100 return (
101 <button {...buttonProps} type="button" className={className}>
102 <WorkLogLine icon={icon} label={label} trailing={trailing} wrapLabel={wrapLabel} />
103 </button>
104 );
106​
107/** Div-based disclosure supports selectable detail and nested actions in an entry. */
108export function WorkLogRow({
109 icon,
110 label,
111 trailing,
112 wrapLabel = false,
113 children,
114 ...rowProps
115}: RowContent & Omit<ComponentProps<"div">, "className" | "style">) {
116 const className = useRowClassName(rowProps.onClick !== undefined);
117 return (
118 <div {...rowProps} className={className}>
119 <WorkLogLine icon={icon} label={label} trailing={trailing} wrapLabel={wrapLabel} />
120 {children}
121 </div>
122 );
124​
125export function WorkLogDetails({
126 children,
127 kind = "text",
128}: {
129 children: ReactNode;
130 kind?: "text" | "panel" | "media";
131}) {
132 return (
133 <div
134 className={cn(
135 "cursor-auto",
136 kind === "text"
137 ? "ms-7 flex max-h-96 flex-col gap-3 overflow-auto px-0.5 py-1 select-text"
138 : kind === "panel"
139 ? "mt-0.5 mb-1.5 pb-2"
140 : "mt-1",
141 )}
142 onClick={(event) => event.stopPropagation()}
143 onPointerDown={(event) => event.stopPropagation()}
144 >
145 {children}
146 </div>
147 );