opencode/packages/ui/src/components/diff.tsx

61 lines
1.7 KiB
TypeScript
Raw Normal View History

import { FileDiff } from "@pierre/precision-diffs"
import { getOrCreateWorkerPoolSingleton } from "@pierre/precision-diffs/worker"
import { createEffect, onCleanup, splitProps } from "solid-js"
import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
import { workerFactory } from "../pierre/worker"
const workerPool = getOrCreateWorkerPoolSingleton({
poolOptions: {
workerFactory,
// poolSize defaults to 8. More workers = more parallelism but
// also more memory. Too many can actually slow things down.
// poolSize: 8,
},
highlighterOptions: {
theme: "OpenCode",
// Optionally preload languages to avoid lazy-loading delays
// langs: ["typescript", "javascript", "css", "html"],
},
})
2025-10-24 21:26:17 +08:00
2025-10-25 00:23:32 +08:00
// interface ThreadMetadata {
// threadId: string
// }
2025-11-26 09:59:37 +08:00
//
//
2025-10-24 21:26:17 +08:00
2025-10-25 00:23:32 +08:00
export function Diff<T>(props: DiffProps<T>) {
let container!: HTMLDivElement
2025-11-08 09:59:02 +08:00
const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
2025-10-24 21:26:17 +08:00
2025-11-25 21:40:11 +08:00
let fileDiffInstance: FileDiff<T> | undefined
const cleanupFunctions: Array<() => void> = []
2025-10-24 21:26:17 +08:00
2025-10-25 00:23:32 +08:00
createEffect(() => {
2025-11-25 21:40:11 +08:00
container.innerHTML = ""
if (!fileDiffInstance) {
fileDiffInstance = new FileDiff<T>(
{
...createDefaultOptions(props.diffStyle),
...others,
},
workerPool,
)
2025-11-25 21:40:11 +08:00
}
2025-11-25 21:47:25 +08:00
fileDiffInstance.render({
2025-11-25 21:40:11 +08:00
oldFile: local.before,
newFile: local.after,
lineAnnotations: local.annotations,
containerWrapper: container,
})
})
onCleanup(() => {
// Clean up FileDiff event handlers and dispose SolidJS components
fileDiffInstance?.cleanUp()
cleanupFunctions.forEach((dispose) => dispose())
2025-10-24 21:26:17 +08:00
})
return <div data-component="diff" style={styleVariables} ref={container} />
2025-10-24 21:26:17 +08:00
}