From ff7948726f389bddbc7cb97c196a74f35e8992ee Mon Sep 17 00:00:00 2001 From: zihluwang Date: Tue, 24 Feb 2026 11:54:18 +0800 Subject: [PATCH] refactor: add TTY --- src/page/json-grid/index.tsx | 1 + src/page/json-viewer/index.tsx | 55 ++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/page/json-grid/index.tsx b/src/page/json-grid/index.tsx index a60a040..2b19391 100644 --- a/src/page/json-grid/index.tsx +++ b/src/page/json-grid/index.tsx @@ -9,6 +9,7 @@ export default function JsonGrid() { const { t } = useTranslation() const initialData = [ + { id: 0, name: "TTY", roles: "CEO", active: true }, { id: 1, name: "Alice", role: "Developer", active: true }, { id: 2, name: "Bob", role: "Designer", active: false }, { id: 3, name: "Charlie", role: "Product Manager", active: true }, diff --git a/src/page/json-viewer/index.tsx b/src/page/json-viewer/index.tsx index 5299b5a..0604347 100644 --- a/src/page/json-viewer/index.tsx +++ b/src/page/json-viewer/index.tsx @@ -15,6 +15,7 @@ export default function JsonViewer() { location: "London", is_active: true, staff_members: [ + { id: 100, name: "TTY", roles: ["CEO"] }, { id: 101, name: "Alice", roles: ["Admin", "Manager"] }, { id: 102, name: "Bob", roles: ["Developer"] }, ], @@ -39,7 +40,13 @@ export default function JsonViewer() { try { parsed = JSON.parse(jsonInput) } catch (e) { - return { parsed: null, matchedPaths: [], matchedValues: [], error: (e as Error).message, queryError: null } + return { + parsed: null, + matchedPaths: [], + matchedValues: [], + error: (e as Error).message, + queryError: null, + } } try { @@ -53,7 +60,13 @@ export default function JsonViewer() { } } catch (e) { // When JSONPath expression is invalid, still display the JSON tree but with no matches - return { parsed, matchedPaths: [], matchedValues: [], error: null, queryError: (e as Error).message } + return { + parsed, + matchedPaths: [], + matchedValues: [], + error: null, + queryError: (e as Error).message, + } } }, [jsonInput, query]) @@ -70,20 +83,23 @@ export default function JsonViewer() { } const objectMatches = result.matchedValues.filter(isPlainObject) - const isObjectTable = objectMatches.length > 0 && objectMatches.length === result.matchedValues.length + const isObjectTable = + objectMatches.length > 0 && objectMatches.length === result.matchedValues.length const csv = isObjectTable ? (() => { - const columns = Array.from(new Set(objectMatches.flatMap((item) => Object.keys(item)))) - const headerRow = columns.map(escapeCsvValue).join(",") - const valueRows = objectMatches.map((item) => columns.map((column) => escapeCsvValue(item[column])).join(",")) - return [headerRow, ...valueRows].join("\n") - })() + const columns = Array.from(new Set(objectMatches.flatMap((item) => Object.keys(item)))) + const headerRow = columns.map(escapeCsvValue).join(",") + const valueRows = objectMatches.map((item) => + columns.map((column) => escapeCsvValue(item[column])).join(",") + ) + return [headerRow, ...valueRows].join("\n") + })() : (() => { - const header = query - const rows = result.matchedValues.map(escapeCsvValue) - return [header, ...rows].join("\n") - })() + const header = query + const rows = result.matchedValues.map(escapeCsvValue) + return [header, ...rows].join("\n") + })() navigator.clipboard.writeText(csv).then(() => { setCopiedCsv(true) @@ -94,7 +110,8 @@ export default function JsonViewer() { const copySelectedRawJson = useCallback(() => { if (result.matchedValues.length === 0) return - const payload = result.matchedValues.length === 1 ? result.matchedValues[0] : result.matchedValues + const payload = + result.matchedValues.length === 1 ? result.matchedValues[0] : result.matchedValues const json = JSON.stringify(payload, null, 2) navigator.clipboard.writeText(json).then(() => { @@ -157,15 +174,13 @@ export default function JsonViewer() { @@ -178,11 +193,7 @@ export default function JsonViewer() { )} {result.parsed && ( - + )}