Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 26 additions & 16 deletions src/ReactUnipika/ReactUnipika.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

export interface ReactUnipikaProps {
settings?: UnipikaSettings;
value: any;

Check warning on line 15 in src/ReactUnipika/ReactUnipika.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
inline?: boolean;
children?: React.ReactNode;
extraTools?: React.ReactNode;
Expand All @@ -25,6 +25,7 @@
showContainerSize?: boolean;
initiallyCollapsed?: boolean;
caseInsensitiveSearch?: boolean;
renderError?: (error: unknown) => React.ReactNode;
}

const defaultUnipikaSettings = {
Expand All @@ -51,26 +52,31 @@
showContainerSize,
initiallyCollapsed,
caseInsensitiveSearch,
renderError,
}: ReactUnipikaProps) {
const convertedValue = React.useMemo(() => {
// TODO: fix me later
// The call is required because unipika.format() applies default values to a passed settings inplace.
// We have to leave this call without it the behaviour will be broken.
if (settings.format === 'raw-json') {
unipika.formatRaw(value, settings);
} else {
unipika.formatFromYSON(value, settings);
}
const {convertedValue, error} = React.useMemo(() => {
try {
// TODO: fix me later
// The call is required because unipika.format() applies default values to a passed settings inplace.
// We have to leave this call without it the behaviour will be broken.
if (settings.format === 'raw-json') {
unipika.formatRaw(value, settings);
} else {
unipika.formatFromYSON(value, settings);
}

if (value === undefined) {
return '';
}
if (value === undefined) {
return '';
}

if (settings.format === 'raw-json') {
return unipika.converters.raw(value, settings);
}
if (settings.format === 'raw-json') {
return unipika.converters.raw(value, settings);
}

return unipika.converters.yson(value, settings);
return {convertedValue: unipika.converters.yson(value, settings)};
} catch (error) {

Check warning on line 77 in src/ReactUnipika/ReactUnipika.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'error' is already declared in the upper scope
return {error};
}
Copy link
Collaborator

@ma-efremoff ma-efremoff Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {convertedValue, error} = React.useMemo(() => {
    try {
        ...
        return {convertedValue: unipika.converters.yson(value, settings)};
    } catch(error) {
        return {error}
    }
}, ...);

}, [value, settings]);

const classes = block(
Expand All @@ -80,6 +86,10 @@
className,
);

if (error) {
return renderError?.(error);
}

function getFormattedTitle() {
if (!inline) {
return undefined;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/ReactUnipika/__stories__/ReactUnipika.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ export const WithContainerSizeYson: StoryObj<ReactUnipikaProps> = {
showContainerSize: true,
},
};

export const WithError: StoryObj<ReactUnipikaProps> = {
args: {
value: {val: Infinity},
renderError: (error: unknown) => {
if (error instanceof Error) {
return <div>{error.message}</div>;
}
return 'Unknown error while parsing data';
},
},
};
6 changes: 6 additions & 0 deletions src/ReactUnipika/__tests__/ReactUnipika.visual.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,9 @@ test('ReactUnipika: search in collapsed - navigate forward', async ({

await expectScreenshot({component: page});
});

test('ReactUnipika: with error', async ({mount, expectScreenshot, page}) => {
await mount(<Stories.WithError />, {width: 1280});

await expectScreenshot({component: page});
});
Loading