Proxy viewer extraction: response viewers into packages/ui (#754)

This commit is contained in:
Gregory Schier authored and GitHub committed 2026-10-14 21:32:19 -07:00
1 parent b5508e6bb1
commit b6c1f700f1
137 files changed
+3498 -2890

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import type { DependencyList } from "react";
import { useEffect, useState } from "react";
/**
* Like useState, except it will update the value when the default value changes
*/
export function useStateWithDeps<T>(defaultValue: T | (() => T), deps: DependencyList) {
const [value, setValue] = useState(defaultValue);
// oxlint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
setValue(defaultValue);
}, [...deps]);
return [value, setValue] as const;
}
+42
View File
@@ -0,0 +1,42 @@
import { atom, useAtom } from "jotai";
import { atomFamily } from "jotai-family";
import { useCallback, useState } from "react";
export type StoredValue = object | boolean | number | string | null;
export type SetStoredValue<T> = (value: T | ((prev: T) => T)) => void | Promise<unknown>;
/**
* Reads and writes a value kept under `key`, falling back to `fallback` until one is written. A
* `null` key keeps the value for the component's lifetime only. Components that remember things
* take one of these, so each host decides where the memory lives.
*/
export type UseStoredValue = <T extends StoredValue>(
key: string | string[] | null,
fallback: T,
) => readonly [T, SetStoredValue<T>];
const memoryAtoms = atomFamily((_key: string) => atom<{ value: unknown } | null>(null));
/** The default {@link UseStoredValue}: kept in memory for the session */
export function useInMemoryStoredValue<T extends StoredValue>(
key: string | string[] | null,
fallback: T,
): readonly [T, SetStoredValue<T>] {
const memoryKey = key == null ? null : Array.isArray(key) ? key.join("::") : key;
const [local, setLocal] = useState<T>(fallback);
const [stored, setStored] = useAtom(memoryAtoms(memoryKey ?? ""));
const storedValue = stored == null ? fallback : (stored.value as T);
const setStoredValue = useCallback<SetStoredValue<T>>(
(update) =>
setStored((prev) => {
const prevValue = prev == null ? fallback : (prev.value as T);
const value = typeof update === "function" ? (update as (p: T) => T)(prevValue) : update;
return { value };
}),
[fallback, setStored],
);
return memoryKey == null ? [local, setLocal] : [storedValue, setStoredValue];
}
+114
View File
@@ -0,0 +1,114 @@
import { atom, useAtom } from "jotai";
import { atomFamily } from "jotai-family";
import { useCallback, useState } from "react";
/** What's typed in the filter box. `null` means the filter box is closed */
const filterTextAtoms = atomFamily((_stateKey: string) => atom<string | null>(null));
/** What's actually applied to the text. Only changes on an explicit apply */
const appliedFilterAtoms = atomFamily((_stateKey: string) => atom<string | null>(null));
export interface TextFilterApi {
stateKey: string | null;
/** Draft text in the filter box, or `null` when the box is closed */
filterText: string | null;
/** The expression currently filtering the text */
appliedFilter: string | null;
isSearching: boolean;
/** The box holds an expression that isn't the one currently applied */
isDirty: boolean;
/** Bumped when the (uncontrolled) filter input must re-read its defaultValue */
filterUpdateKey: number;
setFilterText: (value: string | null) => void;
/** Apply the expression to the text, reporting it to `onApplied` if the filter accepts it */
applyFilter: (value: string) => void;
/** Like applyFilter, but also replaces what's shown in the filter box */
replaceFilter: (value: string) => void;
toggleSearch: () => void;
}
/**
* Draft/applied state for a text filter (JSONPath/XPath).
*
* `onApplied` hears at most once per apply gesture, and only after `runFilter`
* confirms the filter accepts the expression — the filter is the sole judge of
* validity. Because nothing but the gesture reports, refetches can't resurrect
* deleted history entries and a gesture can't report into another state key.
*/
export function useTextFilter({
stateKey,
runFilter,
onApplied,
}: {
stateKey: string | null;
/** Evaluate an expression, rejecting if the filter reports an error */
runFilter: (filter: string) => Promise<unknown>;
onApplied?: (filter: string) => void;
}): TextFilterApi {
const [storedFilterText, setStoredFilterText] = useAtom(filterTextAtoms(stateKey ?? ""));
const [storedAppliedFilter, setStoredAppliedFilter] = useAtom(appliedFilterAtoms(stateKey ?? ""));
const filterText = stateKey ? storedFilterText : null;
const appliedFilter = stateKey ? storedAppliedFilter : null;
const setFilterText = useCallback(
(v: string | null) => {
if (!stateKey) return;
setStoredFilterText(v);
},
[stateKey, setStoredFilterText],
);
const setAppliedFilter = useCallback(
(v: string | null) => {
if (!stateKey) return;
setStoredAppliedFilter(v);
},
[stateKey, setStoredAppliedFilter],
);
const applyFilter = useCallback(
(value: string) => {
setFilterText(value);
const applied = value.trim() === "" ? null : value.trim();
setAppliedFilter(applied);
if (applied == null) return;
runFilter(applied).then(
() => onApplied?.(applied),
() => {}, // Rejected by the filter — don't report
);
},
[setFilterText, setAppliedFilter, runFilter, onApplied],
);
const [filterUpdateKey, setFilterUpdateKey] = useState(0);
const replaceFilter = useCallback(
(value: string) => {
applyFilter(value);
setFilterUpdateKey((k) => k + 1);
},
[applyFilter],
);
const isSearching = filterText != null;
const toggleSearch = useCallback(() => {
if (isSearching) {
setFilterText(null);
setAppliedFilter(null);
} else {
setFilterText("");
}
}, [isSearching, setFilterText, setAppliedFilter]);
return {
stateKey,
filterText,
appliedFilter,
isSearching,
isDirty: filterText != null && filterText.trim() !== (appliedFilter ?? ""),
filterUpdateKey,
setFilterText,
applyFilter,
replaceFilter,
toggleSearch,
};
}