From 0a412c1362dba6adf38a1b2eef67a02db5512efb Mon Sep 17 00:00:00 2001 From: Strek Date: Sun, 22 Sep 2024 23:26:42 +0530 Subject: [PATCH] fix: minor bugs in right pane (#43) Co-authored-by: Amaresh S M --- src/components/scope/index.tsx | 9 ++++- src/components/scope/scope-item.tsx | 27 ++++++++++--- src/components/tree-entry.tsx | 60 +++++++++++++++++++++++------ src/lib/render-value.ts | 2 +- 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/src/components/scope/index.tsx b/src/components/scope/index.tsx index 4670d63..2a4273a 100644 --- a/src/components/scope/index.tsx +++ b/src/components/scope/index.tsx @@ -45,14 +45,21 @@ export const Scope: FC = () => { <> {scopeManager.scopes.map((subScope, index) => ( ))} ) : ( - + )} ); diff --git a/src/components/scope/scope-item.tsx b/src/components/scope/scope-item.tsx index dbc6f70..838291e 100644 --- a/src/components/scope/scope-item.tsx +++ b/src/components/scope/scope-item.tsx @@ -8,35 +8,50 @@ import { TreeEntry } from "../tree-entry"; import type { FC } from "react"; type ScopeItemProperties = { + isArray: boolean; readonly index: number; + readonly path: string; readonly data: Scope | Variable | Reference | null; }; -export const ScopeItem: FC = ({ data, index }) => { +export const ScopeItem: FC = ({ + data, + index, + path, + isArray, +}) => { if (!data) { return null; } - let key = "unknown"; + let key; if (data instanceof Scope) { key = data.type; } else if (data instanceof Variable) { key = data.name; + } else if (data instanceof Reference) { + key = data.identifier.name; + } else { + key = (data as Record)?.type ?? typeof data; } return ( - {Math.max(index, 0)}. {key} + {isArray && `${Math.max(index, 0)}.`} {key}
- {Object.entries(data).map(item => ( - + {Object.entries(data).map((item, index) => ( + ))}
diff --git a/src/components/tree-entry.tsx b/src/components/tree-entry.tsx index 4c2ee99..d2f3c83 100644 --- a/src/components/tree-entry.tsx +++ b/src/components/tree-entry.tsx @@ -7,6 +7,7 @@ import type { FC, ReactNode } from "react"; type TreeEntryProperties = { readonly data: [string, unknown]; + readonly path?: string; }; const isProbablyNode = (value: unknown) => { @@ -20,14 +21,32 @@ const isProbablyNode = (value: unknown) => { ); }; -const sanitizeValue = (value: unknown): ReactNode => { +const SanitizeValue = ({ + value, + path, + isArray, + index, +}: { + value: unknown; + isArray: boolean; + path?: string; + index: number; +}): ReactNode => { if (!value) { return null; } if (Array.isArray(value)) { if (typeof value[0] === "object") { - return value.map(sanitizeValue); + return value.map((item, index) => ( + + )); } return ( @@ -45,20 +64,30 @@ const sanitizeValue = (value: unknown): ReactNode => { isProbablyNode(value) ) { return ( -
- +
+
); } return ( -
-			{JSON.stringify(value, null, 2)}
-		
+
+ +
); }; -export const TreeEntry: FC = ({ data }) => { +export const TreeEntry: FC = ({ data, path }) => { const [key, value] = data; const [open, setOpen] = useState(false); const Icon = open ? MinusSquareIcon : PlusSquareIcon; @@ -68,7 +97,9 @@ export const TreeEntry: FC = ({ data }) => { return ( <>
- {typeof value === "object" || Array.isArray(value) ? ( + {(typeof value === "object" && + Object.values(value ?? {}).length) || + (Array.isArray(value) && value.length) ? (