Skip to content

Commit

Permalink
feat: Syntax highlight for job input and output data
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Sep 18, 2024
1 parent 53fdf64 commit 02298ab
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"react-dom": "^18.3.1",
"react-hook-form": "^7.52.2",
"react-router-dom": "^6.26.0",
"react-syntax-highlighter": "^15.5.0",
"tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7",
"timeago.js": "4.0.0-beta.3",
Expand All @@ -49,6 +50,7 @@
"@types/node": "^22.1.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.13",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.20",
"postcss": "^8.4.41",
Expand Down
20 changes: 3 additions & 17 deletions packages/dashboard/src/components/JobView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JobLogs } from "./JobLogs";
import AlertCircle from "lucide-react/icons/alert-circle";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { getDurationStr } from "@/lib/helpers";
import { JsonHighlight } from "./JsonHighlight";
import type { JobDto } from "@/tsr";

type JobViewProps = {
Expand All @@ -27,11 +28,11 @@ export function JobView({ job }: JobViewProps) {
<div>
<div className="mb-4">
<div className="mb-2">Input</div>
<Format data={job.inputData} />
<JsonHighlight json={job.inputData} />
</div>
<div>
<div className="mb-2">Output</div>
<Format data={job.outputData} />
{job.outputData ? <JsonHighlight json={job.outputData} /> : null}
</div>
</div>
<div>
Expand All @@ -43,21 +44,6 @@ export function JobView({ job }: JobViewProps) {
);
}

function Format({ data }: { data: string | null }) {
let parsedData: unknown;
try {
if (data) {
parsedData = JSON.parse(data);
}
} catch {}

return parsedData ? (
<pre className="p-2 text-xs border border-border rounded-md whitespace-pre-wrap break-all">
{JSON.stringify(parsedData, null, 2)}
</pre>
) : null;
}

function JobError({ error }: { error: string }) {
return (
<Alert variant="destructive" className="mb-4">
Expand Down
36 changes: 36 additions & 0 deletions packages/dashboard/src/components/JsonHighlight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useMemo } from "react";

// @ts-expect-error
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";

// @ts-expect-error
import json from "react-syntax-highlighter/dist/esm/languages/hljs/json";

// @ts-expect-error
import style from "react-syntax-highlighter/dist/esm/styles/hljs/stackoverflow-light";

style["hljs"].padding = "1rem";
delete style["hljs"].background;

SyntaxHighlighter.registerLanguage("json", json);

type SyntaxHighlightProps = {
json: string;
};

export function JsonHighlight({ json }: SyntaxHighlightProps) {
const data = useMemo(() => {
const parsed = JSON.parse(json);
return JSON.stringify(parsed, null, 2);
}, [json]);

return (
<SyntaxHighlighter
className="rounded-md text-xs border border-border"
style={style}
language="json"
>
{data}
</SyntaxHighlighter>
);
}
2 changes: 1 addition & 1 deletion packages/dashboard/src/components/OpenApiReference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type OpenApiReferenceProps = {
};

export function OpenApiReference({ url }: OpenApiReferenceProps) {
const { data } = tsr.getSpec.useQuery({
const { data } = tsr.getSpec.useSuspenseQuery({
queryKey: ["spec"],
});

Expand Down
Loading

0 comments on commit 02298ab

Please sign in to comment.