mirror of
https://github.com/ChiChou/entdb.git
synced 2026-12-05 11:13:40 +01:00
perf(find): CSS-only overflow button; stable tree props
- The … button on a value row shows only when chips wrapped out of sight, with no JavaScript: the chip box's scroll timeline is active only when it has something to scroll, and the button's visibility animation runs only then (browsers without scroll-driven animations always show it). Replaces the measured +N count and its ResizeObserver. - osFor and onPickValue are memoized, so typing in the filter no longer re-renders every row of the memoized tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4893844ce1
commit
b2bc9d64bf
3 files changed
+59
-117
No files matched your search
@@ -200,3 +200,31 @@
|
||||
.plist-code .linked-key a {
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
/*
|
||||
* A value row's … button shows only when some chips are hidden, in CSS: the
|
||||
* chip box (overflow: hidden, so a scroll container) has a scroll timeline,
|
||||
* which is active only when there is something to scroll, i.e. chips wrapped
|
||||
* onto the hidden second line. The animation that makes the button visible
|
||||
* runs only while that timeline is active. Browsers without scroll-driven
|
||||
* animations always show the button.
|
||||
*/
|
||||
@supports (animation-timeline: scroll()) {
|
||||
.value-row {
|
||||
timeline-scope: --value-chips;
|
||||
}
|
||||
.value-chips {
|
||||
scroll-timeline: --value-chips block;
|
||||
}
|
||||
.value-more {
|
||||
visibility: hidden;
|
||||
animation: value-more-show linear both;
|
||||
animation-timeline: --value-chips;
|
||||
}
|
||||
}
|
||||
@keyframes value-more-show {
|
||||
from,
|
||||
to {
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
+20
-12
@@ -201,13 +201,13 @@ export default function FindByKey() {
|
||||
);
|
||||
const hasValues = allValues.length > 0 && uniformValue === undefined;
|
||||
|
||||
const setSelectedAtoms = (atoms: string[]) => {
|
||||
const setSelectedAtoms = useCallback((atoms: string[]) => {
|
||||
setSelectedAtomsState(atoms);
|
||||
const next = new URLSearchParams(window.location.search);
|
||||
next.delete("v");
|
||||
for (const atom of atoms) next.append("v", atom);
|
||||
window.history.replaceState(null, "", `?${next.toString()}`);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const showChanges = changesOnly && diffReady;
|
||||
|
||||
@@ -282,6 +282,22 @@ export default function FindByKey() {
|
||||
? `${group}/${versionTag(activeCompare)}`
|
||||
: os;
|
||||
|
||||
// Stable props for the (memoized) tree: inline functions made every
|
||||
// render, e.g. each keystroke in the filter, re-render all of its rows.
|
||||
const osFor = useCallback(
|
||||
(path: string) => (statusOf(path) === "removed" ? compareOs : undefined),
|
||||
[compareOs, statusOf],
|
||||
);
|
||||
const pickValue = useCallback(
|
||||
(atom: string) =>
|
||||
setSelectedAtoms(
|
||||
selectedAtoms.includes(atom)
|
||||
? selectedAtoms.filter((a) => a !== atom)
|
||||
: [...selectedAtoms, atom],
|
||||
),
|
||||
[selectedAtoms, setSelectedAtoms],
|
||||
);
|
||||
|
||||
/** Keys page at namespace `ns`, keeping an active comparison. */
|
||||
const keysUrl = (ns: string) => {
|
||||
const query = new URLSearchParams({ os });
|
||||
@@ -608,18 +624,10 @@ export default function FindByKey() {
|
||||
list={list}
|
||||
expandAll={expandAll}
|
||||
status={diffReady ? statusOf : undefined}
|
||||
osFor={(path) =>
|
||||
statusOf(path) === "removed" ? compareOs : undefined
|
||||
}
|
||||
osFor={osFor}
|
||||
valueFor={hasValues ? valueFor : undefined}
|
||||
highlight={selectedAtoms}
|
||||
onPickValue={(atom) =>
|
||||
setSelectedAtoms(
|
||||
selectedAtoms.includes(atom)
|
||||
? selectedAtoms.filter((a) => a !== atom)
|
||||
: [...selectedAtoms, atom],
|
||||
)
|
||||
}
|
||||
onPickValue={pickValue}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useLayoutEffect, useRef, useState } from "react";
|
||||
import { MoreHorizontal } from "lucide-react";
|
||||
|
||||
import {
|
||||
Popover,
|
||||
@@ -14,74 +14,6 @@ const TITLE_LIMIT = 4000;
|
||||
|
||||
const collator = new Intl.Collator(undefined, { sensitivity: "base" });
|
||||
|
||||
/**
|
||||
* Overflow counting (+N), batched across every row. A row's chips wrap onto
|
||||
* a hidden second line; after layout we count those and end the chip box at
|
||||
* the last visible chip so +N follows it. Done per row, each measure (reset
|
||||
* width, read, set width) forced its own layout: ~900 layouts per resize or
|
||||
* filter change. Here all rows reset, then all read, then all write, in one
|
||||
* frame: one forced layout for the whole tree.
|
||||
*/
|
||||
interface Target {
|
||||
box: HTMLElement;
|
||||
setHidden: (hidden: number) => void;
|
||||
}
|
||||
|
||||
/** Room kept after the last visible chip for the +N button. */
|
||||
const MORE_RESERVE = 48;
|
||||
|
||||
const targets = new Map<Element, Target>();
|
||||
let pending: Set<Target> | null = null;
|
||||
let observer: ResizeObserver | null = null;
|
||||
|
||||
function measureAll() {
|
||||
const batch = pending ? [...pending] : [];
|
||||
pending = null;
|
||||
for (const { box } of batch) box.style.width = "";
|
||||
const results = batch.map(({ box }) => {
|
||||
const items = [...box.children];
|
||||
if (items.length < 2) return null;
|
||||
const rects = items.map((el) => el.getBoundingClientRect());
|
||||
const top = rects[0].top;
|
||||
if (rects.every((r) => r.top === top)) return null;
|
||||
// Some chips wrapped: keep those that fit with room for +N.
|
||||
const limit = box.getBoundingClientRect().right - MORE_RESERVE;
|
||||
const visible = Math.max(
|
||||
1,
|
||||
rects.findIndex((r) => r.top !== top || r.right > limit),
|
||||
);
|
||||
const left = box.getBoundingClientRect().left;
|
||||
return {
|
||||
hidden: items.length - visible,
|
||||
width: Math.ceil(rects[visible - 1].right - left),
|
||||
};
|
||||
});
|
||||
batch.forEach(({ box, setHidden }, i) => {
|
||||
const result = results[i];
|
||||
box.style.width = result ? `${result.width}px` : "";
|
||||
setHidden(result?.hidden ?? 0);
|
||||
});
|
||||
}
|
||||
|
||||
function schedule(target: Target) {
|
||||
if (!pending) {
|
||||
pending = new Set();
|
||||
requestAnimationFrame(measureAll);
|
||||
}
|
||||
pending.add(target);
|
||||
}
|
||||
|
||||
/** One observer for every row; its callback batches all resized rows. */
|
||||
function sharedObserver() {
|
||||
observer ??= new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const target = targets.get(entry.target);
|
||||
if (target) schedule(target);
|
||||
}
|
||||
});
|
||||
return observer;
|
||||
}
|
||||
|
||||
function titleFor(value: unknown) {
|
||||
const json = JSON.stringify(value, null, 2);
|
||||
return json.length > TITLE_LIMIT ? `${json.slice(0, TITLE_LIMIT)}\n…` : json;
|
||||
@@ -148,32 +80,7 @@ export function ValuePreview({
|
||||
let body: React.ReactNode;
|
||||
/** Every item, for the overflow menu. */
|
||||
let all: React.ReactNode = null;
|
||||
let count: number | null = null;
|
||||
const outer = useRef<HTMLSpanElement>(null);
|
||||
const chips = useRef<HTMLSpanElement>(null);
|
||||
// Chips wrapped onto the hidden second line, counted after layout.
|
||||
const [hidden, setHidden] = useState(0);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const el = outer.current;
|
||||
const box = chips.current;
|
||||
if (!el || !box) return;
|
||||
const target: Target = { box, setHidden };
|
||||
targets.set(el, target);
|
||||
sharedObserver().observe(el);
|
||||
return () => {
|
||||
targets.delete(el);
|
||||
observer?.unobserve(el);
|
||||
pending?.delete(target);
|
||||
};
|
||||
}, []);
|
||||
// The chips changed (value, or filter hits moving first): re-measure.
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: triggers only
|
||||
useLayoutEffect(() => {
|
||||
const target = outer.current && targets.get(outer.current);
|
||||
if (target) schedule(target);
|
||||
}, [value, highlight]);
|
||||
|
||||
let count = 1;
|
||||
if (isList || isDict) {
|
||||
// Nested arrays/dicts show as JSON and aren't atoms, so not pickable.
|
||||
const items = isList
|
||||
@@ -213,35 +120,34 @@ export function ValuePreview({
|
||||
|
||||
return (
|
||||
<span
|
||||
ref={outer}
|
||||
className={cn(
|
||||
"flex min-w-0 items-center gap-1 font-mono text-xs text-muted-foreground",
|
||||
"value-row flex min-w-0 items-center gap-1 font-mono text-xs text-muted-foreground",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{/* Chips that don't fit wrap onto a hidden second line, so none is
|
||||
cut mid-word; the +N menu right after the last one lists them all. */}
|
||||
cut mid-word. The … button lists them all; CSS shows it only when
|
||||
some are hidden (see .value-more in globals.css). */}
|
||||
<span
|
||||
ref={chips}
|
||||
className="flex h-5 min-w-0 flex-wrap items-center gap-1 overflow-hidden"
|
||||
className="value-chips flex h-5 min-w-0 flex-wrap items-center gap-1 overflow-hidden"
|
||||
title={titleFor(value)}
|
||||
>
|
||||
{body}
|
||||
</span>
|
||||
{count !== null && hidden > 0 && (
|
||||
{count > 1 && (
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex h-5 shrink-0 items-center rounded px-1.5 tabular-nums text-muted-foreground ring-1 ring-inset ring-border transition-colors hover:bg-accent hover:text-foreground"
|
||||
className="value-more inline-flex h-5 shrink-0 items-center rounded px-1 text-muted-foreground ring-1 ring-inset ring-border transition-colors hover:bg-accent hover:text-foreground"
|
||||
aria-label={`Show all ${count} ${noun}`}
|
||||
title={`${hidden} more of ${count} ${noun}`}
|
||||
title={`Show all ${count} ${noun}`}
|
||||
>
|
||||
+{hidden}
|
||||
<MoreHorizontal className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
align="start"
|
||||
align="end"
|
||||
className="max-h-80 w-auto max-w-[min(36rem,90vw)] overflow-y-auto p-2 font-mono text-xs"
|
||||
>
|
||||
<p className="mb-1.5 px-0.5 font-sans text-muted-foreground">
|
||||
|
||||
Reference in new issue
Block a user