Skip to content

Commit

Permalink
fix: Allow JSONRenderer to fallback to code view if data is not tabular
Browse files Browse the repository at this point in the history
  • Loading branch information
baumandm committed May 25, 2022
1 parent 545e63c commit 81aa5eb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
11 changes: 8 additions & 3 deletions packages/frontend/src/components/file-viewer/file-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export interface FileViewerProps {
// Set true to indicate the rendered file is a conversion
isConverted?: boolean;

defaultMode?: 'rendered' | 'raw';
canRender?: boolean;

mime?: string;
path?: string;

Expand All @@ -131,6 +134,8 @@ export const FileViewer = ({
headerStyles = {},
isConverted = false,
mime,
defaultMode = 'rendered',
canRender = true,
path,
transformAssetUri,
...props
Expand All @@ -147,7 +152,7 @@ export const FileViewer = ({
const convertedDownloadUrl = isConverted ? getCompletePath(baseAssetUrl, path, true) : undefined;

const [paused, setPaused] = useState(true);
const [mode, setMode] = useState('rendered');
const [mode, setMode] = useState(defaultMode);
const unpause = () => paused && setPaused(false);
const pause = () => !paused && setPaused(true);

Expand All @@ -162,7 +167,7 @@ export const FileViewer = ({
});

let canDisplayRaw = false;
let canDisplayRendered = false;
let canDisplayRendered = canRender;
const canDownload = allowDownload;
let renderer: ReactElement | null = null;

Expand Down Expand Up @@ -334,7 +339,7 @@ export const FileViewer = ({
/>
</Tooltip>
)}
{canDisplayRendered && (
{canRender && canDisplayRendered && (
<Tooltip placement="bottom" label="Display rendered" aria-label="Display rendered">
<IconButton
aria-label="Display rendered"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
* limitations under the License.
*/

import { useMemo } from 'react';
import { useMemo, useState } from 'react';

import { Alert } from '../../alert/alert';
import { CodeRenderer } from '../code-renderer/code-renderer';
import { TableRenderer } from '../table-renderer/table-renderer';

export const JsonRenderer = ({ contents }) => {
const [error, setError] = useState(undefined);

const results = useMemo(() => {
if (contents) {
const data = JSON.parse(contents);

// Assume all the columns are in the first row
// We could extend this to take a larger sample of data to determine the columns
const columns = Object.keys(data[0]).map((column) => ({
Header: column,
accessor: column
}));

return {
columns,
data
};
try {
const data = JSON.parse(contents);

// Assume all the columns are in the first row
// We could extend this to take a larger sample of data to determine the columns
const columns = Object.keys(data[0]).map((column) => ({
Header: column,
accessor: column
}));

return {
columns,
data
};
} catch (error: any) {
setError(error);
}
}
}, [contents]);

if (error) {
// Fallback to displaying the contents as a code snippet
return <CodeRenderer contents={contents} language="json" />;
}
if (results === undefined) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export const InsightViewer = ({
element={
<FileViewer
mime="application/json"
defaultMode="raw"
canRender={false}
contents={JSON.stringify(insight, null, 2)}
baseAssetUrl={`/api/v1/insights/${insight.fullName}/assets`}
baseLinkUrl={`/${insight.itemType}/${insight.fullName}/files`}
Expand Down

0 comments on commit 81aa5eb

Please sign in to comment.