Skip to content

Commit

Permalink
fix(output): improve render value behavior
Browse files Browse the repository at this point in the history
now can handle more values like 'undefined'
  • Loading branch information
Pkcarreno committed Oct 6, 2024
1 parent 53e29c9 commit 81e6e13
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions src/features/editor/components/output/format-output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,40 @@ import type { Loggable } from '@/features/editor/types';
import { JsonView } from './json-view-wrapper';

interface Props {
value: Loggable;
value: Loggable | Record<string, Loggable>;
topLevel?: boolean;
}

export const FormatOutput: React.FC<Props> = ({ value }) => {
if (typeof value === 'string') {
return <span className="whitespace-pre-wrap break-all">{value}</span>;
export const FormatOutput: React.FC<Props> = ({ value, topLevel = true }) => {
if (typeof value === 'string' && value === '') {
return <span className="text-muted italic">empty</span>;
}

if (value && Array.isArray(value) && value.length > 0) {
return value.map((subValue, index) => {
if (subValue && typeof subValue === 'string') {
return (
<div key={`string-${index}`} className="w-fit">
<span className="whitespace-pre-wrap break-all">{subValue}</span>
</div>
);
}
if (Array.isArray(value) && topLevel) {
return value.map((subValue, index) => (
<FormatOutput key={index} value={subValue} topLevel={false} />
));
}

if (subValue && typeof subValue === 'number') {
return (
<div key={`number-${index}`}>
<span className="w-fit text-[var(--w-rjv-key-number)]">
{subValue}
</span>
</div>
);
}
if (typeof value === 'string') {
return <span className="whitespace-pre-wrap break-all">{value}</span>;
}

if (
subValue &&
(typeof subValue === 'object' || Array.isArray(subValue))
) {
return (
<div key={`object-${index}`} className="w-fit">
<JsonView value={subValue} />
</div>
);
}
});
if (typeof value === 'number') {
return <span className="w-fit text-[hsl(var(--yell))]">{value}</span>;
}

if (value && (typeof value === 'object' || Array.isArray(value))) {
return (
<div className="w-fit">
<div className="my-auto w-fit">
<JsonView value={value} />
</div>
);
}

if (value && Array.isArray(value) && value.length === 0) {
return (
<div>
<span className="text-muted-foreground italic">empty</span>
</div>
);
if (!value) {
return <span className="text-muted w-fit">{String(value)}</span>;
}

// return <>{value}</>;
return <span>{String(value)}</span>;
};

0 comments on commit 81e6e13

Please sign in to comment.