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

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import { FileDiff } from "@pierre/precision-diffs"
2025-12-08 20:24:19 +08:00
import { createEffect, createMemo, onCleanup, splitProps } from "solid-js"
import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
2025-12-08 20:24:19 +08:00
import { workerPool } from "../pierre/worker"
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-12-08 20:24:19 +08:00
const fileDiff = createMemo(
() =>
new FileDiff<T>(
{
...createDefaultOptions(props.diffStyle),
...others,
},
workerPool,
2025-12-08 20:24:19 +08:00
),
)
const cleanupFunctions: Array<() => void> = []
createEffect(() => {
container.innerHTML = ""
fileDiff().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
2025-12-08 20:24:19 +08:00
fileDiff()?.cleanUp()
2025-11-25 21:40:11 +08:00
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
}