Skip to content

Add GeoJSON file rendering support and amend preview support in ObjectsDiff #9014

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

Merged
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
61 changes: 21 additions & 40 deletions webui/src/lib/components/repository/ObjectsDiff.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,9 @@ import {useStorageConfigs} from "../../hooks/storageConfig";
import {AppContext} from "../../hooks/appContext";
import {useRefs} from "../../hooks/repo";
import {getRepoStorageConfig} from "../../../pages/repositories/repository/utils";
import {GeoJSONPreview} from "./GeoJSONPreview";

const maxDiffSizeBytes = 120 << 10;
const DiffViewerType = {
GEOJSON: 'geojson',
REACT: 'react',
};
const diffViewersByExtension = {
geojson: DiffViewerType.GEOJSON,
txt: DiffViewerType.REACT,
text: DiffViewerType.REACT,
md: DiffViewerType.REACT,
csv: DiffViewerType.REACT,
tsv: DiffViewerType.REACT,
yaml: DiffViewerType.REACT,
yml: DiffViewerType.REACT,
json: DiffViewerType.REACT,
jsonl: DiffViewerType.REACT,
ndjson: DiffViewerType.REACT,
};

const getViewer = (path) => {
const ext = path.split(".").pop().toLowerCase();
return diffViewersByExtension[ext] || null;
};
const supportedReadableFormats = ["txt", "text", "md", "csv", "tsv", "yaml", "yml", "json", "jsonl", "ndjson", "geojson"];

export const ObjectsDiff = ({diffType, repoId, leftRef, rightRef, path}) => {
const {repo, error: refsError, loading: refsLoading} = useRefs();
Expand All @@ -45,7 +23,7 @@ export const ObjectsDiff = ({diffType, repoId, leftRef, rightRef, path}) => {

if (hooksError) return <AlertError error={hooksError}/>;

const viewer = getViewer(path);
const readable = readableObject(path);
let left;
let right;
switch (diffType) {
Expand Down Expand Up @@ -74,7 +52,7 @@ export const ObjectsDiff = ({diffType, repoId, leftRef, rightRef, path}) => {

const leftStat = left && left.response;
const rightStat = right && right.response;
if (!viewer) {
if (!readable) {
return <NoContentDiff left={leftStat} right={rightStat} diffType={diffType}/>;
}
const objectTooBig = (leftStat && leftStat.size_bytes > maxDiffSizeBytes) || (rightStat && rightStat.size_bytes > maxDiffSizeBytes);
Expand All @@ -88,8 +66,17 @@ export const ObjectsDiff = ({diffType, repoId, leftRef, rightRef, path}) => {
leftSize={leftSize} rightSize={rightSize} diffType={diffType}/>;
}

function readableObject(path) {
for (const ext of supportedReadableFormats) {
if (path.endsWith("." + ext)) {
return true;
}
}
return false;
}

const NoContentDiff = ({left, right, diffType}) => {
const supportedFileExtensions = Object.keys(diffViewersByExtension).map((ext) => `.${ext}`);
const supportedFileExtensions = supportedReadableFormats.map((fileType) => `.${fileType}`);
return <div>
<span><StatDiff left={left} right={right} diffType={diffType}/></span>
<span><Alert variant="light"><InfoIcon/> {`lakeFS supports content diff for ${supportedFileExtensions.join(',')} file formats only`}</Alert></span>
Expand All @@ -108,22 +95,16 @@ const ContentDiff = ({config, repoId, path, leftRef, rightRef, leftSize, rightSi
const err = (left && left.error) || (right && right.err);
if (err) return <AlertError error={err}/>;

const viewer = getViewer(path);
if (!viewer) return null;

return <div>
<DiffSizeReport leftSize={leftSize} rightSize={rightSize} diffType={diffType}/>
{
viewer === DiffViewerType.GEOJSON
? <GeoJSONPreview data={right?.response} />
: <ReactDiffViewer
oldValue={left?.response}
newValue={right?.response}
splitView={false}
useDarkTheme={state.settings.darkMode}
compareMethod={DiffMethod.WORDS}
/>
}
<ReactDiffViewer
oldValue={left?.response}
newValue={right?.response}
splitView={false}
useDarkTheme={state.settings.darkMode}
compareMethod={DiffMethod.WORDS}
/>

</div>;
}

Expand Down
10 changes: 10 additions & 0 deletions webui/src/pages/repositories/repository/fileRenderers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SyntaxHighlighter from "react-syntax-highlighter";
import {FileType, RendererComponent} from "./types";
import {DuckDBRenderer} from "./data";
import {
GeoJSONRenderer,
ImageRenderer,
IpynbRenderer,
MarkdownRenderer,
Expand Down Expand Up @@ -48,6 +49,11 @@ export const Renderers: {[fileType in FileType] : FC<RendererComponent> } = {
[FileType.TOO_LARGE]: props => (
<ObjectTooLarge {...props}/>
),
[FileType.GEOJSON]: props => (
<TextDownloader {...props} onReady={text =>
<GeoJSONRenderer {...props} text={text}/>
} />
),
}

export const guessLanguage = (extension: string | null, contentType: string | null) => {
Expand Down Expand Up @@ -102,6 +108,8 @@ export function guessType(contentType: string | null, fileExtension: string | nu
case 'application/x-toml':
case 'application/toml':
return FileType.TEXT
case 'application/geo+json':
return FileType.GEOJSON;
case 'application/x-ipynb+json':
case 'application/x-ipynb':
case 'application/ipynb':
Expand Down Expand Up @@ -144,6 +152,8 @@ export function guessType(contentType: string | null, fileExtension: string | nu
case 'jsonl':
case 'ndjson':
return FileType.TEXT
case 'geojson':
return FileType.GEOJSON
}
if (guessLanguage(fileExtension, contentType))
return FileType.TEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import "react-ipynb-renderer/dist/styles/default.css";
import { useMarkdownProcessor } from "./useMarkdownProcessor";
import { AppContext } from "../../../../lib/hooks/appContext";
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";
import {GeoJSONPreview} from "../../../../lib/components/repository/GeoJSONPreview";

export const ObjectTooLarge: FC<RendererComponent> = ({ path, sizeBytes }) => {
return (
Expand Down Expand Up @@ -146,3 +147,7 @@ export const PDFRenderer: FC<RendererComponent> = ({
</div>
);
};

export const GeoJSONRenderer: FC<RendererComponentWithText> = ({ text }) => {
return <GeoJSONPreview data={text} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export enum FileType {
TEXT,
UNSUPPORTED,
TOO_LARGE,
GEOJSON
}
Loading