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 glitch in markdown ast preview #65

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/components/tree-entry.tsx
amareshsm marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,41 @@ export const TreeEntry: FC<TreeEntryProperties> = ({ data, path }) => {
const [key, value] = data;
const [open, setOpen] = useState(false);
const Icon = open ? MinusSquareIcon : PlusSquareIcon;

const toggleOpen = () => setOpen(!open);
const isObject = typeof value === "object" && value !== null;
const isExpandable =
isObject &&
(Array.isArray(value)
? value.length > 0
: Object.keys(value).length > 0);
const values = renderValue(value);
const renderParts = values.map((part, partIndex) => (
<span
key={partIndex}
className={`${values.length === 1 ? "flex-1" : ""} ${partIndex ? "text-muted-foreground" : "text-primary"}`}
amareshsm marked this conversation as resolved.
Show resolved Hide resolved
>
{part}
</span>
));

return (
<>
<div className="flex items-center gap-3">
{(typeof value === "object" &&
Object.values(value ?? {}).length) ||
(Array.isArray(value) && value.length) ? (
<button
onClick={toggleOpen}
aria-label="Toggle"
type="button"
>
<Icon size={16} className="text-muted-foreground" />
</button>
) : (
<div className="w-4 h-4" />
)}
{key && <span>{key}</span>}
{renderValue(value).map((part, partIndex) => (
<span
key={partIndex}
className={
partIndex ? "text-muted-foreground" : "text-primary"
}
>
{part}
</span>
))}
<>
amareshsm marked this conversation as resolved.
Show resolved Hide resolved
{isExpandable ? (
<button
onClick={toggleOpen}
aria-label="Toggle"
type="button"
>
<Icon size={16} className="text-muted-foreground" />
</button>
) : (
<div className="w-4 h-4"></div>
)}
{key && <span className="flex-none">{key}</span>}
{renderParts}
</>
</div>
{open ? (
<SanitizeValue
Expand Down