feat(SearchableJSONViewer): cover no result case

This commit is contained in:
Rıdvan Akca
2023-03-10 17:58:06 +03:00
parent 3e0149c058
commit aebfcc38dd
2 changed files with 20 additions and 8 deletions

View File

@@ -2,11 +2,13 @@ import * as React from 'react';
import get from 'lodash/get'; import get from 'lodash/get';
import set from 'lodash/set'; import set from 'lodash/set';
import throttle from 'lodash/throttle'; import throttle from 'lodash/throttle';
import { Box } from '@mui/material'; import isEmpty from 'lodash/isEmpty';
import { Box, Typography } from '@mui/material';
import { IJSONObject, IJSONValue } from '@automatisch/types'; import { IJSONObject, IJSONValue } from '@automatisch/types';
import JSONViewer from 'components/JSONViewer'; import JSONViewer from 'components/JSONViewer';
import SearchInput from 'components/SearchInput'; import SearchInput from 'components/SearchInput';
import useFormatMessage from 'hooks/useFormatMessage';
type JSONViewerProps = { type JSONViewerProps = {
data: IJSONObject; data: IJSONObject;
@@ -15,7 +17,10 @@ type JSONViewerProps = {
type Entry = [string, IJSONValue]; type Entry = [string, IJSONValue];
const SearchableJSONViewer = ({ data }: JSONViewerProps) => { const SearchableJSONViewer = ({ data }: JSONViewerProps) => {
const [filteredData, setFilteredData] = React.useState(data); const [filteredData, setFilteredData] = React.useState<IJSONObject | null>(
data
);
const formatMessage = useFormatMessage();
const allEntries = React.useMemo(() => { const allEntries = React.useMemo(() => {
const entries: Entry[] = []; const entries: Entry[] = [];
@@ -23,7 +28,6 @@ const SearchableJSONViewer = ({ data }: JSONViewerProps) => {
for (const key in obj) { for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) { if (typeof obj[key] === 'object' && obj[key] !== null) {
entries.push([[prefix, key].filter(Boolean).join('.'), obj[key]]); entries.push([[prefix, key].filter(Boolean).join('.'), obj[key]]);
collectEntries( collectEntries(
obj[key] as IJSONObject, obj[key] as IJSONObject,
[prefix, key].filter(Boolean).join('.') [prefix, key].filter(Boolean).join('.')
@@ -60,17 +64,24 @@ const SearchableJSONViewer = ({ data }: JSONViewerProps) => {
} }
}); });
setFilteredData(newFilteredData); if (isEmpty(newFilteredData)) {
setFilteredData(null);
} else {
setFilteredData(newFilteredData);
}
}, 400), }, 400),
[allEntries] [allEntries]
); );
return ( return (
<> <>
<Box mb={1} mt={2}> <Box my={2}>
<SearchInput onChange={onSearchChange} /> <SearchInput onChange={onSearchChange} />
</Box> </Box>
<JSONViewer data={filteredData} /> {filteredData && <JSONViewer data={filteredData} />}
{!filteredData && (
<Typography>{formatMessage('jsonViewer.noDataFound')}</Typography>
)}
</> </>
); );
}; };

View File

@@ -138,5 +138,6 @@
"resetPasswordForm.confirmPasswordFieldLabel": "Confirm password", "resetPasswordForm.confirmPasswordFieldLabel": "Confirm password",
"resetPasswordForm.passwordUpdated": "The password has been updated. Now, you can login.", "resetPasswordForm.passwordUpdated": "The password has been updated. Now, you can login.",
"usageAlert.informationText": "Tasks: {consumedTaskCount}/{allowedTaskCount} (Resets {relativeResetDate})", "usageAlert.informationText": "Tasks: {consumedTaskCount}/{allowedTaskCount} (Resets {relativeResetDate})",
"usageAlert.viewPlans": "View plans" "usageAlert.viewPlans": "View plans",
} "jsonViewer.noDataFound": "We couldn't find anything matching your search"
}