mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-31 19:45:15 +02:00
fix(json-view): coerce numeric strings to numbers on edit
This commit is contained in:
parent
cdb7ffb8d7
commit
38b73858b2
1 changed files with 18 additions and 1 deletions
|
|
@ -35,6 +35,23 @@ export interface JsonViewProps {
|
||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Recursively coerce string values that are valid JSON numbers back to numbers.
|
||||||
|
* react-json-view's text input always yields strings; this restores the
|
||||||
|
* correct type so filters like ``{ "folder_id": 56 }`` survive editing. */
|
||||||
|
function coerceNumbers(value: unknown): unknown {
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const n = Number(value);
|
||||||
|
return !Number.isNaN(n) && value.trim() !== "" ? n : value;
|
||||||
|
}
|
||||||
|
if (Array.isArray(value)) return value.map(coerceNumbers);
|
||||||
|
if (value && typeof value === "object") {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(value as Record<string, unknown>).map(([k, v]) => [k, coerceNumbers(v)])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
const DARK_THEME = "monokai" as const;
|
const DARK_THEME = "monokai" as const;
|
||||||
const LIGHT_THEME = "rjv-default" as const;
|
const LIGHT_THEME = "rjv-default" as const;
|
||||||
|
|
||||||
|
|
@ -67,7 +84,7 @@ export function JsonView({
|
||||||
|
|
||||||
const handleChange = useCallback(
|
const handleChange = useCallback(
|
||||||
(interaction: InteractionProps) => {
|
(interaction: InteractionProps) => {
|
||||||
onChange?.(interaction.updated_src);
|
onChange?.(coerceNumbers(interaction.updated_src));
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[onChange]
|
[onChange]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue