@dowel-ui/react 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/dist/components/ai-disclosure/ai-disclosure.d.ts +92 -0
- package/dist/components/ai-disclosure/ai-disclosure.d.ts.map +1 -0
- package/dist/components/ai-disclosure/ai-disclosure.js +122 -0
- package/dist/components/ai-disclosure/ai-disclosure.js.map +1 -0
- package/dist/components/ai-disclosure/index.d.ts +2 -0
- package/dist/components/ai-disclosure/index.js +2 -0
- package/dist/components/ai-disclosure/meta.js +17 -0
- package/dist/components/ai-disclosure/meta.js.map +1 -0
- package/dist/components/avatar/avatar.d.ts +1 -1
- package/dist/components/date-picker/date-picker.js +1 -1
- package/dist/components/diff-viewer/diff-model.d.ts +69 -0
- package/dist/components/diff-viewer/diff-model.d.ts.map +1 -0
- package/dist/components/diff-viewer/diff-model.js +180 -0
- package/dist/components/diff-viewer/diff-model.js.map +1 -0
- package/dist/components/diff-viewer/diff-viewer.d.ts +42 -0
- package/dist/components/diff-viewer/diff-viewer.d.ts.map +1 -0
- package/dist/components/diff-viewer/diff-viewer.js +212 -0
- package/dist/components/diff-viewer/diff-viewer.js.map +1 -0
- package/dist/components/diff-viewer/index.d.ts +3 -0
- package/dist/components/diff-viewer/index.js +3 -0
- package/dist/components/diff-viewer/meta.js +17 -0
- package/dist/components/diff-viewer/meta.js.map +1 -0
- package/dist/components/log-viewer/index.d.ts +3 -0
- package/dist/components/log-viewer/index.js +3 -0
- package/dist/components/log-viewer/log-stream.d.ts +68 -0
- package/dist/components/log-viewer/log-stream.d.ts.map +1 -0
- package/dist/components/log-viewer/log-stream.js +120 -0
- package/dist/components/log-viewer/log-stream.js.map +1 -0
- package/dist/components/log-viewer/log-viewer.d.ts +51 -0
- package/dist/components/log-viewer/log-viewer.d.ts.map +1 -0
- package/dist/components/log-viewer/log-viewer.js +284 -0
- package/dist/components/log-viewer/log-viewer.js.map +1 -0
- package/dist/components/log-viewer/meta.js +17 -0
- package/dist/components/log-viewer/meta.js.map +1 -0
- package/dist/components/spinner/spinner.d.ts +1 -1
- package/dist/components/time-range-picker/index.d.ts +3 -0
- package/dist/components/time-range-picker/index.js +3 -0
- package/dist/components/time-range-picker/meta.js +22 -0
- package/dist/components/time-range-picker/meta.js.map +1 -0
- package/dist/components/time-range-picker/time-expression.d.ts +88 -0
- package/dist/components/time-range-picker/time-expression.d.ts.map +1 -0
- package/dist/components/time-range-picker/time-expression.js +335 -0
- package/dist/components/time-range-picker/time-expression.js.map +1 -0
- package/dist/components/time-range-picker/time-range-picker.d.ts +65 -0
- package/dist/components/time-range-picker/time-range-picker.d.ts.map +1 -0
- package/dist/components/time-range-picker/time-range-picker.js +377 -0
- package/dist/components/time-range-picker/time-range-picker.js.map +1 -0
- package/dist/index.d.ts +12 -1
- package/dist/index.js +9 -2
- package/dist/registry/components.d.ts.map +1 -1
- package/dist/registry/components.js +58 -50
- package/dist/registry/components.js.map +1 -1
- package/package.json +6 -4
- package/src/components/ai-disclosure/ai-disclosure.tsx +244 -0
- package/src/components/ai-disclosure/index.ts +9 -0
- package/src/components/ai-disclosure/meta.ts +23 -0
- package/src/components/diff-viewer/diff-model.ts +256 -0
- package/src/components/diff-viewer/diff-viewer.tsx +339 -0
- package/src/components/diff-viewer/index.ts +17 -0
- package/src/components/diff-viewer/meta.ts +20 -0
- package/src/components/log-viewer/index.ts +21 -0
- package/src/components/log-viewer/log-stream.ts +176 -0
- package/src/components/log-viewer/log-viewer.tsx +424 -0
- package/src/components/log-viewer/meta.ts +21 -0
- package/src/components/time-range-picker/index.ts +25 -0
- package/src/components/time-range-picker/meta.ts +22 -0
- package/src/components/time-range-picker/time-expression.ts +491 -0
- package/src/components/time-range-picker/time-range-picker.tsx +522 -0
- package/src/index.ts +4 -0
- package/src/registry/components.ts +8 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useMemo, useState } from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Filtering and match-finding for a log stream.
|
|
7
|
+
*
|
|
8
|
+
* Separate from the view because it is pure and worth testing without a DOM,
|
|
9
|
+
* and because the awkward parts are here rather than in the rendering: a bad
|
|
10
|
+
* regex must not throw while someone is halfway through typing it, and a match
|
|
11
|
+
* has to be located precisely enough to highlight rather than merely detected.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
15
|
+
|
|
16
|
+
export const LOG_LEVELS: readonly LogLevel[] = [
|
|
17
|
+
"trace",
|
|
18
|
+
"debug",
|
|
19
|
+
"info",
|
|
20
|
+
"warn",
|
|
21
|
+
"error",
|
|
22
|
+
"fatal",
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export interface LogLine {
|
|
26
|
+
id: string;
|
|
27
|
+
message: string;
|
|
28
|
+
level?: LogLevel;
|
|
29
|
+
/** ISO timestamp, or anything the consumer wants shown in the gutter. */
|
|
30
|
+
timestamp?: string;
|
|
31
|
+
/** Structured fields, revealed when the row is expanded. */
|
|
32
|
+
fields?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A [start, end) slice of a message that matched the filter. */
|
|
36
|
+
export type MatchRange = readonly [number, number];
|
|
37
|
+
|
|
38
|
+
export interface FilterState {
|
|
39
|
+
/** Substring, or a pattern when `regex` is on. */
|
|
40
|
+
query: string;
|
|
41
|
+
regex: boolean;
|
|
42
|
+
/** Levels to show. Empty means all of them. */
|
|
43
|
+
levels: Set<LogLevel>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Compiles the query.
|
|
48
|
+
*
|
|
49
|
+
* Returns null for an invalid pattern rather than throwing: the query is being
|
|
50
|
+
* typed, so it spends most of its life syntactically incomplete, and a viewer
|
|
51
|
+
* that crashes on "(" is unusable.
|
|
52
|
+
*/
|
|
53
|
+
export function compileQuery(query: string, regex: boolean): RegExp | null {
|
|
54
|
+
if (query.length === 0) return null;
|
|
55
|
+
try {
|
|
56
|
+
return regex
|
|
57
|
+
? new RegExp(query, "gi")
|
|
58
|
+
: new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Every match in a line, so each can be highlighted rather than just the first. */
|
|
65
|
+
export function findMatches(message: string, pattern: RegExp | null): MatchRange[] {
|
|
66
|
+
if (!pattern) return [];
|
|
67
|
+
|
|
68
|
+
const ranges: MatchRange[] = [];
|
|
69
|
+
// Fresh lastIndex: the pattern is reused across lines and a global regex
|
|
70
|
+
// carries position between calls, which would skip matches in later lines.
|
|
71
|
+
pattern.lastIndex = 0;
|
|
72
|
+
|
|
73
|
+
let match = pattern.exec(message);
|
|
74
|
+
while (match !== null) {
|
|
75
|
+
// A pattern that can match empty — "a*" — would loop forever otherwise.
|
|
76
|
+
if (match[0].length === 0) {
|
|
77
|
+
pattern.lastIndex += 1;
|
|
78
|
+
} else {
|
|
79
|
+
ranges.push([match.index, match.index + match[0].length]);
|
|
80
|
+
}
|
|
81
|
+
match = pattern.exec(message);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return ranges;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Splits a message into alternating plain and matched segments. */
|
|
88
|
+
export function segment(
|
|
89
|
+
message: string,
|
|
90
|
+
ranges: MatchRange[],
|
|
91
|
+
): { text: string; match: boolean }[] {
|
|
92
|
+
if (ranges.length === 0) return [{ text: message, match: false }];
|
|
93
|
+
|
|
94
|
+
const parts: { text: string; match: boolean }[] = [];
|
|
95
|
+
let cursor = 0;
|
|
96
|
+
|
|
97
|
+
for (const [start, end] of ranges) {
|
|
98
|
+
if (start > cursor) parts.push({ text: message.slice(cursor, start), match: false });
|
|
99
|
+
parts.push({ text: message.slice(start, end), match: true });
|
|
100
|
+
cursor = end;
|
|
101
|
+
}
|
|
102
|
+
if (cursor < message.length) parts.push({ text: message.slice(cursor), match: false });
|
|
103
|
+
|
|
104
|
+
return parts;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface VisibleLine extends LogLine {
|
|
108
|
+
matches: MatchRange[];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface UseLogStreamOptions {
|
|
112
|
+
lines: LogLine[];
|
|
113
|
+
/** Levels present but unchecked are hidden. Empty shows everything. */
|
|
114
|
+
initialLevels?: Set<LogLevel>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function useLogStream({ lines, initialLevels }: UseLogStreamOptions) {
|
|
118
|
+
const [query, setQuery] = useState("");
|
|
119
|
+
const [regex, setRegex] = useState(false);
|
|
120
|
+
const [levels, setLevels] = useState<Set<LogLevel>>(initialLevels ?? new Set());
|
|
121
|
+
|
|
122
|
+
const pattern = useMemo(() => compileQuery(query, regex), [query, regex]);
|
|
123
|
+
|
|
124
|
+
// An invalid pattern is reported, not swallowed: with no feedback the reader
|
|
125
|
+
// sees an empty log and concludes nothing matched.
|
|
126
|
+
const invalidPattern = regex && query.length > 0 && pattern === null;
|
|
127
|
+
|
|
128
|
+
const visible = useMemo<VisibleLine[]>(() => {
|
|
129
|
+
const result: VisibleLine[] = [];
|
|
130
|
+
|
|
131
|
+
for (const line of lines) {
|
|
132
|
+
if (levels.size > 0 && line.level && !levels.has(line.level)) continue;
|
|
133
|
+
|
|
134
|
+
const matches = findMatches(line.message, pattern);
|
|
135
|
+
// A query with no match hides the line; an invalid pattern hides nothing,
|
|
136
|
+
// because the reader has not finished saying what they want yet.
|
|
137
|
+
if (pattern && matches.length === 0) continue;
|
|
138
|
+
|
|
139
|
+
result.push({ ...line, matches });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return result;
|
|
143
|
+
}, [lines, levels, pattern]);
|
|
144
|
+
|
|
145
|
+
const counts = useMemo(() => {
|
|
146
|
+
const byLevel = new Map<LogLevel, number>();
|
|
147
|
+
for (const line of lines) {
|
|
148
|
+
if (!line.level) continue;
|
|
149
|
+
byLevel.set(line.level, (byLevel.get(line.level) ?? 0) + 1);
|
|
150
|
+
}
|
|
151
|
+
return byLevel;
|
|
152
|
+
}, [lines]);
|
|
153
|
+
|
|
154
|
+
const toggleLevel = useCallback((level: LogLevel) => {
|
|
155
|
+
setLevels((current) => {
|
|
156
|
+
const next = new Set(current);
|
|
157
|
+
if (next.has(level)) next.delete(level);
|
|
158
|
+
else next.add(level);
|
|
159
|
+
return next;
|
|
160
|
+
});
|
|
161
|
+
}, []);
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
query,
|
|
165
|
+
setQuery,
|
|
166
|
+
regex,
|
|
167
|
+
setRegex,
|
|
168
|
+
levels,
|
|
169
|
+
toggleLevel,
|
|
170
|
+
setLevels,
|
|
171
|
+
visible,
|
|
172
|
+
counts,
|
|
173
|
+
invalidPattern,
|
|
174
|
+
total: lines.length,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
4
|
+
import {
|
|
5
|
+
useEffect,
|
|
6
|
+
useId,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
type ComponentPropsWithRef,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from "react";
|
|
12
|
+
|
|
13
|
+
import { disabledStyles, focusRing } from "@/lib/styles";
|
|
14
|
+
import { cn } from "@/lib/utils";
|
|
15
|
+
|
|
16
|
+
import { LOG_LEVELS, segment, type LogLevel, type VisibleLine } from "./log-stream";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A streaming console: filter, follow, expand.
|
|
20
|
+
*
|
|
21
|
+
* The incumbent is react-lazylog, which was last published in 2022, is built on
|
|
22
|
+
* react-virtualized, and cannot run on React 19 — while still taking about
|
|
23
|
+
* 15,000 downloads a week. This is for those people.
|
|
24
|
+
*
|
|
25
|
+
* Two accessibility decisions worth stating rather than discovering.
|
|
26
|
+
*
|
|
27
|
+
* `role="log"` implies `aria-live="polite"`, which is right for a handful of
|
|
28
|
+
* events and catastrophic for a console: a screen reader would read every line
|
|
29
|
+
* of a build as it scrolls past, and nothing else would be audible. Announcing
|
|
30
|
+
* is therefore off by default and opt-in through `announce`. The role stays,
|
|
31
|
+
* because it still describes what the region is.
|
|
32
|
+
*
|
|
33
|
+
* And virtualization means most rows are not in the DOM. That is a real trade,
|
|
34
|
+
* not an implementation detail: assistive technology cannot reach what is not
|
|
35
|
+
* rendered, so a reader who needs the whole log needs an escape — which is what
|
|
36
|
+
* `onDownload` is for. A log viewer with no way out of the virtual window is
|
|
37
|
+
* not accessible however good its ARIA is.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
const LEVEL_STYLES: Record<LogLevel, string> = {
|
|
41
|
+
trace: "text-muted-foreground",
|
|
42
|
+
debug: "text-muted-foreground",
|
|
43
|
+
info: "text-foreground",
|
|
44
|
+
warn: "text-warning",
|
|
45
|
+
error: "text-destructive",
|
|
46
|
+
fatal: "text-destructive font-semibold",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** Distance from the bottom, in pixels, that still counts as "at the end". */
|
|
50
|
+
const FOLLOW_THRESHOLD = 24;
|
|
51
|
+
|
|
52
|
+
export interface LogViewerProps extends Omit<ComponentPropsWithRef<"div">, "children"> {
|
|
53
|
+
lines: VisibleLine[];
|
|
54
|
+
/** Names the region. */
|
|
55
|
+
label: string;
|
|
56
|
+
/** Row height in pixels. Rows are one line; expansion is measured. */
|
|
57
|
+
rowHeight?: number;
|
|
58
|
+
height?: number | string;
|
|
59
|
+
/**
|
|
60
|
+
* Read new lines aloud. Off by default — a console that announces every line
|
|
61
|
+
* makes a screen reader useless for anything else.
|
|
62
|
+
*/
|
|
63
|
+
announce?: boolean;
|
|
64
|
+
/** The way out of the virtual window, for anyone who needs the whole log. */
|
|
65
|
+
onDownload?: () => void;
|
|
66
|
+
children?: ReactNode;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function LogViewer({
|
|
70
|
+
className,
|
|
71
|
+
lines,
|
|
72
|
+
label,
|
|
73
|
+
rowHeight = 22,
|
|
74
|
+
height = 360,
|
|
75
|
+
announce = false,
|
|
76
|
+
onDownload,
|
|
77
|
+
children,
|
|
78
|
+
...props
|
|
79
|
+
}: LogViewerProps) {
|
|
80
|
+
const scrollRef = useRef<HTMLDivElement | null>(null);
|
|
81
|
+
const [following, setFollowing] = useState(true);
|
|
82
|
+
const [expanded, setExpanded] = useState<Set<string>>(new Set());
|
|
83
|
+
const labelId = useId();
|
|
84
|
+
|
|
85
|
+
const virtualizer = useVirtualizer({
|
|
86
|
+
count: lines.length,
|
|
87
|
+
getScrollElement: () => scrollRef.current,
|
|
88
|
+
estimateSize: () => rowHeight,
|
|
89
|
+
overscan: 12,
|
|
90
|
+
// Without a starting rect the virtualizer renders nothing until a
|
|
91
|
+
// ResizeObserver fires, so the first paint is an empty box. Seeding it from
|
|
92
|
+
// the declared height means rows are there immediately, and it is measured
|
|
93
|
+
// properly a moment later.
|
|
94
|
+
initialRect: { width: 0, height: typeof height === "number" ? height : 360 },
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Follow mode, hand-written rather than delegated. A spring-anchoring library
|
|
98
|
+
// wants to own the scroll container and so does the virtualizer, and the two
|
|
99
|
+
// fight; this is scrollToIndex on append plus a proximity check.
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (!following || lines.length === 0) return;
|
|
102
|
+
virtualizer.scrollToIndex(lines.length - 1, { align: "end" });
|
|
103
|
+
}, [following, lines.length, virtualizer]);
|
|
104
|
+
|
|
105
|
+
function handleScroll() {
|
|
106
|
+
const element = scrollRef.current;
|
|
107
|
+
if (!element) return;
|
|
108
|
+
|
|
109
|
+
const distance = element.scrollHeight - element.scrollTop - element.clientHeight;
|
|
110
|
+
// Scrolling up detaches, because the reader is looking at something and
|
|
111
|
+
// yanking them back to the tail would lose it. Returning to the bottom
|
|
112
|
+
// re-attaches, which is the gesture people already expect.
|
|
113
|
+
setFollowing(distance <= FOLLOW_THRESHOLD);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function toggleExpanded(id: string) {
|
|
117
|
+
setExpanded((current) => {
|
|
118
|
+
const next = new Set(current);
|
|
119
|
+
if (next.has(id)) next.delete(id);
|
|
120
|
+
else next.add(id);
|
|
121
|
+
return next;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const items = virtualizer.getVirtualItems();
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<div data-slot="log-viewer" className={cn("flex flex-col gap-2", className)} {...props}>
|
|
129
|
+
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
130
|
+
<span id={labelId} className="text-sm font-medium">
|
|
131
|
+
{label}
|
|
132
|
+
</span>
|
|
133
|
+
<div className="flex items-center gap-2">
|
|
134
|
+
{!following ? (
|
|
135
|
+
<button
|
|
136
|
+
type="button"
|
|
137
|
+
data-slot="log-viewer-jump"
|
|
138
|
+
onClick={() => {
|
|
139
|
+
setFollowing(true);
|
|
140
|
+
}}
|
|
141
|
+
className={cn(
|
|
142
|
+
"rounded-md border border-input bg-background px-2 py-0.5 text-xs font-medium",
|
|
143
|
+
"transition-colors hover:bg-accent hover:text-accent-foreground",
|
|
144
|
+
focusRing,
|
|
145
|
+
)}
|
|
146
|
+
>
|
|
147
|
+
Jump to latest
|
|
148
|
+
</button>
|
|
149
|
+
) : (
|
|
150
|
+
<span className="text-xs text-muted-foreground">Following</span>
|
|
151
|
+
)}
|
|
152
|
+
{onDownload ? (
|
|
153
|
+
<button
|
|
154
|
+
type="button"
|
|
155
|
+
data-slot="log-viewer-download"
|
|
156
|
+
onClick={onDownload}
|
|
157
|
+
className={cn(
|
|
158
|
+
"rounded-md border border-input bg-background px-2 py-0.5 text-xs font-medium",
|
|
159
|
+
"transition-colors hover:bg-accent hover:text-accent-foreground",
|
|
160
|
+
focusRing,
|
|
161
|
+
)}
|
|
162
|
+
>
|
|
163
|
+
Download full log
|
|
164
|
+
</button>
|
|
165
|
+
) : null}
|
|
166
|
+
</div>
|
|
167
|
+
</div>
|
|
168
|
+
|
|
169
|
+
{children}
|
|
170
|
+
|
|
171
|
+
<div
|
|
172
|
+
ref={scrollRef}
|
|
173
|
+
onScroll={handleScroll}
|
|
174
|
+
role="log"
|
|
175
|
+
aria-labelledby={labelId}
|
|
176
|
+
// Explicitly off unless asked for: role="log" implies polite, and a
|
|
177
|
+
// console that reads every line aloud drowns out everything else.
|
|
178
|
+
aria-live={announce ? "polite" : "off"}
|
|
179
|
+
// A scrollable region must be focusable, or its content is unreachable
|
|
180
|
+
// without a pointer — WCAG 2.1.1, and axe's scrollable-region-focusable
|
|
181
|
+
// requires exactly this. jsx-a11y's heuristic cannot see that this
|
|
182
|
+
// element scrolls, so here the requirement outranks the rule.
|
|
183
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
|
184
|
+
tabIndex={0}
|
|
185
|
+
style={{ height }}
|
|
186
|
+
className={cn(
|
|
187
|
+
"overflow-auto rounded-lg border border-border bg-muted/30 font-mono text-xs",
|
|
188
|
+
focusRing,
|
|
189
|
+
)}
|
|
190
|
+
>
|
|
191
|
+
{lines.length === 0 ? (
|
|
192
|
+
<p className="p-3 text-muted-foreground">No lines match the current filter.</p>
|
|
193
|
+
) : (
|
|
194
|
+
<div style={{ height: virtualizer.getTotalSize(), position: "relative" }}>
|
|
195
|
+
{items.map((item) => {
|
|
196
|
+
const line = lines[item.index];
|
|
197
|
+
if (!line) return null;
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<div
|
|
201
|
+
key={line.id}
|
|
202
|
+
ref={virtualizer.measureElement}
|
|
203
|
+
data-index={item.index}
|
|
204
|
+
data-slot="log-viewer-row"
|
|
205
|
+
data-level={line.level}
|
|
206
|
+
className="absolute top-0 left-0 w-full"
|
|
207
|
+
style={{ transform: `translateY(${String(item.start)}px)` }}
|
|
208
|
+
>
|
|
209
|
+
<LogViewerRow
|
|
210
|
+
line={line}
|
|
211
|
+
expanded={expanded.has(line.id)}
|
|
212
|
+
onToggle={() => {
|
|
213
|
+
toggleExpanded(line.id);
|
|
214
|
+
}}
|
|
215
|
+
/>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
})}
|
|
219
|
+
</div>
|
|
220
|
+
)}
|
|
221
|
+
</div>
|
|
222
|
+
</div>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface LogViewerRowProps {
|
|
227
|
+
line: VisibleLine;
|
|
228
|
+
expanded?: boolean;
|
|
229
|
+
onToggle?: () => void;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* One line.
|
|
234
|
+
*
|
|
235
|
+
* Exported because the virtualized container cannot be measured outside a real
|
|
236
|
+
* browser — jsdom reports every box as zero — so the row's behaviour would
|
|
237
|
+
* otherwise be untestable. It is also genuinely useful on its own for anyone
|
|
238
|
+
* rendering a short log without virtualization.
|
|
239
|
+
*/
|
|
240
|
+
export function LogViewerRow({ line, expanded = false, onToggle }: LogViewerRowProps) {
|
|
241
|
+
const hasFields = line.fields !== undefined && Object.keys(line.fields).length > 0;
|
|
242
|
+
const parts = segment(line.message, line.matches);
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<div className="px-3 py-0.5 hover:bg-accent/40">
|
|
246
|
+
<div className="flex items-baseline gap-2">
|
|
247
|
+
{line.timestamp ? (
|
|
248
|
+
<time className="shrink-0 text-muted-foreground tabular-nums">{line.timestamp}</time>
|
|
249
|
+
) : null}
|
|
250
|
+
|
|
251
|
+
{line.level ? (
|
|
252
|
+
// The level in text, not a coloured bar. Colour alone cannot
|
|
253
|
+
// distinguish warn from error for everyone reading.
|
|
254
|
+
<span className={cn("w-11 shrink-0 uppercase", LEVEL_STYLES[line.level])}>
|
|
255
|
+
{line.level}
|
|
256
|
+
</span>
|
|
257
|
+
) : null}
|
|
258
|
+
|
|
259
|
+
<span
|
|
260
|
+
className={cn(
|
|
261
|
+
"min-w-0 flex-1 break-all whitespace-pre-wrap",
|
|
262
|
+
line.level && LEVEL_STYLES[line.level],
|
|
263
|
+
)}
|
|
264
|
+
>
|
|
265
|
+
{parts.map((part, index) =>
|
|
266
|
+
part.match ? (
|
|
267
|
+
// A mark element, so the match is conveyed structurally rather
|
|
268
|
+
// than only as a background colour.
|
|
269
|
+
<mark key={index} className="rounded-[2px] bg-warning/35 text-inherit">
|
|
270
|
+
{part.text}
|
|
271
|
+
</mark>
|
|
272
|
+
) : (
|
|
273
|
+
<span key={index}>{part.text}</span>
|
|
274
|
+
),
|
|
275
|
+
)}
|
|
276
|
+
</span>
|
|
277
|
+
|
|
278
|
+
{hasFields ? (
|
|
279
|
+
<button
|
|
280
|
+
type="button"
|
|
281
|
+
data-slot="log-viewer-expand"
|
|
282
|
+
aria-expanded={expanded}
|
|
283
|
+
onClick={onToggle}
|
|
284
|
+
className={cn(
|
|
285
|
+
"shrink-0 rounded px-1 text-2xs text-muted-foreground",
|
|
286
|
+
"transition-colors hover:text-foreground",
|
|
287
|
+
focusRing,
|
|
288
|
+
disabledStyles,
|
|
289
|
+
)}
|
|
290
|
+
>
|
|
291
|
+
{expanded ? "Hide fields" : "Fields"}
|
|
292
|
+
</button>
|
|
293
|
+
) : null}
|
|
294
|
+
</div>
|
|
295
|
+
|
|
296
|
+
{expanded && line.fields ? (
|
|
297
|
+
<dl
|
|
298
|
+
data-slot="log-viewer-fields"
|
|
299
|
+
className="mt-1 mb-1 ml-4 grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 border-l border-border pl-3"
|
|
300
|
+
>
|
|
301
|
+
{Object.entries(line.fields).map(([key, value]) => (
|
|
302
|
+
<div key={key} className="contents">
|
|
303
|
+
<dt className="text-muted-foreground">{key}</dt>
|
|
304
|
+
<dd className="m-0 break-all">
|
|
305
|
+
{typeof value === "string" ? value : JSON.stringify(value)}
|
|
306
|
+
</dd>
|
|
307
|
+
</div>
|
|
308
|
+
))}
|
|
309
|
+
</dl>
|
|
310
|
+
) : null}
|
|
311
|
+
</div>
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export interface LogViewerToolbarProps extends Omit<ComponentPropsWithRef<"div">, "onChange"> {
|
|
316
|
+
query: string;
|
|
317
|
+
onQueryChange: (query: string) => void;
|
|
318
|
+
regex: boolean;
|
|
319
|
+
onRegexChange: (regex: boolean) => void;
|
|
320
|
+
levels: Set<LogLevel>;
|
|
321
|
+
onToggleLevel: (level: LogLevel) => void;
|
|
322
|
+
counts: Map<LogLevel, number>;
|
|
323
|
+
invalidPattern?: boolean;
|
|
324
|
+
/** Shown as "N of M lines". */
|
|
325
|
+
showing: number;
|
|
326
|
+
total: number;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function LogViewerToolbar({
|
|
330
|
+
className,
|
|
331
|
+
query,
|
|
332
|
+
onQueryChange,
|
|
333
|
+
regex,
|
|
334
|
+
onRegexChange,
|
|
335
|
+
levels,
|
|
336
|
+
onToggleLevel,
|
|
337
|
+
counts,
|
|
338
|
+
invalidPattern = false,
|
|
339
|
+
showing,
|
|
340
|
+
total,
|
|
341
|
+
...props
|
|
342
|
+
}: LogViewerToolbarProps) {
|
|
343
|
+
const filterId = useId();
|
|
344
|
+
const errorId = useId();
|
|
345
|
+
|
|
346
|
+
return (
|
|
347
|
+
<div
|
|
348
|
+
data-slot="log-viewer-toolbar"
|
|
349
|
+
className={cn("flex flex-wrap items-center gap-2", className)}
|
|
350
|
+
{...props}
|
|
351
|
+
>
|
|
352
|
+
<div className="flex min-w-0 flex-1 items-center gap-2">
|
|
353
|
+
<label htmlFor={filterId} className="sr-only">
|
|
354
|
+
Filter log
|
|
355
|
+
</label>
|
|
356
|
+
<input
|
|
357
|
+
id={filterId}
|
|
358
|
+
type="search"
|
|
359
|
+
value={query}
|
|
360
|
+
placeholder={regex ? "Pattern…" : "Filter…"}
|
|
361
|
+
aria-invalid={invalidPattern || undefined}
|
|
362
|
+
aria-describedby={invalidPattern ? errorId : undefined}
|
|
363
|
+
onChange={(event) => {
|
|
364
|
+
onQueryChange(event.target.value);
|
|
365
|
+
}}
|
|
366
|
+
className={cn(
|
|
367
|
+
"min-w-32 flex-1 rounded-md border border-input bg-background px-2 py-1 text-sm",
|
|
368
|
+
invalidPattern && "border-destructive",
|
|
369
|
+
focusRing,
|
|
370
|
+
)}
|
|
371
|
+
/>
|
|
372
|
+
<label className="flex shrink-0 items-center gap-1 text-xs text-muted-foreground">
|
|
373
|
+
<input
|
|
374
|
+
type="checkbox"
|
|
375
|
+
checked={regex}
|
|
376
|
+
onChange={(event) => {
|
|
377
|
+
onRegexChange(event.target.checked);
|
|
378
|
+
}}
|
|
379
|
+
className={cn("size-3.5 rounded border-input", focusRing)}
|
|
380
|
+
/>
|
|
381
|
+
Regex
|
|
382
|
+
</label>
|
|
383
|
+
</div>
|
|
384
|
+
|
|
385
|
+
<div className="flex flex-wrap items-center gap-1">
|
|
386
|
+
{LOG_LEVELS.filter((level) => counts.has(level)).map((level) => {
|
|
387
|
+
const on = levels.size === 0 || levels.has(level);
|
|
388
|
+
return (
|
|
389
|
+
<button
|
|
390
|
+
key={level}
|
|
391
|
+
type="button"
|
|
392
|
+
data-slot="log-viewer-facet"
|
|
393
|
+
aria-pressed={on}
|
|
394
|
+
onClick={() => {
|
|
395
|
+
onToggleLevel(level);
|
|
396
|
+
}}
|
|
397
|
+
className={cn(
|
|
398
|
+
"rounded-md border px-1.5 py-0.5 font-mono text-2xs uppercase transition-colors",
|
|
399
|
+
on
|
|
400
|
+
? "border-transparent bg-secondary text-secondary-foreground"
|
|
401
|
+
: "border-input bg-background text-muted-foreground line-through",
|
|
402
|
+
focusRing,
|
|
403
|
+
)}
|
|
404
|
+
>
|
|
405
|
+
{level} {counts.get(level) ?? 0}
|
|
406
|
+
</button>
|
|
407
|
+
);
|
|
408
|
+
})}
|
|
409
|
+
</div>
|
|
410
|
+
|
|
411
|
+
<p className="w-full text-xs text-muted-foreground tabular-nums">
|
|
412
|
+
{invalidPattern ? (
|
|
413
|
+
// Said out loud rather than shown as an empty log, which reads as
|
|
414
|
+
// "nothing matched" and sends the reader looking for the wrong thing.
|
|
415
|
+
<span id={errorId} className="text-destructive">
|
|
416
|
+
Incomplete pattern — showing everything until it is valid
|
|
417
|
+
</span>
|
|
418
|
+
) : (
|
|
419
|
+
`${String(showing)} of ${String(total)} lines`
|
|
420
|
+
)}
|
|
421
|
+
</p>
|
|
422
|
+
</div>
|
|
423
|
+
);
|
|
424
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineMeta } from "@/registry/schema";
|
|
2
|
+
|
|
3
|
+
export const meta = defineMeta({
|
|
4
|
+
name: "log-viewer",
|
|
5
|
+
title: "Log Viewer",
|
|
6
|
+
description: "A streaming console with filtering, level facets and follow mode.",
|
|
7
|
+
category: "data",
|
|
8
|
+
status: "stable",
|
|
9
|
+
dependencies: ["@tanstack/react-virtual"],
|
|
10
|
+
registryDependencies: [],
|
|
11
|
+
files: ["log-stream.ts", "log-viewer.tsx"],
|
|
12
|
+
a11y:
|
|
13
|
+
'role="log" implies aria-live="polite", which is right for a few events and unusable for a ' +
|
|
14
|
+
"console — a screen reader would read every line of a build and nothing else would be " +
|
|
15
|
+
"audible. Announcing is off by default and opt-in. Virtualization is the harder trade: most " +
|
|
16
|
+
"rows are not in the DOM, so assistive technology cannot reach them, which is why the " +
|
|
17
|
+
"component takes an onDownload escape rather than pretending the virtual window is the whole " +
|
|
18
|
+
"log. Levels are written in text as well as coloured, matches use mark elements rather than " +
|
|
19
|
+
"a background colour, and an invalid pattern is announced instead of silently showing an " +
|
|
20
|
+
'empty log that reads as "nothing matched".',
|
|
21
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export {
|
|
2
|
+
TimeRange,
|
|
3
|
+
TimeRangeCalendar,
|
|
4
|
+
TimeRangeContent,
|
|
5
|
+
TimeRangeExpression,
|
|
6
|
+
TimeRangePresets,
|
|
7
|
+
TimeRangeTrigger,
|
|
8
|
+
type TimeRangeContentProps,
|
|
9
|
+
type TimeRangeProps,
|
|
10
|
+
type TimeRangeTriggerProps,
|
|
11
|
+
} from "./time-range-picker";
|
|
12
|
+
export {
|
|
13
|
+
DEFAULT_PRESETS,
|
|
14
|
+
TimeExpressionError,
|
|
15
|
+
absoluteExpression,
|
|
16
|
+
describeTimeRange,
|
|
17
|
+
formatResolvedRange,
|
|
18
|
+
isValidTimeRange,
|
|
19
|
+
resolveExpression,
|
|
20
|
+
resolveTimeRange,
|
|
21
|
+
type ResolveOptions,
|
|
22
|
+
type ResolvedRange,
|
|
23
|
+
type TimeRangePreset,
|
|
24
|
+
type TimeUnit,
|
|
25
|
+
} from "./time-expression";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defineMeta } from "@/registry/schema";
|
|
2
|
+
|
|
3
|
+
export const meta = defineMeta({
|
|
4
|
+
name: "time-range-picker",
|
|
5
|
+
title: "Time Range Picker",
|
|
6
|
+
description:
|
|
7
|
+
"A time range whose value is an expression, so it stays relative across a reload.",
|
|
8
|
+
category: "form",
|
|
9
|
+
status: "stable",
|
|
10
|
+
dependencies: ["react-day-picker"],
|
|
11
|
+
registryDependencies: ["button", "calendar", "input", "popover"],
|
|
12
|
+
files: ["time-expression.ts", "time-range-picker.tsx"],
|
|
13
|
+
a11y:
|
|
14
|
+
'The trigger is named by the range it holds — "Last 6 hours" — and the window that ' +
|
|
15
|
+
"resolves to is read after it, so the relative choice is not replaced by two timestamps. " +
|
|
16
|
+
'The popover carries role="dialog" and is named. Presets are buttons rather than radios ' +
|
|
17
|
+
"because choosing one both selects and dismisses, with aria-pressed carrying which matches " +
|
|
18
|
+
"the current expression. The expression field reports why an unparseable entry is invalid " +
|
|
19
|
+
"through one aria-describedby element that the preview and the error share, so a reader " +
|
|
20
|
+
"hears the error replace the preview rather than both at once; an invalid expression is " +
|
|
21
|
+
"never applied, because a chart silently re-scoping itself is worse than one that refuses.",
|
|
22
|
+
});
|