@grimoire-rs/indexer 0.5.1 → 0.5.3
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/CHANGELOG.md +127 -1
- package/dist/renderer/astro/components/CardLogo.d.ts +2 -2
- package/dist/renderer/astro/components/CardLogo.tsx +2 -2
- package/dist/renderer/astro/components/Catalog.d.ts +12 -4
- package/dist/renderer/astro/components/Catalog.js +356 -88
- package/dist/renderer/astro/components/Catalog.tsx +442 -108
- package/dist/renderer/astro/components/PackageCard.d.ts +2 -2
- package/dist/renderer/astro/components/PackageCard.js +1 -1
- package/dist/renderer/astro/components/PackageCard.tsx +11 -3
- package/dist/renderer/astro/components/PackageRow.d.ts +2 -2
- package/dist/renderer/astro/components/PackageRow.tsx +2 -2
- package/dist/renderer/astro/layouts/Base.astro +145 -51
- package/dist/renderer/astro/lib/catalog.d.ts +31 -1
- package/dist/renderer/astro/lib/catalog.js +32 -0
- package/dist/renderer/astro/lib/catalog.ts +73 -1
- package/dist/renderer/astro/lib/search.d.ts +17 -0
- package/dist/renderer/astro/lib/search.js +138 -0
- package/dist/renderer/astro/lib/search.ts +157 -0
- package/dist/renderer/astro/pages/index.astro +45 -2
- package/dist/renderer/astro/styles/tokens.css +6 -1
- package/dist/renderer/index.d.ts.map +1 -1
- package/dist/renderer/index.js +52 -0
- package/dist/renderer/index.js.map +1 -1
- package/package.json +6 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime";
|
|
2
|
-
import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from "preact/hooks";
|
|
2
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from "preact/hooks";
|
|
3
3
|
// Lucide (ISC) draws the toolbar. The brand marks and kind glyphs moved out
|
|
4
4
|
// with the card and the row that wear them.
|
|
5
5
|
import { ArrowDownWideNarrow, ArrowUpNarrowWide, LayoutGrid, List, X, } from "lucide-preact";
|
|
@@ -7,6 +7,7 @@ import { PackageCard } from "./PackageCard.js";
|
|
|
7
7
|
import { PackageRow } from "./PackageRow.js";
|
|
8
8
|
import { keywordFrequency, selectRailKeywords } from "../lib/keywordRail.js";
|
|
9
9
|
import { lastUpdated } from "../lib/catalog.js";
|
|
10
|
+
import { withBase } from "../lib/base.js";
|
|
10
11
|
// Known kinds get stable chip ordering + badge colors; unknown kinds
|
|
11
12
|
// (future schema growth) still render with a neutral badge.
|
|
12
13
|
const KNOWN_KINDS = ["skill", "rule", "agent", "mcp", "bundle"];
|
|
@@ -22,6 +23,10 @@ function kindOrder(kind) {
|
|
|
22
23
|
* all of it is a wall of chips nobody reads.
|
|
23
24
|
*/
|
|
24
25
|
const KEYWORD_CHIP_LIMIT = 8;
|
|
26
|
+
/**
|
|
27
|
+
* How many packages the catalog builds at a time. See `limit` in `Catalog`.
|
|
28
|
+
*/
|
|
29
|
+
const WINDOW = 48;
|
|
25
30
|
/**
|
|
26
31
|
* `popovertarget` needs an id, and the catalog is a singleton on its page —
|
|
27
32
|
* one island, one toolbar, one overflow menu — so this is a constant rather
|
|
@@ -41,6 +46,7 @@ export const NATURAL = {
|
|
|
41
46
|
name: "asc",
|
|
42
47
|
updated: "desc",
|
|
43
48
|
rating: "desc",
|
|
49
|
+
relevance: "desc",
|
|
44
50
|
};
|
|
45
51
|
/**
|
|
46
52
|
* Bigger first, with `null` as its own bucket underneath every number.
|
|
@@ -85,6 +91,16 @@ const CHAINS = {
|
|
|
85
91
|
name: [byName],
|
|
86
92
|
updated: [byUpdated, byName],
|
|
87
93
|
rating: [byRating, byUpdated, byName],
|
|
94
|
+
// Relevance cannot be a key here: a score belongs to a query, not to a
|
|
95
|
+
// package, so it is not on the record `compare` is handed. `shown` sorts
|
|
96
|
+
// that mode itself.
|
|
97
|
+
//
|
|
98
|
+
// This entry is what the mode falls back to whenever there are no scores —
|
|
99
|
+
// no query typed, or the fuzzy index still downloading — which is also why
|
|
100
|
+
// relevance can be offered and stored like any other mode. Alphabetical is
|
|
101
|
+
// the honest answer to "rank these against nothing", and it is what the
|
|
102
|
+
// catalog already shows on arrival.
|
|
103
|
+
relevance: [byName],
|
|
88
104
|
};
|
|
89
105
|
// Deprecated packages get no special ordering here — they are filtered out
|
|
90
106
|
// of the default browse entirely (see `shown` below) and interleave like
|
|
@@ -242,7 +258,13 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
242
258
|
// Local to the overflow menu and deliberately not shareable: it narrows
|
|
243
259
|
// the list of keywords, not the catalog.
|
|
244
260
|
const [keywordFilter, setKeywordFilter] = useState("");
|
|
261
|
+
// The fuzzy matcher, once it has been fetched. `null` is the normal state
|
|
262
|
+
// for most of a visit — see the loader below — and every consumer treats
|
|
263
|
+
// it as "fall back to the substring filter", never as an error.
|
|
264
|
+
const [index, setIndex] = useState(null);
|
|
245
265
|
const searchRef = useRef(null);
|
|
266
|
+
/** Whether the sort combo's last interaction came from a pointer. */
|
|
267
|
+
const pickedByPointer = useRef(false);
|
|
246
268
|
const gridRef = useRef(null);
|
|
247
269
|
const controlsRef = useRef(null);
|
|
248
270
|
// Both views, one selector: a table row is the Tab stop its card is, so
|
|
@@ -258,8 +280,40 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
258
280
|
...(controlsRef.current?.querySelectorAll('button.chip:not([aria-hidden="true"])') ?? []),
|
|
259
281
|
];
|
|
260
282
|
const railRefs = useRef(new Map());
|
|
261
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Where each chip sat at the last commit, as `offsetLeft`/`offsetTop`.
|
|
285
|
+
*
|
|
286
|
+
* Layout positions, deliberately, not `getBoundingClientRect`: the offsets
|
|
287
|
+
* ignore transforms and page scroll, so a chip measured mid-slide reports
|
|
288
|
+
* the seat it is animating towards rather than the box it is drawn in this
|
|
289
|
+
* frame. See the FLIP effect below for why that distinction is the whole
|
|
290
|
+
* fix.
|
|
291
|
+
*/
|
|
292
|
+
const railSeats = useRef(new Map());
|
|
293
|
+
/** The slide each chip is currently running, so a new one can replace it. */
|
|
294
|
+
const railSlides = useRef(new WeakMap());
|
|
262
295
|
const railRef = useRef(null);
|
|
296
|
+
/**
|
|
297
|
+
* Which chips are up, in order, as of the current render — written below,
|
|
298
|
+
* once `visibleKeywords` exists, and read by the two layout effects.
|
|
299
|
+
*
|
|
300
|
+
* Both of those effects measure geometry, and a geometry read forces a
|
|
301
|
+
* synchronous style and layout pass over the WHOLE document. Neither had a
|
|
302
|
+
* dependency array, so both ran on every commit — including a commit that
|
|
303
|
+
* changed no chip at all. Switching to the list view is that commit, and at
|
|
304
|
+
* 500 packages it paid two forced layouts over several hundred rows Preact
|
|
305
|
+
* had just mounted: a 600ms task, most of it in `ForcedStyleAndLayout`.
|
|
306
|
+
*
|
|
307
|
+
* A ref rather than a dependency array because the chip list is computed
|
|
308
|
+
* hundreds of lines below these hooks, and a value cannot be a dependency
|
|
309
|
+
* before it exists. Writing a ref during render is safe — it is not state,
|
|
310
|
+
* nothing re-renders from it, and an effect body always runs after the
|
|
311
|
+
* render that wrote it.
|
|
312
|
+
*/
|
|
313
|
+
const railSignature = useRef("");
|
|
314
|
+
/** What `railSignature` was when each effect last did its reads. */
|
|
315
|
+
const railMeasured = useRef(null);
|
|
316
|
+
const railFlipped = useRef(null);
|
|
263
317
|
const kwMenuRef = useRef(null);
|
|
264
318
|
const kwTriggerRef = useRef(null);
|
|
265
319
|
// Drives the trigger's own `aria-expanded` and its open styling. The panel's
|
|
@@ -330,38 +384,55 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
330
384
|
* disagrees with itself on every other frame.
|
|
331
385
|
*/
|
|
332
386
|
const [railFit, setRailFit] = useState(KEYWORD_CHIP_LIMIT);
|
|
333
|
-
|
|
387
|
+
const measureRail = useCallback(() => {
|
|
334
388
|
const rail = railRef.current;
|
|
335
389
|
if (!rail)
|
|
336
390
|
return;
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
391
|
+
const edge = rail.getBoundingClientRect().right;
|
|
392
|
+
let fits = 0;
|
|
393
|
+
for (const chip of rail.children) {
|
|
394
|
+
// Half a pixel of slack: a fractional layout can leave a chip's right
|
|
395
|
+
// edge a rounding error past a boundary it visually sits inside.
|
|
396
|
+
if (chip.getBoundingClientRect().right > edge + 0.5)
|
|
397
|
+
break;
|
|
398
|
+
fits += 1;
|
|
399
|
+
}
|
|
400
|
+
// At least one, always. A rail too narrow for its shortest chip should
|
|
401
|
+
// show that chip clipped rather than render an empty group beside a
|
|
402
|
+
// divider that then divides nothing.
|
|
403
|
+
setRailFit(Math.max(1, fits));
|
|
404
|
+
}, []);
|
|
405
|
+
/**
|
|
406
|
+
* The rail's own `ref`, so the resize observer is attached exactly when the
|
|
407
|
+
* element exists. It is conditionally rendered — no keywords, no rail — and
|
|
408
|
+
* a `[]`-dependency effect would miss it appearing later.
|
|
409
|
+
*
|
|
410
|
+
* This also stops the observer being torn down and rebuilt on every commit,
|
|
411
|
+
* which was not free: `observe()` delivers an initial callback, so a rebuild
|
|
412
|
+
* per commit meant a measurement per commit no matter what gated the effect.
|
|
413
|
+
*
|
|
414
|
+
* Guarded rather than assumed: this runs under the test renderer too, whose
|
|
415
|
+
* DOM has no `ResizeObserver` — and a missing one costs only re-measurement
|
|
416
|
+
* on viewport resize, which is not worth throwing during a render over.
|
|
417
|
+
*/
|
|
418
|
+
const railObserver = useRef(null);
|
|
419
|
+
const attachRail = useCallback((el) => {
|
|
420
|
+
railRef.current = el;
|
|
421
|
+
railObserver.current?.disconnect();
|
|
422
|
+
railObserver.current = null;
|
|
423
|
+
if (!el || typeof ResizeObserver === "undefined")
|
|
358
424
|
return;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
425
|
+
railObserver.current = new ResizeObserver(measureRail);
|
|
426
|
+
railObserver.current.observe(el);
|
|
427
|
+
}, [measureRail]);
|
|
428
|
+
// Re-measured when the chips change, and only then — a rescore can swap a
|
|
429
|
+
// short word for a long one at the same width, which the observer above
|
|
430
|
+
// would never see. See `railSignature` for why the guard is a ref.
|
|
431
|
+
useLayoutEffect(() => {
|
|
432
|
+
if (railSignature.current === railMeasured.current)
|
|
433
|
+
return;
|
|
434
|
+
railMeasured.current = railSignature.current;
|
|
435
|
+
measureRail();
|
|
365
436
|
});
|
|
366
437
|
/**
|
|
367
438
|
* FLIP for the keyword rail: chips slide to their new places instead of
|
|
@@ -373,71 +444,89 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
373
444
|
* between two frames reads as having been *replaced*, and a reader who
|
|
374
445
|
* cannot see that a chip moved has no reason to believe it is the same one.
|
|
375
446
|
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
447
|
+
* Two choices carry the whole thing, and both are here because the shape
|
|
448
|
+
* they replace — an inline `translate` under the stylesheet's transition,
|
|
449
|
+
* dropped again on a `requestAnimationFrame` — could leave a chip stopped
|
|
450
|
+
* off its seat with no path back:
|
|
451
|
+
*
|
|
452
|
+
* - **Seats come from `offsetLeft`/`offsetTop`, never from
|
|
453
|
+
* `getBoundingClientRect`.** The offsets are layout positions and ignore
|
|
454
|
+
* both transforms and scroll; a rect is where the chip is *drawn*, so a
|
|
455
|
+
* chip measured mid-slide recorded its animated box and the next
|
|
456
|
+
* inversion compounded that error — the stutter. Mid-slide is the common
|
|
457
|
+
* case, not a rare one: the fit measurement above commits a second time
|
|
458
|
+
* whenever a rescore changes how many chips fit, and that commit lands
|
|
459
|
+
* inside the previous slide. Because the offsets are transform-blind,
|
|
460
|
+
* that second commit now measures the same seats and starts nothing.
|
|
461
|
+
* - **The slide is a Web Animation, not an inline style.** It needs no
|
|
462
|
+
* frame to start, so no commit can land between an invert and its play
|
|
463
|
+
* and freeze a chip at the offset. It writes nothing to `style`, so
|
|
464
|
+
* there is nothing left to strip when a chip unmounts or a pass is
|
|
465
|
+
* superseded. And it clears itself the instant it finishes or is
|
|
466
|
+
* cancelled, which makes "the rail always ends up in its real state" a
|
|
467
|
+
* property of the mechanism rather than of a cleanup remembering to run.
|
|
382
468
|
*/
|
|
383
469
|
useLayoutEffect(() => {
|
|
384
|
-
//
|
|
385
|
-
//
|
|
386
|
-
// reports the *translated* box, so a chip caught mid-slide would be
|
|
387
|
-
// measured where it is drawn rather than where it belongs and the next
|
|
388
|
-
// inversion would compound that error; and a chip whose play frame never
|
|
389
|
-
// ran is still carrying `transition: none` with an offset, which is a chip
|
|
390
|
-
// frozen off its seat. Clearing here is what unfreezes it.
|
|
470
|
+
// Same guard as the fit measurement, for the same reason: `offsetLeft`
|
|
471
|
+
// forces layout, and a commit that moved no chip has nothing to animate.
|
|
391
472
|
//
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
//
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
473
|
+
// The seats it leaves behind are therefore from the last chip change
|
|
474
|
+
// rather than from the last commit, which is what FLIP wants. The one
|
|
475
|
+
// case that costs: a viewport resize between two chip changes moves the
|
|
476
|
+
// chips without changing the signature, so the next slide starts from
|
|
477
|
+
// pre-resize seats. One slightly-off slide after a resize, against two
|
|
478
|
+
// forced layouts on every commit — including every keystroke and every
|
|
479
|
+
// view switch — which is the trade this makes.
|
|
480
|
+
if (railSignature.current === railFlipped.current)
|
|
481
|
+
return;
|
|
482
|
+
railFlipped.current = railSignature.current;
|
|
483
|
+
const previous = railSeats.current;
|
|
400
484
|
const current = new Map();
|
|
401
485
|
const moved = [];
|
|
402
|
-
// A second pass, deliberately: every write above is flushed before the
|
|
403
|
-
// first read below, rather than interleaving them per chip.
|
|
404
486
|
for (const [keyword, el] of railRefs.current) {
|
|
405
|
-
const
|
|
406
|
-
|
|
487
|
+
const x = el.offsetLeft;
|
|
488
|
+
const y = el.offsetTop;
|
|
489
|
+
current.set(keyword, { x, y });
|
|
407
490
|
const was = previous.get(keyword);
|
|
408
491
|
if (!was)
|
|
409
492
|
continue;
|
|
410
|
-
const dx = was.
|
|
411
|
-
const dy = was.
|
|
493
|
+
const dx = was.x - x;
|
|
494
|
+
const dy = was.y - y;
|
|
412
495
|
if (dx !== 0 || dy !== 0)
|
|
413
496
|
moved.push({ el, dx, dy });
|
|
414
497
|
}
|
|
415
|
-
|
|
498
|
+
// Rebuilt from `railRefs` every pass, so a chip that left the rail leaves
|
|
499
|
+
// this map with it and cannot seed a slide if it comes back elsewhere.
|
|
500
|
+
railSeats.current = current;
|
|
416
501
|
if (moved.length === 0)
|
|
417
502
|
return;
|
|
503
|
+
const rail = railRef.current;
|
|
504
|
+
// Guarded rather than assumed, like the fit observer above: the test
|
|
505
|
+
// renderer's DOM has no Web Animations API, and a rail that does not
|
|
506
|
+
// slide is not worth throwing during a render over.
|
|
507
|
+
if (!rail || typeof rail.animate !== "function")
|
|
508
|
+
return;
|
|
509
|
+
// The motion is the ornament here — the filtering works identically
|
|
510
|
+
// without it — so a reader who asked for less of it gets none.
|
|
511
|
+
if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches)
|
|
512
|
+
return;
|
|
513
|
+
// The stylesheet stays the source of truth for how long a slide runs:
|
|
514
|
+
// `--grim-duration-slow` is written in milliseconds and that is the unit
|
|
515
|
+
// the Web Animations API takes, so the token survives the move out of CSS.
|
|
516
|
+
// Read once, after every seat above, so the reads and the writes below
|
|
517
|
+
// stay in two passes rather than interleaving per chip.
|
|
518
|
+
const ms = Number.parseFloat(getComputedStyle(rail).getPropertyValue("--grim-duration-slow"));
|
|
418
519
|
for (const { el, dx, dy } of moved) {
|
|
419
|
-
|
|
420
|
-
|
|
520
|
+
// One slide per chip. A chip that moves again mid-slide takes the new
|
|
521
|
+
// delta from its layout seat, which starts the second slide a step off
|
|
522
|
+
// where the first had drawn it — a jump measured in the pixels one
|
|
523
|
+
// frame of easing covers, and it is bounded, unlike two animations
|
|
524
|
+
// compositing the same property against each other.
|
|
525
|
+
// ponytail: not blended with the in-flight offset, which would cost a
|
|
526
|
+
// computed-style read per chip in the middle of the write pass.
|
|
527
|
+
railSlides.current.get(el)?.cancel();
|
|
528
|
+
railSlides.current.set(el, el.animate({ translate: [`${dx}px ${dy}px`, "none"] }, { duration: Number.isFinite(ms) ? ms : 200, easing: "ease-out" }));
|
|
421
529
|
}
|
|
422
|
-
const frame = requestAnimationFrame(() => {
|
|
423
|
-
for (const { el } of moved) {
|
|
424
|
-
el.style.transition = "";
|
|
425
|
-
el.style.translate = "";
|
|
426
|
-
}
|
|
427
|
-
});
|
|
428
|
-
return () => {
|
|
429
|
-
cancelAnimationFrame(frame);
|
|
430
|
-
// Cancelling is not enough on its own. Nothing else takes these off, so
|
|
431
|
-
// a commit landing before the frame ran would leave every moved chip
|
|
432
|
-
// sitting at its inverted offset with transitions disabled — the rail
|
|
433
|
-
// stopping halfway and staying there. Deselecting the last keyword is
|
|
434
|
-
// the reliable way to see it: the rescore is at its largest, so the fit
|
|
435
|
-
// changes and the extra commit always lands.
|
|
436
|
-
for (const { el } of moved) {
|
|
437
|
-
el.style.transition = "";
|
|
438
|
-
el.style.translate = "";
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
530
|
});
|
|
442
531
|
/** Move focus `delta` cards along, clamping at both ends rather than wrapping. */
|
|
443
532
|
const focusCard = (from, delta) => {
|
|
@@ -490,7 +579,7 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
490
579
|
const s = readPref("sort");
|
|
491
580
|
const d = readPref("dir");
|
|
492
581
|
const v = readPref("view");
|
|
493
|
-
const field = s === "updated" || s === "rating" ? s : "name";
|
|
582
|
+
const field = s === "updated" || s === "rating" || s === "relevance" ? s : "name";
|
|
494
583
|
const published = new Set(packages.flatMap((p) => p.keywords ?? []));
|
|
495
584
|
setQuery(params.get("q") ?? "");
|
|
496
585
|
setKinds(list(params.get("kind")).filter((k) => KNOWN_KINDS.includes(k)));
|
|
@@ -765,16 +854,71 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
765
854
|
return [...seen].sort((a, b) => kindOrder(a) - kindOrder(b) || a.localeCompare(b));
|
|
766
855
|
}, [counted]);
|
|
767
856
|
const q = query.trim().toLowerCase();
|
|
857
|
+
/**
|
|
858
|
+
* Fetch the fuzzy matcher, once, the first time anyone searches.
|
|
859
|
+
*
|
|
860
|
+
* Not on mount: a reader who never types pays nothing — no `fuzzysort`
|
|
861
|
+
* chunk, no `/all.json`. Not per keystroke either; `tried` latches on the
|
|
862
|
+
* first attempt, so a failed load degrades to the substring filter for the
|
|
863
|
+
* rest of the visit rather than re-fetching on every letter.
|
|
864
|
+
*
|
|
865
|
+
* A failure is not shown to the reader on purpose. Search keeps working —
|
|
866
|
+
* the substring path over the card's own fields is what the catalog did
|
|
867
|
+
* before this existed — so an error banner would report a downgrade nobody
|
|
868
|
+
* asked about, over a page that is doing what they asked. It goes to the
|
|
869
|
+
* console with the error value itself, chain and stack intact.
|
|
870
|
+
*/
|
|
871
|
+
const tried = useRef(false);
|
|
872
|
+
useEffect(() => {
|
|
873
|
+
if (!q || tried.current)
|
|
874
|
+
return;
|
|
875
|
+
tried.current = true;
|
|
876
|
+
// Self-catching, so the `void` attaches nothing it needs to: every await
|
|
877
|
+
// on the path is inside the try.
|
|
878
|
+
void (async () => {
|
|
879
|
+
try {
|
|
880
|
+
const { loadSearchIndex } = await import("../lib/search.js");
|
|
881
|
+
setIndex(await loadSearchIndex(withBase("/all.json")));
|
|
882
|
+
}
|
|
883
|
+
catch (err) {
|
|
884
|
+
console.error("catalog: fuzzy search unavailable, using substring match", err);
|
|
885
|
+
}
|
|
886
|
+
})();
|
|
887
|
+
}, [q]);
|
|
888
|
+
/**
|
|
889
|
+
* What the current query scored against every package, or `null` when
|
|
890
|
+
* there is no query or no matcher yet.
|
|
891
|
+
*
|
|
892
|
+
* Memoized on the pair: re-scoring the whole catalog is the one genuinely
|
|
893
|
+
* expensive thing a keystroke triggers, and every consumer below reads it.
|
|
894
|
+
*/
|
|
895
|
+
const scores = useMemo(() => (index && q ? index.search(q) : null), [index, q]);
|
|
768
896
|
// Query and facets first, deprecation last — so the toggle can report how
|
|
769
897
|
// many entries *it alone* is holding back, rather than a catalog-wide
|
|
770
898
|
// number that has nothing to do with what is on screen.
|
|
771
|
-
|
|
899
|
+
//
|
|
900
|
+
// Memoized, and `shown` with it, for a reason beyond the scan's own cost:
|
|
901
|
+
// an unmemoized `.filter().sort()` yields a NEW array on every render, so
|
|
902
|
+
// anything downstream keyed on `shown` — the keyword rail's set-cover below
|
|
903
|
+
// — could never hit its own cache. Both had to move together or neither
|
|
904
|
+
// helped. Every dependency here is a primitive or a state array, so the
|
|
905
|
+
// identity is stable exactly when the answer is.
|
|
906
|
+
const matching = useMemo(() => packages.filter((p) => {
|
|
772
907
|
if (kinds.length > 0 && !kinds.includes(p.kind))
|
|
773
908
|
return false;
|
|
774
909
|
if (!keywords.every((kw) => p.keywords?.includes(kw)))
|
|
775
910
|
return false;
|
|
776
911
|
if (!q)
|
|
777
912
|
return true;
|
|
913
|
+
// The fuzzy index, once it is here: multi-term, order-independent,
|
|
914
|
+
// typo-tolerant, and over every field `/all.json` carries — the
|
|
915
|
+
// licence, the vendor, the repository, the authors, none of which
|
|
916
|
+
// are on the record this island was handed.
|
|
917
|
+
if (scores)
|
|
918
|
+
return scores.has(p.ref);
|
|
919
|
+
// Until then, and if the fetch never lands: the substring pass over
|
|
920
|
+
// the fields the card itself ships. Narrower on both axes, and the
|
|
921
|
+
// reason the search box is never dead while a chunk downloads.
|
|
778
922
|
return [
|
|
779
923
|
p.name,
|
|
780
924
|
p.description ?? "",
|
|
@@ -784,8 +928,82 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
784
928
|
p.summary ?? "",
|
|
785
929
|
(p.keywords ?? []).join(" "),
|
|
786
930
|
].some((field) => field.toLowerCase().includes(q));
|
|
787
|
-
});
|
|
788
|
-
const shown = (
|
|
931
|
+
}), [packages, kinds, keywords, q, scores]);
|
|
932
|
+
const shown = useMemo(() => {
|
|
933
|
+
// `.filter()` already returns a fresh array, so sorting in place here
|
|
934
|
+
// mutates nothing the memo above holds — except in the `showDeprecated`
|
|
935
|
+
// branch, where `matching` IS that array. Copy before sorting.
|
|
936
|
+
const list = showDeprecated
|
|
937
|
+
? [...matching]
|
|
938
|
+
: matching.filter((p) => !p.deprecated);
|
|
939
|
+
// Relevance is sorted here rather than in `compare`, because the score is
|
|
940
|
+
// a property of the query and not of the package — see `CHAINS`. Name
|
|
941
|
+
// breaks the tie, so equally-scored packages keep a total order and the
|
|
942
|
+
// list cannot reshuffle between renders.
|
|
943
|
+
if (sort === "relevance" && scores) {
|
|
944
|
+
return list.sort((a, b) => {
|
|
945
|
+
const d = descending(scores.get(a.ref) ?? null, scores.get(b.ref) ?? null) ||
|
|
946
|
+
byName(a, b);
|
|
947
|
+
return dir === NATURAL.relevance ? d : -d;
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
return list.sort((a, b) => compare(a, b, sort, dir));
|
|
951
|
+
}, [matching, showDeprecated, sort, dir, scores]);
|
|
952
|
+
/**
|
|
953
|
+
* How many of `shown` are actually built.
|
|
954
|
+
*
|
|
955
|
+
* The island's cost is dominated by constructing components, not by drawing
|
|
956
|
+
* them: `content-visibility` on the card and the row already means the
|
|
957
|
+
* browser skips layout and paint for anything off screen, but Preact still
|
|
958
|
+
* built every one. At 500 packages that was ~440 card components on
|
|
959
|
+
* hydration and another ~440 row components the moment the view changed —
|
|
960
|
+
* a 1.9s wait before a stored list view was on screen at all.
|
|
961
|
+
*
|
|
962
|
+
* So only a viewport's worth is built, and the slice GROWS as a sentinel
|
|
963
|
+
* below the list comes into view. It never shrinks, which is the whole
|
|
964
|
+
* reason this is a slice rather than true virtualization: an item that has
|
|
965
|
+
* been built stays built, so scrolling back up can never meet a blank row,
|
|
966
|
+
* and find-in-page keeps working over everything reached so far. The
|
|
967
|
+
* worst case — a reader who scrolls to the bottom — is exactly today's
|
|
968
|
+
* behaviour, and its paint is still bounded by `content-visibility`.
|
|
969
|
+
*
|
|
970
|
+
* 48 covers a tall viewport of either shape with room over: rows are 34px,
|
|
971
|
+
* and the card grid is three or four across at 19rem minimum.
|
|
972
|
+
*/
|
|
973
|
+
const [limit, setLimit] = useState(WINDOW);
|
|
974
|
+
// A new result set starts a new window — otherwise narrowing to 3 matches
|
|
975
|
+
// and clearing the filter again would leave the whole catalog built.
|
|
976
|
+
// `shown` is memoized, so this identity changes exactly when the answer does.
|
|
977
|
+
const shownRef = useRef(shown);
|
|
978
|
+
if (shownRef.current !== shown) {
|
|
979
|
+
shownRef.current = shown;
|
|
980
|
+
if (limit !== WINDOW)
|
|
981
|
+
setLimit(WINDOW);
|
|
982
|
+
}
|
|
983
|
+
const visible = limit >= shown.length ? shown : shown.slice(0, limit);
|
|
984
|
+
/**
|
|
985
|
+
* Grow the window when the sentinel below the list is reached.
|
|
986
|
+
*
|
|
987
|
+
* `rootMargin` is what keeps this invisible in use: the next slice is built
|
|
988
|
+
* a screen and a half before the reader gets to it, so the list reads as
|
|
989
|
+
* complete rather than as something that loads while you look at it. The
|
|
990
|
+
* observer re-fires while the sentinel stays in view, so a fast scroll
|
|
991
|
+
* keeps growing the window a slice per frame rather than stalling.
|
|
992
|
+
*/
|
|
993
|
+
const sentinelRef = useRef(null);
|
|
994
|
+
useEffect(() => {
|
|
995
|
+
const el = sentinelRef.current;
|
|
996
|
+
// Guarded like the rail's observer: the test renderer's DOM has neither.
|
|
997
|
+
if (!el || typeof IntersectionObserver === "undefined")
|
|
998
|
+
return;
|
|
999
|
+
const io = new IntersectionObserver((entries) => {
|
|
1000
|
+
if (!entries.some((e) => e.isIntersecting))
|
|
1001
|
+
return;
|
|
1002
|
+
setLimit((l) => (l >= shown.length ? l : l + WINDOW));
|
|
1003
|
+
}, { rootMargin: "150% 0px" });
|
|
1004
|
+
io.observe(el);
|
|
1005
|
+
return () => io.disconnect();
|
|
1006
|
+
}, [shown.length]);
|
|
789
1007
|
/**
|
|
790
1008
|
* The keyword rail, over what is on screen rather than over the catalog.
|
|
791
1009
|
*
|
|
@@ -809,7 +1027,15 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
809
1027
|
keyword,
|
|
810
1028
|
count: shown.length,
|
|
811
1029
|
}));
|
|
812
|
-
|
|
1030
|
+
// Memoized on `shown` alone, because that is the only thing the scan reads.
|
|
1031
|
+
// `selectRailKeywords` is a greedy set-cover over every keyword of every
|
|
1032
|
+
// shown package — it scales with catalog size, and unmemoized it ran on
|
|
1033
|
+
// EVERY render: each keystroke in the search box, each chip click, each
|
|
1034
|
+
// view toggle, and once more for every re-render none of those caused. At a
|
|
1035
|
+
// corporate-sized catalog that is the most expensive thing in the render
|
|
1036
|
+
// path, repeated for an answer that had not changed.
|
|
1037
|
+
const scored = useMemo(() => selectRailKeywords(shown, KEYWORD_CHIP_LIMIT), [shown]);
|
|
1038
|
+
const rail = scored
|
|
813
1039
|
// `selectRailKeywords` scores the actives like any other keyword, so
|
|
814
1040
|
// over-request and drop them rather than spend rail slots twice.
|
|
815
1041
|
.filter((k) => !keywords.includes(k.keyword))
|
|
@@ -821,6 +1047,10 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
821
1047
|
// chip clipped at the rail's edge is one the reader cannot reach anywhere
|
|
822
1048
|
// else, and a "+N more" that does not count it is lying about where it is.
|
|
823
1049
|
const clippedKeywords = visibleKeywords.slice(railFit).map((k) => k.keyword);
|
|
1050
|
+
// What the two layout effects above compare against. In render order, so a
|
|
1051
|
+
// reorder counts as a change — the FLIP effect exists to animate exactly
|
|
1052
|
+
// that. NUL-joined because a keyword may contain anything but that.
|
|
1053
|
+
railSignature.current = visibleKeywords.map((k) => k.keyword).join("\u0000");
|
|
824
1054
|
const menuKeywords = keywordFrequency(shown).filter((k) => clippedKeywords.includes(k.keyword) ||
|
|
825
1055
|
!visibleKeywords.some((v) => v.keyword === k.keyword));
|
|
826
1056
|
// Plain substring, not a fuzzy match: this searches a list the reader is
|
|
@@ -831,7 +1061,10 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
831
1061
|
// no ratings gets no rating chip for the same reason.
|
|
832
1062
|
const hasDeprecated = packages.some((p) => p.deprecated);
|
|
833
1063
|
const hasRatings = packages.some((p) => p.rating);
|
|
834
|
-
return (_jsxs("section", { class: "catalog", "data-slot": "catalog", children: [_jsxs("div", { class: "controls", "data-slot": "catalog-toolbar", ref: controlsRef, children: [_jsxs("div", { class: "search-field", "data-slot": "catalog-search", children: [_jsx("input", { ref: searchRef, type: "search", placeholder: "Search packages \u2014 name, keyword, description\u2026", value: query, onInput: (e) => setQuery(e.target.value), onKeyDown: onSearchKeyDown, "aria-label": "Search packages", "aria-keyshortcuts": "/" }), _jsx("kbd", { class: "search-hint", "aria-hidden": "true", children: "/" })
|
|
1064
|
+
return (_jsxs("section", { class: "catalog", "data-slot": "catalog", children: [_jsxs("div", { class: "controls", "data-slot": "catalog-toolbar", ref: controlsRef, children: [_jsxs("div", { class: "search-field", "data-slot": "catalog-search", children: [_jsx("input", { ref: searchRef, type: "search", placeholder: "Search packages \u2014 name, keyword, description\u2026", value: query, onInput: (e) => setQuery(e.target.value), onKeyDown: onSearchKeyDown, "aria-label": "Search packages", "aria-keyshortcuts": "/" }), _jsx("kbd", { class: "search-hint", "aria-hidden": "true", children: "/" }), query && (_jsx("button", { type: "button", class: "search-clear", "aria-label": "Clear search", title: "Clear search", onClick: () => {
|
|
1065
|
+
setQuery("");
|
|
1066
|
+
searchRef.current?.focus();
|
|
1067
|
+
}, children: _jsx(X, { size: 12, "aria-hidden": "true" }) }))] }), _jsxs("div", { class: "filter-row", children: [_jsxs("div", { class: "chips kind-chips", role: "group", "aria-label": "Filter by kind", children: [_jsx("button", { type: "button", class: kinds.length === 0 ? "chip active" : "chip", "data-slot": "filter-chip", "aria-pressed": kinds.length === 0, onKeyDown: onChipKeyDown, onClick: () => setKinds([]), children: "all" }), kindNames.map((k) => (_jsx("button", { type: "button", class: kinds.includes(k) ? `chip active kind-${k}` : `chip kind-${k}`, "data-slot": "filter-chip", "aria-pressed": kinds.includes(k), onKeyDown: onChipKeyDown, onClick: () => toggleKind(k), children: k }, k)))] }), visibleKeywords.length > 0 && (_jsxs(_Fragment, { children: [_jsx("span", { class: "filter-divider", "aria-hidden": "true" }), _jsx("div", { class: "chips kw-rail", role: "group", "aria-label": "Filter by keyword", ref: attachRail, children: visibleKeywords.map(({ keyword }, i) => {
|
|
835
1068
|
// Past the measured fit: still laid out, so the measurement
|
|
836
1069
|
// that decided this stays true on the next pass, but drawn
|
|
837
1070
|
// as nothing and out of reach. Removing it from the flow
|
|
@@ -884,7 +1117,39 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
884
1117
|
? "Ascending — click for descending"
|
|
885
1118
|
: "Descending — click for ascending", "aria-label": dir === "asc"
|
|
886
1119
|
? "Sorted ascending; sort descending"
|
|
887
|
-
: "Sorted descending; sort ascending", onClick: () => setDir((d) => (d === "asc" ? "desc" : "asc")), children: dir === "asc" ? (_jsx(ArrowUpNarrowWide, { size: 15, "aria-hidden": "true" })) : (_jsx(ArrowDownWideNarrow, { size: 15, "aria-hidden": "true" })) }), _jsxs("select", { class: "sort-field", "data-slot": "filter-chip", "aria-label": "Sort by", value: sort,
|
|
1120
|
+
: "Sorted descending; sort ascending", onClick: () => setDir((d) => (d === "asc" ? "desc" : "asc")), children: dir === "asc" ? (_jsx(ArrowUpNarrowWide, { size: 15, "aria-hidden": "true" })) : (_jsx(ArrowDownWideNarrow, { size: 15, "aria-hidden": "true" })) }), _jsxs("select", { class: "sort-field", "data-slot": "filter-chip", "aria-label": "Sort by", value: sort,
|
|
1121
|
+
// Chromium matches `:focus-visible` on a `<select>` after a
|
|
1122
|
+
// plain MOUSE click — a select accepts keyboard input, so the
|
|
1123
|
+
// engine treats every focus as keyboard focus. The repo's
|
|
1124
|
+
// `:focus-visible` convention therefore cannot keep the accent
|
|
1125
|
+
// ring off this one control, and CSS has nothing else to go on:
|
|
1126
|
+
// no selector distinguishes focus that arrived from a pointer.
|
|
1127
|
+
//
|
|
1128
|
+
// So the pointer marks itself. `data-pointer` suppresses the
|
|
1129
|
+
// ring for the whole pointer interaction — an open dropdown is
|
|
1130
|
+
// its own affordance and needs no second one around the closed
|
|
1131
|
+
// box behind it — and the pick then hands focus back, so
|
|
1132
|
+
// nothing is left lit beside the thin neutral chips.
|
|
1133
|
+
//
|
|
1134
|
+
// The keyboard path must do NEITHER. Arrow keys on a closed
|
|
1135
|
+
// select fire `change` per option, so blurring there would take
|
|
1136
|
+
// the control away mid-selection, and a keyboard reader is
|
|
1137
|
+
// exactly who the ring exists for. `onKeyDown` clears both, so
|
|
1138
|
+
// a reader who clicks once and later tabs back is a keyboard
|
|
1139
|
+
// reader again.
|
|
1140
|
+
//
|
|
1141
|
+
// Written to the node rather than to state: this fires while
|
|
1142
|
+
// the native dropdown is open, and a re-render of the element
|
|
1143
|
+
// holding it open is not worth the risk for a styling hint.
|
|
1144
|
+
onPointerDown: (event) => {
|
|
1145
|
+
pickedByPointer.current = true;
|
|
1146
|
+
event.currentTarget.dataset.pointer = "";
|
|
1147
|
+
}, onKeyDown: (event) => {
|
|
1148
|
+
pickedByPointer.current = false;
|
|
1149
|
+
delete event.currentTarget.dataset.pointer;
|
|
1150
|
+
}, onBlur: (event) => {
|
|
1151
|
+
delete event.currentTarget.dataset.pointer;
|
|
1152
|
+
}, onChange: (event) => {
|
|
888
1153
|
const next = event.currentTarget
|
|
889
1154
|
.value;
|
|
890
1155
|
setSort(next);
|
|
@@ -892,7 +1157,10 @@ export default function Catalog({ packages, vscodeExtension, }) {
|
|
|
892
1157
|
// the previous one over lands the reader on "oldest first"
|
|
893
1158
|
// because they had asked for Z→A a moment ago.
|
|
894
1159
|
setDir(NATURAL[next]);
|
|
895
|
-
|
|
1160
|
+
// The pointer path only: see the handlers above.
|
|
1161
|
+
if (pickedByPointer.current)
|
|
1162
|
+
event.currentTarget.blur();
|
|
1163
|
+
}, children: [_jsx("option", { value: "name", children: "name" }), _jsx("option", { value: "updated", children: "updated" }), hasRatings && _jsx("option", { value: "rating", children: "rating" }), _jsx("option", { value: "relevance", children: "relevance" })] })] }), _jsxs("div", { class: "view-toggle", role: "group", "aria-label": "Catalog view", children: [_jsx("button", { type: "button", class: view === "cards" ? "view-pick active" : "view-pick", "data-slot": "filter-chip", "aria-pressed": view === "cards", title: "Cards", "aria-label": "Show packages as cards", onClick: () => setView("cards"), children: _jsx(LayoutGrid, { size: 15, "aria-hidden": "true" }) }), _jsx("button", { type: "button", class: view === "table" ? "view-pick active" : "view-pick", "data-slot": "filter-chip", "aria-pressed": view === "table", title: "List", "aria-label": "Show packages as a list", onClick: () => setView("table"), children: _jsx(List, { size: 15, "aria-hidden": "true" }) })] })] })] }), shown.length === 0 ? (_jsx("p", { class: "empty", children: "No packages match." })) : view === "table" ? (_jsx(PackageTable, { packages: visible, hasRatings: hasRatings, onKeyDown: onCardKeyDown, rootRef: gridRef })) : (_jsx("ul", { class: "grid", ref: (el) => {
|
|
896
1164
|
gridRef.current = el;
|
|
897
|
-
}, children:
|
|
1165
|
+
}, children: visible.map((p) => (_jsx(PackageCard, { pkg: p, vscodeExtension: vscodeExtension, activeKeywords: keywords, onToggleKeyword: toggleKeyword, onKeyDown: onCardKeyDown }, `${p.namespace}/${p.name}`))) })), visible.length < shown.length && (_jsx("div", { ref: sentinelRef, "aria-hidden": "true" }))] }));
|
|
898
1166
|
}
|