Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor bugs in right pane #43

Merged
merged 13 commits into from
Sep 22, 2024
1 change: 1 addition & 0 deletions src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { markdown } from "@codemirror/lang-markdown";
import { EditorView } from "@codemirror/view";
import { EditorState } from "@codemirror/state";
import clsx from "clsx";
import { LanguageSupport } from "@codemirror/language";
import { debounce } from "../lib/utils";
import { LanguageSupport } from "@codemirror/language";

Expand Down
7 changes: 6 additions & 1 deletion src/components/scope/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ export const Scope: FC = () => {
<>
{scopeManager.scopes.map((subScope, index) => (
<ScopeItem
isArray={Array.isArray(scopeManager.scopes)}
key={index}
data={subScope}
index={index + 1}
/>
))}
</>
) : (
<ScopeItem data={scopeManager.globalScope} index={-1} />
<ScopeItem
isArray={Array.isArray(scopeManager.globalScope)}
data={scopeManager.globalScope}
index={-1}
/>
)}
</Accordion>
);
Expand Down
11 changes: 9 additions & 2 deletions src/components/scope/scope-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import { TreeEntry } from "../tree-entry";
import type { FC } from "react";

type ScopeItemProperties = {
isArray: boolean;
readonly index: number;
readonly data: Scope | Variable | Reference | null;
};

export const ScopeItem: FC<ScopeItemProperties> = ({ data, index }) => {
export const ScopeItem: FC<ScopeItemProperties> = ({
data,
index,
isArray,
}) => {
if (!data) {
return null;
}
Expand All @@ -23,6 +28,8 @@ export const ScopeItem: FC<ScopeItemProperties> = ({ data, index }) => {
key = data.type;
} else if (data instanceof Variable) {
key = data.name;
} else if (data instanceof Reference) {
key = data.identifier.name;
}

return (
Expand All @@ -31,7 +38,7 @@ export const ScopeItem: FC<ScopeItemProperties> = ({ data, index }) => {
className="border rounded-lg overflow-hidden"
>
<AccordionTrigger className="text-sm bg-muted-foreground/5 px-4 py-3 capitalize">
{Math.max(index, 0)}. {key}
{isArray ? `${Math.max(index, 0)}.` : null} {key}
</AccordionTrigger>
<AccordionContent className="p-4 border-t">
<div className="space-y-1">
Expand Down
33 changes: 29 additions & 4 deletions src/components/tree-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@ const isProbablyNode = (value: unknown) => {
);
};

const sanitizeValue = (value: unknown): ReactNode => {
const SanitizeValue = ({
value,
isArray,
index,
}: {
value: unknown;
isArray: boolean;
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) => (
<SanitizeValue
key={index}
value={item}
index={index}
isArray={Array.isArray(value)}
/>
));
}

return (
Expand All @@ -46,7 +61,11 @@ const sanitizeValue = (value: unknown): ReactNode => {
) {
return (
<div className="mt-3 space-y-3 ml-2">
<ScopeItem data={value as Scope} index={0} />
<ScopeItem
isArray={isArray}
data={value as Scope}
index={index ? index : 0}
/>
</div>
);
}
Expand Down Expand Up @@ -91,7 +110,13 @@ export const TreeEntry: FC<TreeEntryProperties> = ({ data }) => {
</span>
))}
</div>
{open ? sanitizeValue(value) : null}
{open ? (
<SanitizeValue
value={value}
isArray={Array.isArray(value)}
index={0}
/>
) : null}
</>
);
};