import { parseDiffFromFile, type FileContents } from '@pierre/diffs'; import { type FileDiffMetadata, type FileDiffProps, FileDiff, } from '@pierre/diffs/react'; import type { MarkRequired } from 'ts-essentials'; import type { Changeset } from 'json-diff-ts'; import { MSErrorBoundary } from '../ErrorBoundary'; import { patchObject } from "../../../core/DataUtils"; type DiffableVal = [] | object | string; interface JsonDiffOptionalProps { right?: DiffableVal, left: DiffableVal diff?: Changeset diffOpts?: FileDiffProps } export type JsonDiffProps = MarkRequired | MarkRequired const JsonDiffPatchComponent = (props: JsonDiffProps) => { const { right: rightVal, left, diff, diffOpts = {}, } = props; if(rightVal === undefined && diff === undefined) { throw new Error(`Must provide either 'right' or 'diff' props`); } let right = rightVal; if(right === undefined) { right = patchObject(structuredClone(left), diff); } const fileDiff: FileDiffMetadata = parseDiffFromFile(valToFileContents(left), valToFileContents(right)); return } export const JsonDiffPatch = (props: JsonDiffProps) => const valToFileContents = (val: DiffableVal, fileContentOpts: Partial = {}): FileContents => { let strContent: string; const { name = 'Content' } = fileContentOpts; const fileName: string = name; let lang: FileContents['lang']; if(typeof val === 'string') { strContent = val; } else { strContent = JSON.stringify(val, undefined, 2); lang = 'json'; } return { name: fileName, lang, contents: strContent }; }