mirror of
https://github.com/ChiChou/entdb.git
synced 2026-12-05 11:13:40 +01:00
feat(find): show values and filter binaries by value
- Each binary's value is shown in the tree (hidden on phones); a value shared by every binary is stated once in the header instead. - "Value" (or =) opens a typeahead of this key's values with faceted counts. Arrays match by "contains", scalars by "equals"; several picks must all match. Matches are highlighted and listed first in each row. - Picks show on their own row under the key and persist in ?v=. - Works with version compare: lost binaries filter by their old value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4b6e6a52bd
commit
e76fd472ae
6 files changed
+513
-32
No files matched your search
@@ -20,6 +20,7 @@ import {
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
import { ValuePreview } from "@/components/value-preview";
|
||||
import filesToTree, { type TreeWithFullPath } from "@/lib/tree";
|
||||
import Link from "next/link";
|
||||
|
||||
@@ -34,9 +35,14 @@ export type PathStatus = "added" | "removed";
|
||||
interface Marks {
|
||||
status?: (path: string) => PathStatus | undefined;
|
||||
osFor?: (path: string) => string | undefined;
|
||||
/** Shows each binary's entitlement value next to its name. */
|
||||
valueFor?: (path: string) => unknown;
|
||||
/** Atoms to mark in the values (the active value filter). */
|
||||
highlight?: readonly string[];
|
||||
}
|
||||
|
||||
const MarksContext = createContext<Marks>({});
|
||||
const NO_HIGHLIGHT: readonly string[] = [];
|
||||
|
||||
function countMarks(item: TreeWithFullPath, status: Marks["status"]) {
|
||||
let added = 0;
|
||||
@@ -128,11 +134,19 @@ const FileItem = memo(function FileItem({
|
||||
className={cn(
|
||||
"font-mono text-sm hover:underline truncate",
|
||||
!mark && "text-muted-foreground hover:text-foreground",
|
||||
marks.valueFor && "sm:max-w-[60%] sm:shrink-0",
|
||||
)}
|
||||
title={fullPath}
|
||||
>
|
||||
{name}
|
||||
</Link>
|
||||
{marks.valueFor && (
|
||||
<ValuePreview
|
||||
value={marks.valueFor(fullPath)}
|
||||
highlight={marks.highlight ?? NO_HIGHLIGHT}
|
||||
className="ml-1 hidden sm:flex"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -327,13 +341,18 @@ const FileSystem = memo(function FileSystem({
|
||||
expandAll = null,
|
||||
status,
|
||||
osFor,
|
||||
valueFor,
|
||||
highlight,
|
||||
}: {
|
||||
list: string[];
|
||||
os: string;
|
||||
expandAll?: boolean | null;
|
||||
} & Marks) {
|
||||
const tree = useMemo(() => filesToTree(list), [list]);
|
||||
const marks = useMemo(() => ({ status, osFor }), [status, osFor]);
|
||||
const marks = useMemo(
|
||||
() => ({ status, osFor, valueFor, highlight }),
|
||||
[status, osFor, valueFor, highlight],
|
||||
);
|
||||
return (
|
||||
<MarksContext.Provider value={marks}>
|
||||
<div onKeyDown={onTreeKeyDown}>
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
* / focus the page filter Esc clear it, then leave it
|
||||
* j / k next / previous item ? this help
|
||||
* g h|k|f go home / keys / files v version picker
|
||||
* = value filter (find page)
|
||||
*
|
||||
* Lists opt in by marking focusable rows with `data-nav-item`. Widgets that
|
||||
* own their keys (the namespace tree, file trees) handle them first and call
|
||||
@@ -30,6 +31,9 @@ export const NAV_ITEM_ATTR = "data-nav-item";
|
||||
/** Marks the button that opens the version picker. */
|
||||
export const VERSION_PICKER_ATTR = "data-version-picker";
|
||||
|
||||
/** Marks the button that opens the value filter. */
|
||||
export const VALUE_FILTER_ATTR = "data-value-filter";
|
||||
|
||||
const GOTO_WINDOW_MS = 1200;
|
||||
|
||||
function isTyping(target: EventTarget | null) {
|
||||
@@ -149,9 +153,10 @@ export function KeyboardShortcuts({ os }: { os: string }) {
|
||||
case "g":
|
||||
gotoAt = performance.now();
|
||||
return;
|
||||
case "v": {
|
||||
case "v":
|
||||
case "=": {
|
||||
const picker = document.querySelector<HTMLElement>(
|
||||
`[${VERSION_PICKER_ATTR}]`,
|
||||
`[${event.key === "v" ? VERSION_PICKER_ATTR : VALUE_FILTER_ATTR}]`,
|
||||
);
|
||||
if (!picker) return;
|
||||
event.preventDefault();
|
||||
@@ -226,6 +231,7 @@ const SHORTCUTS: { title: string; items: [string[], string][] }[] = [
|
||||
[["/"], "Filter"],
|
||||
[["Esc"], "Clear / leave filter"],
|
||||
[["v"], "Switch version"],
|
||||
[["="], "Filter by value"],
|
||||
[["?"], "Show shortcuts"],
|
||||
],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
"use client";
|
||||
|
||||
import { Braces, Check, X } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
import { VALUE_FILTER_ATTR } from "@/components/keyboard-shortcuts";
|
||||
import {
|
||||
Command,
|
||||
CommandEmpty,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
} from "@/components/ui/command";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { countAtoms, matchesAtoms } from "@/lib/entitlement-value";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
/** Rendering thousands of cmdk items is slow; typing narrows the rest. */
|
||||
const MAX_SUGGESTIONS = 150;
|
||||
|
||||
/**
|
||||
* "Value contains …" filter with suggestions drawn from this key's values.
|
||||
* Selecting several atoms means all of them (AND); the counts are faceted,
|
||||
* so they always say how many binaries would remain after adding that atom.
|
||||
*/
|
||||
export function ValueFilter({
|
||||
values,
|
||||
selected,
|
||||
onChange,
|
||||
}: {
|
||||
/** Every binary's value for the key. */
|
||||
values: unknown[];
|
||||
selected: string[];
|
||||
onChange: (selected: string[]) => void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState("");
|
||||
|
||||
const facets = useMemo(
|
||||
() => countAtoms(values.filter((value) => matchesAtoms(value, selected))),
|
||||
[values, selected],
|
||||
);
|
||||
|
||||
const suggestions = useMemo(() => {
|
||||
const q = query.trim().toLowerCase();
|
||||
const rest = facets.filter(({ atom }) => !selected.includes(atom));
|
||||
if (!q) return rest;
|
||||
// Prefix matches first, then anywhere; each keeps the count order.
|
||||
const prefix = [];
|
||||
const inner = [];
|
||||
for (const facet of rest) {
|
||||
const index = facet.atom.toLowerCase().indexOf(q);
|
||||
if (index === 0) prefix.push(facet);
|
||||
else if (index > 0) inner.push(facet);
|
||||
}
|
||||
return [...prefix, ...inner];
|
||||
}, [facets, query, selected]);
|
||||
|
||||
const toggle = (atom: string) => {
|
||||
onChange(
|
||||
selected.includes(atom)
|
||||
? selected.filter((a) => a !== atom)
|
||||
: [...selected, atom],
|
||||
);
|
||||
setQuery("");
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
{...{ [VALUE_FILTER_ATTR]: "" }}
|
||||
className={cn(
|
||||
"inline-flex h-7 items-center gap-1.5 rounded-md border px-2 text-xs transition-colors hover:bg-accent",
|
||||
selected.length > 0
|
||||
? "border-primary/40 text-foreground"
|
||||
: "text-muted-foreground",
|
||||
)}
|
||||
title="Filter by value (=)"
|
||||
>
|
||||
<Braces className="h-3.5 w-3.5" />
|
||||
Value
|
||||
{selected.length > 0 && (
|
||||
<span className="rounded bg-primary px-1 tabular-nums text-[11px] leading-4 text-primary-foreground">
|
||||
{selected.length}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
className="w-[min(28rem,calc(100vw-2rem))] p-0"
|
||||
align="end"
|
||||
>
|
||||
<Command shouldFilter={false}>
|
||||
<CommandInput
|
||||
value={query}
|
||||
onValueChange={setQuery}
|
||||
placeholder="Value contains…"
|
||||
onKeyDown={(e) => {
|
||||
// Backspace on an empty query drops the last condition.
|
||||
if (e.key === "Backspace" && !query && selected.length > 0) {
|
||||
e.preventDefault();
|
||||
onChange(selected.slice(0, -1));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<CommandList className="max-h-[min(22rem,60vh)]">
|
||||
<CommandEmpty>
|
||||
{facets.length === 0 ? "No binaries match." : "No such value."}
|
||||
</CommandEmpty>
|
||||
{/* Hidden while typing, so Enter picks a match, not these. */}
|
||||
{!query &&
|
||||
selected.map((atom) => (
|
||||
<CommandItem
|
||||
key={`selected:${atom}`}
|
||||
value={`selected:${atom}`}
|
||||
onSelect={() => toggle(atom)}
|
||||
className="gap-2"
|
||||
>
|
||||
<Check className="h-3.5 w-3.5 shrink-0" />
|
||||
<span className="min-w-0 flex-1 truncate font-mono text-xs">
|
||||
{atom}
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
{suggestions.slice(0, MAX_SUGGESTIONS).map(({ atom, count }) => (
|
||||
<CommandItem
|
||||
key={atom}
|
||||
value={atom}
|
||||
onSelect={() => toggle(atom)}
|
||||
className="gap-2"
|
||||
>
|
||||
<span className="h-3.5 w-3.5 shrink-0" />
|
||||
<span
|
||||
className="min-w-0 flex-1 truncate font-mono text-xs"
|
||||
title={atom}
|
||||
>
|
||||
{atom}
|
||||
</span>
|
||||
<span className="shrink-0 text-xs tabular-nums text-muted-foreground">
|
||||
{count}
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
{suggestions.length > MAX_SUGGESTIONS && (
|
||||
<div className="px-2 py-1.5 text-xs text-muted-foreground">
|
||||
{suggestions.length - MAX_SUGGESTIONS} more — type to narrow
|
||||
</div>
|
||||
)}
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The active value conditions, on their own row under the key header so
|
||||
* adding one never reflows the header controls.
|
||||
*/
|
||||
export function ValueConditions({
|
||||
selected,
|
||||
onChange,
|
||||
}: {
|
||||
selected: string[];
|
||||
onChange: (selected: string[]) => void;
|
||||
}) {
|
||||
if (selected.length === 0) return null;
|
||||
return (
|
||||
<div className="flex min-w-0 flex-wrap items-center gap-1.5 text-xs">
|
||||
<span className="text-muted-foreground">Value contains</span>
|
||||
{selected.map((atom, i) => (
|
||||
<span key={atom} className="flex min-w-0 items-center gap-1.5">
|
||||
{i > 0 && <span className="text-muted-foreground">and</span>}
|
||||
<span
|
||||
className="inline-flex h-6 min-w-0 max-w-[20rem] items-center gap-0.5 rounded-md bg-primary/10 pl-2 pr-0.5 font-mono"
|
||||
title={atom}
|
||||
>
|
||||
<span className="truncate">{atom}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onChange(selected.filter((a) => a !== atom))}
|
||||
className="rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
aria-label={`Remove ${atom}`}
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</span>
|
||||
</span>
|
||||
))}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onChange([])}
|
||||
className="rounded px-1.5 py-0.5 text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { atomOf } from "@/lib/entitlement-value";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const TITLE_LIMIT = 4000;
|
||||
|
||||
function titleFor(value: unknown) {
|
||||
const json = JSON.stringify(value, null, 2);
|
||||
return json.length > TITLE_LIMIT ? `${json.slice(0, TITLE_LIMIT)}\n…` : json;
|
||||
}
|
||||
|
||||
function Atom({ atom, hit }: { atom: string; hit: boolean }) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
hit && "rounded-sm bg-primary/10 px-0.5 font-medium text-foreground",
|
||||
)}
|
||||
>
|
||||
{atom}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* One-line summary of an entitlement value, for a tree row. Array items and
|
||||
* dict keys that match the active value filter come first and are marked, so
|
||||
* the reason a binary matched is visible even when the list is truncated.
|
||||
*/
|
||||
export function ValuePreview({
|
||||
value,
|
||||
highlight,
|
||||
className,
|
||||
}: {
|
||||
value: unknown;
|
||||
highlight: readonly string[];
|
||||
className?: string;
|
||||
}) {
|
||||
const hits = new Set(highlight);
|
||||
const isList = Array.isArray(value);
|
||||
const isDict = !isList && value !== null && typeof value === "object";
|
||||
|
||||
let body: React.ReactNode;
|
||||
let count: number | null = null;
|
||||
if (isList || isDict) {
|
||||
const items = isList
|
||||
? (value as unknown[]).map((item) =>
|
||||
item !== null && typeof item === "object"
|
||||
? JSON.stringify(item)
|
||||
: atomOf(item),
|
||||
)
|
||||
: Object.keys(value as object);
|
||||
count = items.length;
|
||||
const ordered = [
|
||||
...items.filter((item) => hits.has(item)),
|
||||
...items.filter((item) => !hits.has(item)),
|
||||
];
|
||||
body = (
|
||||
<>
|
||||
{isList ? "[" : "{"}
|
||||
{ordered.map((item, i) => (
|
||||
// biome-ignore lint/suspicious/noArrayIndexKey: items may repeat
|
||||
<span key={i}>
|
||||
{i > 0 && ", "}
|
||||
<Atom atom={item} hit={hits.has(item)} />
|
||||
</span>
|
||||
))}
|
||||
{isList ? "]" : "}"}
|
||||
</>
|
||||
);
|
||||
} else {
|
||||
const atom = atomOf(value);
|
||||
body = (
|
||||
<Atom
|
||||
atom={typeof value === "string" ? `"${atom}"` : atom}
|
||||
hit={hits.has(atom)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"flex min-w-0 items-center gap-1.5 font-mono text-xs text-muted-foreground",
|
||||
className,
|
||||
)}
|
||||
title={titleFor(value)}
|
||||
>
|
||||
<span className="truncate">{body}</span>
|
||||
{count !== null && count > 1 && (
|
||||
<span className="shrink-0 rounded bg-muted px-1 tabular-nums">
|
||||
{count}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user