mirror of
https://github.com/ChiChou/entdb.git
synced 2026-12-06 11:43:25 +01:00
feat(find): key header, version compare and a way back to keys
- The key is the page title with a copy button; the count reads "N binaries in <version>" - Compare with another version (?diff=): binaries that gained or lost the key are marked in the folder tree, with a Changed only toggle (?changes=1) - A breadcrumb (All keys > namespace) returns to the keys page; the crumb the user came from, and the back arrow, restore exactly where they were (namespace, filter, comparison, scroll) - Clicking a key while comparing opens its find page comparing against the same version - Where supported, the clicked key name morphs into the find page title (view transition; skipped for reduced motion and modified clicks) - Shared OS version helpers move to src/lib/os-version.ts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d9a7364f65
commit
942e2966ea
5 files changed
+700
-99
No files matched your search
+470
-72
@@ -1,22 +1,70 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useMemo } from "react";
|
||||
import { redirect, useSearchParams } from "next/navigation";
|
||||
import { Search, X, ChevronsUpDown, ChevronsDownUp } from "lucide-react";
|
||||
import {
|
||||
ArrowLeft,
|
||||
Check,
|
||||
ChevronRight,
|
||||
ChevronsDownUp,
|
||||
ChevronsUpDown,
|
||||
CircleMinus,
|
||||
CirclePlus,
|
||||
Copy,
|
||||
GitCompare,
|
||||
ListFilter,
|
||||
Search,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import FileSystem from "@/components/filesystem";
|
||||
import { BetaBadge } from "@/components/beta-badge";
|
||||
import FileSystem, { type PathStatus } from "@/components/filesystem";
|
||||
import { HeaderPortal } from "@/components/header-portal";
|
||||
import { createEngine } from "@/lib/engine";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { useQueryFilter } from "@/hooks/use-query-filter";
|
||||
import { createEngine } from "@/lib/engine";
|
||||
import {
|
||||
KEY_TRANSITION_NAME,
|
||||
type KeysReturn,
|
||||
keysPageFor,
|
||||
requestScrollRestore,
|
||||
} from "@/lib/key-navigation";
|
||||
import { compareOSVersion, matchesVersion, versionTag } from "@/lib/os-version";
|
||||
import type { OS } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const FILTER_INPUT_ID = "path-filter";
|
||||
|
||||
const skeletonWidths = [60, 72, 48, 80, 55, 66, 74, 52];
|
||||
|
||||
function keysUrlFor(os: string, ns: string) {
|
||||
const query = new URLSearchParams({ os });
|
||||
if (ns) query.set("ns", ns);
|
||||
return `/os/keys?${query.toString()}`;
|
||||
}
|
||||
|
||||
/** Paths for a key in a build; a key absent from the build has none. */
|
||||
async function pathsForKey(group: string, build: string, key: string) {
|
||||
const engine = await createEngine(group);
|
||||
try {
|
||||
return await engine.getPathsForKey(build, key);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.startsWith("key not found"))
|
||||
return [];
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export default function FindByKey() {
|
||||
const params = useSearchParams();
|
||||
const router = useRouter();
|
||||
const os = params.get("os");
|
||||
const key = params.get("key");
|
||||
|
||||
const [group, build] = os ? os.split("/") : ["", ""];
|
||||
const compareWith = params.get("diff");
|
||||
|
||||
useEffect(() => {
|
||||
if (os && key) {
|
||||
@@ -30,76 +78,259 @@ export default function FindByKey() {
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [paths, setPaths] = useState<string[]>([]);
|
||||
const [versions, setVersions] = useState<OS[]>([]);
|
||||
const [comparePaths, setComparePaths] = useState<string[] | null>(null);
|
||||
const [compareLoading, setCompareLoading] = useState(false);
|
||||
const [compareError, setCompareError] = useState(false);
|
||||
const [changesOnly, setChangesOnlyState] = useState(
|
||||
() => params.get("changes") === "1",
|
||||
);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [expandAll, setExpandAll] = useState<boolean | null>(null);
|
||||
const { keyword, setKeyword, debouncedKeyword } = useQueryFilter();
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchPaths() {
|
||||
if (!key) return;
|
||||
|
||||
const engine = await createEngine(group);
|
||||
const result = await engine.getPathsForKey(build, key);
|
||||
setPaths(result);
|
||||
}
|
||||
let cancelled = false;
|
||||
setLoading(true);
|
||||
fetchPaths().finally(() => setLoading(false));
|
||||
Promise.all([
|
||||
pathsForKey(group, build, key),
|
||||
createEngine(group).then((engine) =>
|
||||
engine.listOS().catch(() => [] as OS[]),
|
||||
),
|
||||
])
|
||||
.then(([result, list]) => {
|
||||
if (cancelled) return;
|
||||
setPaths(result);
|
||||
setVersions([...list].sort(compareOSVersion));
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setPaths([]);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [group, build, key]);
|
||||
|
||||
const filtered = useMemo(
|
||||
() =>
|
||||
paths.filter((path) =>
|
||||
path.toLowerCase().includes(debouncedKeyword.toLowerCase())
|
||||
),
|
||||
[debouncedKeyword, paths]
|
||||
);
|
||||
const currentVersion = versions.find((v) => matchesVersion(v, build));
|
||||
const compareVersion = compareWith
|
||||
? versions.find((v) => matchesVersion(v, compareWith))
|
||||
: undefined;
|
||||
const activeCompare =
|
||||
compareVersion && compareVersion !== currentVersion ? compareVersion : null;
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
if (!activeCompare) {
|
||||
setComparePaths(null);
|
||||
setCompareLoading(false);
|
||||
setCompareError(false);
|
||||
return;
|
||||
}
|
||||
setComparePaths(null);
|
||||
setCompareLoading(true);
|
||||
setCompareError(false);
|
||||
pathsForKey(group, versionTag(activeCompare), key)
|
||||
.then((result) => {
|
||||
if (!cancelled) setComparePaths(result);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setCompareError(true);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setCompareLoading(false);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [group, key, activeCompare]);
|
||||
|
||||
const diffReady = Boolean(activeCompare) && comparePaths !== null;
|
||||
|
||||
const { added, removed } = useMemo(() => {
|
||||
if (!diffReady || !comparePaths) return { added: [], removed: [] };
|
||||
const current = new Set(paths);
|
||||
const base = new Set(comparePaths);
|
||||
return {
|
||||
added: paths.filter((path) => !base.has(path)),
|
||||
removed: comparePaths.filter((path) => !current.has(path)),
|
||||
};
|
||||
}, [comparePaths, diffReady, paths]);
|
||||
|
||||
const showChanges = changesOnly && diffReady;
|
||||
|
||||
// Lost binaries stay in the tree (marked) so changes show in place.
|
||||
const { list, statusOf } = useMemo(() => {
|
||||
const addedSet = new Set(added);
|
||||
const removedSet = new Set(removed);
|
||||
const all = showChanges ? [...added, ...removed] : [...paths, ...removed];
|
||||
const kw = debouncedKeyword.toLowerCase();
|
||||
return {
|
||||
list: kw ? all.filter((path) => path.toLowerCase().includes(kw)) : all,
|
||||
statusOf: (path: string): PathStatus | undefined =>
|
||||
addedSet.has(path)
|
||||
? "added"
|
||||
: removedSet.has(path)
|
||||
? "removed"
|
||||
: undefined,
|
||||
};
|
||||
}, [added, debouncedKeyword, paths, removed, showChanges]);
|
||||
|
||||
const setParam = (name: string, value: string | null) => {
|
||||
const next = new URLSearchParams(params.toString());
|
||||
if (value) next.set(name, value);
|
||||
else next.delete(name);
|
||||
router.replace(`/os/find?${next.toString()}`, { scroll: false });
|
||||
};
|
||||
|
||||
const setChangesOnly = (value: boolean) => {
|
||||
setChangesOnlyState(value);
|
||||
const next = new URLSearchParams(window.location.search);
|
||||
if (value) next.set("changes", "1");
|
||||
else next.delete("changes");
|
||||
window.history.replaceState(null, "", `?${next.toString()}`);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!copied) return;
|
||||
const timer = setTimeout(() => setCopied(false), 1500);
|
||||
return () => clearTimeout(timer);
|
||||
}, [copied]);
|
||||
|
||||
// "/" focuses the filter from anywhere on the page.
|
||||
useEffect(() => {
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key !== "/" || event.metaKey || event.ctrlKey || event.altKey)
|
||||
return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (
|
||||
target?.isContentEditable ||
|
||||
["INPUT", "TEXTAREA", "SELECT"].includes(target?.tagName ?? "")
|
||||
)
|
||||
return;
|
||||
const input = document.getElementById(FILTER_INPUT_ID);
|
||||
if (!input) return;
|
||||
event.preventDefault();
|
||||
(input as HTMLInputElement).select();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, []);
|
||||
|
||||
const isFiltering = debouncedKeyword.length > 0;
|
||||
|
||||
// Reset expandAll when filter changes
|
||||
// Narrowed views (filter, changes only) open every folder; otherwise the
|
||||
// tree starts with top-level folders open, as before.
|
||||
const narrowed = isFiltering || showChanges;
|
||||
useEffect(() => {
|
||||
setExpandAll(isFiltering ? true : null);
|
||||
}, [isFiltering]);
|
||||
setExpandAll(narrowed ? true : null);
|
||||
}, [narrowed]);
|
||||
|
||||
const [keysReturn, setKeysReturn] = useState<KeysReturn | null>(null);
|
||||
// sessionStorage is client-only; read it after hydration.
|
||||
useEffect(() => setKeysReturn(keysPageFor(key, os)), [key, os]);
|
||||
|
||||
const namespace = key.includes(".") ? key.slice(0, key.lastIndexOf(".")) : "";
|
||||
const backUrl = keysReturn?.url ?? keysUrlFor(os, namespace);
|
||||
const backNs = keysReturn
|
||||
? (new URLSearchParams(keysReturn.url.split("?")[1] ?? "").get("ns") ?? "")
|
||||
: namespace;
|
||||
const compareOs = activeCompare
|
||||
? `${group}/${versionTag(activeCompare)}`
|
||||
: os;
|
||||
|
||||
/** Keys page at namespace `ns`, keeping an active comparison. */
|
||||
const keysUrl = (ns: string) => {
|
||||
const query = new URLSearchParams({ os });
|
||||
if (ns) query.set("ns", ns);
|
||||
if (activeCompare) query.set("diff", versionTag(activeCompare));
|
||||
return `/os/keys?${query.toString()}`;
|
||||
};
|
||||
|
||||
// All keys › [namespace the user came from] › the key's namespace.
|
||||
const crumbs: { ns: string; label: string }[] = [
|
||||
{ ns: "", label: "All keys" },
|
||||
];
|
||||
if (backNs && backNs !== namespace)
|
||||
crumbs.push({ ns: backNs, label: backNs });
|
||||
if (namespace)
|
||||
crumbs.push({
|
||||
ns: namespace,
|
||||
label:
|
||||
backNs && namespace.startsWith(`${backNs}.`)
|
||||
? namespace.slice(backNs.length + 1)
|
||||
: namespace,
|
||||
});
|
||||
|
||||
const returnToKeys = (event: React.MouseEvent<HTMLAnchorElement>) => {
|
||||
if (!keysReturn) return;
|
||||
requestScrollRestore(keysReturn);
|
||||
// Same tab, straight from the keys page: go back in history.
|
||||
if (
|
||||
window.history.length > 1 &&
|
||||
event.button === 0 &&
|
||||
!event.metaKey &&
|
||||
!event.ctrlKey &&
|
||||
!event.shiftKey
|
||||
) {
|
||||
event.preventDefault();
|
||||
router.back();
|
||||
}
|
||||
};
|
||||
|
||||
const filterControls = (
|
||||
<>
|
||||
<div className="relative flex-1 sm:flex-none sm:w-96">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
||||
<div className="relative w-full">
|
||||
<Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||
<Input
|
||||
id={FILTER_INPUT_ID}
|
||||
type="text"
|
||||
placeholder="Filter paths..."
|
||||
placeholder="Filter binaries..."
|
||||
value={keyword}
|
||||
onChange={(e) => setKeyword(e.target.value)}
|
||||
className="pl-9 pr-9"
|
||||
/>
|
||||
{!keyword && (
|
||||
<kbd
|
||||
className="pointer-events-none absolute right-2.5 top-1/2 hidden -translate-y-1/2 rounded border bg-muted px-1.5 font-mono text-[11px] text-muted-foreground sm:block"
|
||||
title="Press / to filter"
|
||||
>
|
||||
/
|
||||
</kbd>
|
||||
)}
|
||||
{keyword && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setKeyword("")}
|
||||
className="absolute right-1 top-1/2 -translate-y-1/2 h-7 w-7 p-0"
|
||||
className="absolute right-1 top-1/2 h-7 w-7 -translate-y-1/2 p-0"
|
||||
aria-label="Clear filter"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
{!loading && paths.length > 0 && (
|
||||
<div className="flex items-center border border-border rounded-md">
|
||||
{!loading && list.length > 0 && (
|
||||
<div className="flex shrink-0 items-center rounded-md border border-border">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setExpandAll(true)}
|
||||
className="h-8 px-2 rounded-r-none"
|
||||
title="Expand All"
|
||||
className="h-8 rounded-r-none px-2"
|
||||
title="Expand all"
|
||||
aria-label="Expand all"
|
||||
>
|
||||
<ChevronsUpDown className="h-4 w-4" />
|
||||
</Button>
|
||||
<div className="w-px h-4 bg-border" />
|
||||
<div className="h-4 w-px bg-border" />
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setExpandAll(false)}
|
||||
className="h-8 px-2 rounded-l-none"
|
||||
title="Collapse All"
|
||||
className="h-8 rounded-l-none px-2"
|
||||
title="Collapse all"
|
||||
aria-label="Collapse all"
|
||||
>
|
||||
<ChevronsDownUp className="h-4 w-4" />
|
||||
</Button>
|
||||
@@ -108,47 +339,214 @@ export default function FindByKey() {
|
||||
</>
|
||||
);
|
||||
|
||||
// Versions grouped by major for the compare picker.
|
||||
const versionGroups = useMemo(() => {
|
||||
const groups = new Map<string, OS[]>();
|
||||
for (const version of versions) {
|
||||
if (version === currentVersion) continue;
|
||||
const major = version.version.split(".")[0];
|
||||
const list = groups.get(major);
|
||||
if (list) list.push(version);
|
||||
else groups.set(major, [version]);
|
||||
}
|
||||
return [...groups];
|
||||
}, [currentVersion, versions]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex h-full flex-col">
|
||||
<HeaderPortal>{filterControls}</HeaderPortal>
|
||||
|
||||
<div className="mb-3 shrink-0 flex items-baseline justify-between gap-4">
|
||||
<div>
|
||||
<span className="text-sm text-muted-foreground">Binaries with </span>
|
||||
<code className="text-sm break-all text-primary font-medium">{key}</code>
|
||||
</div>
|
||||
{!loading && (
|
||||
<span className="text-sm text-muted-foreground whitespace-nowrap">
|
||||
{isFiltering ? `${filtered.length} of ${paths.length}` : paths.length} paths
|
||||
{/* Where this key lives, styled like the keys page's own path. The
|
||||
arrow, and the crumb the user came from, return to exactly where
|
||||
they were (namespace, filter, comparison, scroll). */}
|
||||
<nav
|
||||
aria-label="Breadcrumb"
|
||||
className="mb-1.5 flex min-w-0 flex-wrap items-center gap-x-1 gap-y-0.5 text-sm"
|
||||
>
|
||||
<Link
|
||||
href={backUrl}
|
||||
onClick={returnToKeys}
|
||||
aria-label="Back to keys"
|
||||
title="Back to keys"
|
||||
className="mr-1 rounded-md p-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
<ArrowLeft className="h-3.5 w-3.5" />
|
||||
</Link>
|
||||
{crumbs.map((crumb, i) => (
|
||||
<span key={crumb.ns || "(all)"} className="flex items-center gap-1">
|
||||
{i > 0 && (
|
||||
<ChevronRight className="h-3.5 w-3.5 text-muted-foreground/60" />
|
||||
)}
|
||||
<Link
|
||||
href={crumb.ns === backNs ? backUrl : keysUrl(crumb.ns)}
|
||||
onClick={crumb.ns === backNs ? returnToKeys : undefined}
|
||||
title={crumb.ns || "All keys"}
|
||||
className={cn(
|
||||
"rounded px-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground",
|
||||
crumb.ns && "font-mono",
|
||||
)}
|
||||
>
|
||||
{crumb.label}
|
||||
</Link>
|
||||
</span>
|
||||
)}
|
||||
))}
|
||||
</nav>
|
||||
|
||||
{/* Key context: what we're looking at, and where it lives. */}
|
||||
<div className="mb-4 flex flex-wrap items-center gap-x-3 gap-y-2">
|
||||
<div className="flex min-w-0 items-center gap-1">
|
||||
<code
|
||||
data-key-title
|
||||
// The clicked key on the keys page morphs into this title.
|
||||
style={{ viewTransitionName: KEY_TRANSITION_NAME }}
|
||||
className="break-all font-mono text-base font-semibold"
|
||||
>
|
||||
{key}
|
||||
</code>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
navigator.clipboard
|
||||
.writeText(key)
|
||||
.then(() => setCopied(true))
|
||||
.catch(() => {});
|
||||
}}
|
||||
className="rounded p-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
|
||||
aria-label={copied ? "Copied" : "Copy key"}
|
||||
title={copied ? "Copied" : "Copy key"}
|
||||
>
|
||||
{copied ? (
|
||||
<Check className="h-3.5 w-3.5 text-emerald-600 dark:text-emerald-400" />
|
||||
) : (
|
||||
<Copy className="h-3.5 w-3.5" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="ml-auto flex flex-wrap items-center justify-end gap-2 text-sm">
|
||||
{!loading && (
|
||||
<span className="tabular-nums text-muted-foreground">
|
||||
{isFiltering ? `${list.length} of ${paths.length}` : paths.length}{" "}
|
||||
{paths.length === 1 ? "binary" : "binaries"}
|
||||
{currentVersion && (
|
||||
<>
|
||||
{" "}
|
||||
in {currentVersion.version}
|
||||
{currentVersion.beta && (
|
||||
<span className="ml-1 align-middle">
|
||||
<BetaBadge />
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{compareLoading && (
|
||||
<span className="text-muted-foreground">Comparing…</span>
|
||||
)}
|
||||
{compareError && (
|
||||
<span className="text-destructive">Compare unavailable</span>
|
||||
)}
|
||||
|
||||
{diffReady && (
|
||||
<>
|
||||
<span className="inline-flex items-center gap-1 text-emerald-700 dark:text-emerald-300">
|
||||
<CirclePlus className="h-3.5 w-3.5" />
|
||||
{added.length} gained
|
||||
</span>
|
||||
<span className="inline-flex items-center gap-1 text-red-700 dark:text-red-300">
|
||||
<CircleMinus className="h-3.5 w-3.5" />
|
||||
{removed.length} lost
|
||||
</span>
|
||||
{added.length + removed.length > 0 && (
|
||||
<Button
|
||||
type="button"
|
||||
variant={changesOnly ? "secondary" : "outline"}
|
||||
size="sm"
|
||||
onClick={() => setChangesOnly(!changesOnly)}
|
||||
className="h-7 gap-1.5 px-2 text-xs"
|
||||
aria-pressed={changesOnly}
|
||||
title="Show only binaries that gained or lost this key"
|
||||
>
|
||||
<ListFilter className="h-3.5 w-3.5" />
|
||||
Changed only
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{versions.length > 1 && (
|
||||
<label className="inline-flex h-7 items-center gap-1.5 rounded-md border px-2 text-xs text-muted-foreground has-[select:focus-visible]:ring-2 has-[select:focus-visible]:ring-ring">
|
||||
<GitCompare className="h-3.5 w-3.5" />
|
||||
<span className="sr-only">Compare with</span>
|
||||
<select
|
||||
value={activeCompare ? versionTag(activeCompare) : ""}
|
||||
onChange={(e) => setParam("diff", e.target.value || null)}
|
||||
className="bg-transparent text-foreground outline-none"
|
||||
>
|
||||
<option value="">Compare with…</option>
|
||||
{versionGroups.map(([major, list]) => (
|
||||
<optgroup key={major} label={`${group} ${major}`}>
|
||||
{list.map((version) => (
|
||||
<option
|
||||
key={versionTag(version)}
|
||||
value={versionTag(version)}
|
||||
>
|
||||
{version.version} ({version.build})
|
||||
{version.beta ? " beta" : ""}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
))}
|
||||
</select>
|
||||
{activeCompare && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setParam("diff", null)}
|
||||
className="rounded p-0.5 hover:bg-accent hover:text-foreground"
|
||||
aria-label="Stop comparing"
|
||||
title="Stop comparing"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-h-0 overflow-auto">
|
||||
{loading ? (
|
||||
<div className="space-y-2">
|
||||
{Array.from({ length: 6 }).map((_, i) => (
|
||||
<div key={i} className="flex items-center gap-2 py-1">
|
||||
<div className="h-4 w-4 bg-muted rounded animate-pulse" />
|
||||
<div
|
||||
className="h-5 bg-muted rounded animate-pulse"
|
||||
style={{ width: `${120 + Math.random() * 200}px` }}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="text-center py-12 text-muted-foreground">
|
||||
{paths.length === 0 ? (
|
||||
<p>No binaries found with this entitlement.</p>
|
||||
) : (
|
||||
<p>No paths match "{keyword}"</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<FileSystem os={os} list={filtered} expandAll={expandAll} />
|
||||
)}
|
||||
</div>
|
||||
{loading ? (
|
||||
<div className="space-y-2">
|
||||
{skeletonWidths.map((width) => (
|
||||
<div
|
||||
key={width}
|
||||
className="h-5 animate-pulse rounded bg-muted"
|
||||
style={{ width: `${width / 3}%` }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : list.length === 0 ? (
|
||||
<div className="py-12 text-center text-muted-foreground">
|
||||
{paths.length === 0 && !diffReady ? (
|
||||
<p>No binaries found with this entitlement.</p>
|
||||
) : showChanges && !isFiltering ? (
|
||||
<p>No binaries gained or lost this key.</p>
|
||||
) : (
|
||||
<p>No binaries match "{keyword}"</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<FileSystem
|
||||
os={os}
|
||||
list={list}
|
||||
expandAll={expandAll}
|
||||
status={diffReady ? statusOf : undefined}
|
||||
osFor={(path) =>
|
||||
statusOf(path) === "removed" ? compareOs : undefined
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+37
-22
@@ -27,6 +27,8 @@ import {
|
||||
} from "@/components/ui/popover";
|
||||
import { useQueryFilter } from "@/hooks/use-query-filter";
|
||||
import { createEngine } from "@/lib/engine";
|
||||
import { basePath } from "@/lib/env";
|
||||
import { takeScrollRestore } from "@/lib/key-navigation";
|
||||
import {
|
||||
type NamespaceNode,
|
||||
allNamespaces,
|
||||
@@ -35,30 +37,18 @@ import {
|
||||
inNamespace,
|
||||
namespacePath,
|
||||
} from "@/lib/key-tree";
|
||||
import { compareOSVersion, matchesVersion, versionTag } from "@/lib/os-version";
|
||||
import type { OS } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
function versionTag(os: OS) {
|
||||
return `${os.version}_${os.build}`;
|
||||
}
|
||||
|
||||
function isCurrentVersion(os: OS, build: string) {
|
||||
return os.build === build || versionTag(os) === build;
|
||||
return matchesVersion(os, build);
|
||||
}
|
||||
|
||||
function matchesVersion(os: OS, tag: string) {
|
||||
return os.build === tag || versionTag(os) === tag;
|
||||
}
|
||||
|
||||
function compareOSVersion(a: OS, b: OS) {
|
||||
const vA = a.version.split(".").map(Number);
|
||||
const vB = b.version.split(".").map(Number);
|
||||
for (let i = 0; i < Math.max(vA.length, vB.length); i++) {
|
||||
const diff = (vB[i] || 0) - (vA[i] || 0);
|
||||
if (diff !== 0) return diff;
|
||||
}
|
||||
|
||||
return b.build.localeCompare(a.build);
|
||||
function withoutBasePath(path: string) {
|
||||
return basePath && path.startsWith(basePath)
|
||||
? path.slice(basePath.length)
|
||||
: path;
|
||||
}
|
||||
|
||||
/** Open every namespace when a filtered result is this small. */
|
||||
@@ -407,7 +397,12 @@ export default function Keys() {
|
||||
const loweredKeyword = debouncedKeyword.toLowerCase();
|
||||
|
||||
return [
|
||||
...addedKeys.map((key) => ({ key, os, status: "added" as const })),
|
||||
...addedKeys.map((key) => ({
|
||||
key,
|
||||
os,
|
||||
status: "added" as const,
|
||||
diff: activeCompareTag ?? undefined,
|
||||
})),
|
||||
...removedKeys.map((key) => ({
|
||||
key,
|
||||
os: compareOs,
|
||||
@@ -419,7 +414,15 @@ export default function Keys() {
|
||||
(a, b) =>
|
||||
a.key.localeCompare(b.key) || a.status.localeCompare(b.status),
|
||||
);
|
||||
}, [addedKeys, compareOs, debouncedKeyword, diffReady, os, removedKeys]);
|
||||
}, [
|
||||
activeCompareTag,
|
||||
addedKeys,
|
||||
compareOs,
|
||||
debouncedKeyword,
|
||||
diffReady,
|
||||
os,
|
||||
removedKeys,
|
||||
]);
|
||||
|
||||
const displayedKeys = useMemo<DisplayedKey[]>(
|
||||
() =>
|
||||
@@ -427,8 +430,10 @@ export default function Keys() {
|
||||
key,
|
||||
os,
|
||||
status: diffReady && addedKeySet.has(key) ? "added" : "current",
|
||||
// Open the find page comparing against the same version.
|
||||
diff: diffReady ? (activeCompareTag ?? undefined) : undefined,
|
||||
})),
|
||||
[addedKeySet, diffReady, filtered, os],
|
||||
[activeCompareTag, addedKeySet, diffReady, filtered, os],
|
||||
);
|
||||
|
||||
const isFiltering = debouncedKeyword.length > 0;
|
||||
@@ -522,7 +527,17 @@ export default function Keys() {
|
||||
return Object.entries(groups).sort(([a], [b]) => Number(b) - Number(a));
|
||||
}, [versions]);
|
||||
|
||||
// "/" focuses the filter from anywhere on the page, as on GitHub.
|
||||
// Returning from a key's find page: restore the scroll position once the
|
||||
// keys have rendered (browser restoration ran against the skeleton).
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
const y = takeScrollRestore(
|
||||
withoutBasePath(window.location.pathname) + window.location.search,
|
||||
);
|
||||
if (y !== null) requestAnimationFrame(() => window.scrollTo(0, y));
|
||||
}, [loading]);
|
||||
|
||||
// "/" focuses the filter from anywhere on the page.
|
||||
useEffect(() => {
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key !== "/" || event.metaKey || event.ctrlKey || event.altKey)
|
||||
|
||||
@@ -2,19 +2,45 @@
|
||||
|
||||
import { Check, Copy, Minus, Plus } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import { basePath } from "@/lib/env";
|
||||
import {
|
||||
KEY_TRANSITION_NAME,
|
||||
canAnimateNavigation,
|
||||
navigateWithTransition,
|
||||
rememberKeysPage,
|
||||
} from "@/lib/key-navigation";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
/** router.push() adds basePath itself, so store paths without it. */
|
||||
function withoutBasePath(path: string) {
|
||||
return basePath && path.startsWith(basePath)
|
||||
? path.slice(basePath.length)
|
||||
: path;
|
||||
}
|
||||
|
||||
export type DisplayedKey = {
|
||||
key: string;
|
||||
os: string;
|
||||
status: "current" | "added" | "removed";
|
||||
/** Version being compared against, carried over to the find page. */
|
||||
diff?: string;
|
||||
};
|
||||
|
||||
function findHref(entry: DisplayedKey) {
|
||||
const query = new URLSearchParams({ key: entry.key, os: entry.os });
|
||||
if (entry.diff) query.set("diff", entry.diff);
|
||||
return `/os/find?${query.toString()}`;
|
||||
}
|
||||
|
||||
export function KeyLink({ entry }: { entry: DisplayedKey }) {
|
||||
const isNew = entry.status === "added";
|
||||
const isRemoved = entry.status === "removed";
|
||||
const router = useRouter();
|
||||
const labelRef = useRef<HTMLSpanElement>(null);
|
||||
const href = findHref(entry);
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -28,9 +54,23 @@ export function KeyLink({ entry }: { entry: DisplayedKey }) {
|
||||
)}
|
||||
>
|
||||
<Link
|
||||
href={`/os/find?key=${encodeURIComponent(
|
||||
entry.key,
|
||||
)}&os=${encodeURIComponent(entry.os)}`}
|
||||
href={href}
|
||||
onClick={(event) => {
|
||||
// Remember exactly where we were, for the find page's back link.
|
||||
rememberKeysPage({
|
||||
url:
|
||||
withoutBasePath(window.location.pathname) +
|
||||
window.location.search,
|
||||
key: entry.key,
|
||||
os: entry.os,
|
||||
scrollY: window.scrollY,
|
||||
});
|
||||
if (!canAnimateNavigation(event) || !labelRef.current) return;
|
||||
// Morph this key name into the find page title.
|
||||
event.preventDefault();
|
||||
labelRef.current.style.viewTransitionName = KEY_TRANSITION_NAME;
|
||||
navigateWithTransition(() => router.push(href), "[data-key-title]");
|
||||
}}
|
||||
className="flex min-w-0 items-start gap-2 rounded-md py-1 pl-1 pr-7 font-mono text-sm leading-5"
|
||||
title={entry.key}
|
||||
onKeyDown={(event) => {
|
||||
@@ -63,7 +103,9 @@ export function KeyLink({ entry }: { entry: DisplayedKey }) {
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
<KeyLabel text={entry.key} />
|
||||
<span ref={labelRef} className="min-w-0">
|
||||
<KeyLabel text={entry.key} />
|
||||
</span>
|
||||
</Link>
|
||||
<CopyKey text={entry.key} />
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Continuity between the keys page and a key's find page:
|
||||
* - remember where on the keys page the user clicked a key, so the find page
|
||||
* can offer a precise way back (same namespace, filter, comparison, scroll);
|
||||
* - morph the clicked key name into the find page title with a view
|
||||
* transition where the browser supports it.
|
||||
*/
|
||||
|
||||
const RETURN_KEY = "entdb:keys-return";
|
||||
|
||||
/** `view-transition-name` shared by the clicked key and the find page title. */
|
||||
export const KEY_TRANSITION_NAME = "entitlement-key";
|
||||
|
||||
export interface KeysReturn {
|
||||
/** Keys page URL (path + query, without basePath) to return to. */
|
||||
url: string;
|
||||
/** The find page this return belongs to. */
|
||||
key: string;
|
||||
os: string;
|
||||
/** Scroll position on the keys page when the key was clicked. */
|
||||
scrollY: number;
|
||||
}
|
||||
|
||||
export function rememberKeysPage(entry: KeysReturn) {
|
||||
try {
|
||||
sessionStorage.setItem(RETURN_KEY, JSON.stringify(entry));
|
||||
} catch {
|
||||
// Storage unavailable: the find page falls back to the key's namespace.
|
||||
}
|
||||
}
|
||||
|
||||
const RESTORE_KEY = "entdb:keys-restore";
|
||||
|
||||
/**
|
||||
* Ask the keys page to restore its scroll once its data has loaded. The
|
||||
* browser's own restoration runs before the async load, against a page that
|
||||
* is still a short skeleton.
|
||||
*/
|
||||
export function requestScrollRestore(entry: KeysReturn) {
|
||||
try {
|
||||
sessionStorage.setItem(
|
||||
RESTORE_KEY,
|
||||
JSON.stringify({ url: entry.url, scrollY: entry.scrollY }),
|
||||
);
|
||||
} catch {
|
||||
// Not critical.
|
||||
}
|
||||
}
|
||||
|
||||
/** One-shot: the scroll position to restore for `url`, if any. */
|
||||
export function takeScrollRestore(url: string): number | null {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(RESTORE_KEY);
|
||||
if (!raw) return null;
|
||||
sessionStorage.removeItem(RESTORE_KEY);
|
||||
const entry = JSON.parse(raw) as { url: string; scrollY: number };
|
||||
return entry.url === url ? entry.scrollY : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The remembered keys page, if it led to this exact find page. */
|
||||
export function keysPageFor(key: string, os: string): KeysReturn | null {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(RETURN_KEY);
|
||||
if (!raw) return null;
|
||||
const entry = JSON.parse(raw) as KeysReturn;
|
||||
return entry.key === key && entry.os === os ? entry : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
type ViewTransitionDocument = Document & {
|
||||
startViewTransition?: (update: () => Promise<void>) => unknown;
|
||||
};
|
||||
|
||||
/** Whether a click should animate (plain left click, motion allowed). */
|
||||
export function canAnimateNavigation(event: {
|
||||
button: number;
|
||||
metaKey: boolean;
|
||||
ctrlKey: boolean;
|
||||
shiftKey: boolean;
|
||||
altKey: boolean;
|
||||
}) {
|
||||
return (
|
||||
event.button === 0 &&
|
||||
!event.metaKey &&
|
||||
!event.ctrlKey &&
|
||||
!event.shiftKey &&
|
||||
!event.altKey &&
|
||||
typeof (document as ViewTransitionDocument).startViewTransition ===
|
||||
"function" &&
|
||||
!window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Navigate inside a view transition. The transition snapshots the new state
|
||||
* once `selector` exists (the destination's title), or after `timeoutMs` so a
|
||||
* slow route never freezes the page.
|
||||
*/
|
||||
export function navigateWithTransition(
|
||||
navigate: () => void,
|
||||
selector: string,
|
||||
timeoutMs = 800,
|
||||
) {
|
||||
const start = (document as ViewTransitionDocument).startViewTransition;
|
||||
if (!start) {
|
||||
navigate();
|
||||
return;
|
||||
}
|
||||
start.call(document, async () => {
|
||||
navigate();
|
||||
const deadline = performance.now() + timeoutMs;
|
||||
// Poll with timers: rendering (and so requestAnimationFrame) is paused
|
||||
// while a view transition's update callback is pending.
|
||||
while (!document.querySelector(selector) && performance.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 16));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { OS } from "@/lib/types";
|
||||
|
||||
/** URL tag for a build, e.g. `27.2.1_24B91034`. */
|
||||
export function versionTag(os: OS) {
|
||||
return `${os.version}_${os.build}`;
|
||||
}
|
||||
|
||||
/** Whether `os` is the build named by `tag` (a bare build or a full tag). */
|
||||
export function matchesVersion(os: OS, tag: string) {
|
||||
return os.build === tag || versionTag(os) === tag;
|
||||
}
|
||||
|
||||
/** Sort comparator: newest version first, then by build. */
|
||||
export function compareOSVersion(a: OS, b: OS) {
|
||||
const vA = a.version.split(".").map(Number);
|
||||
const vB = b.version.split(".").map(Number);
|
||||
for (let i = 0; i < Math.max(vA.length, vB.length); i++) {
|
||||
const diff = (vB[i] || 0) - (vA[i] || 0);
|
||||
if (diff !== 0) return diff;
|
||||
}
|
||||
|
||||
return b.build.localeCompare(a.build);
|
||||
}
|
||||
Reference in new issue
Block a user