diff.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { checksum } from "@opencode-ai/util/encode"
  2. import { FileDiff } from "@pierre/diffs"
  3. import { createEffect, createMemo, onCleanup, splitProps } from "solid-js"
  4. import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
  5. import { getWorkerPool } from "../pierre/worker"
  6. export function Diff<T>(props: DiffProps<T>) {
  7. let container!: HTMLDivElement
  8. const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
  9. const options = createMemo(() => ({
  10. ...createDefaultOptions(props.diffStyle),
  11. ...others,
  12. }))
  13. let instance: FileDiff<T> | undefined
  14. createEffect(() => {
  15. const opts = options()
  16. const workerPool = getWorkerPool(props.diffStyle)
  17. const annotations = local.annotations
  18. instance?.cleanUp()
  19. instance = new FileDiff<T>(opts, workerPool)
  20. container.innerHTML = ""
  21. instance.render({
  22. oldFile: {
  23. ...local.before,
  24. cacheKey: checksum(local.before.contents),
  25. },
  26. newFile: {
  27. ...local.after,
  28. cacheKey: checksum(local.after.contents),
  29. },
  30. lineAnnotations: annotations,
  31. containerWrapper: container,
  32. })
  33. })
  34. onCleanup(() => {
  35. instance?.cleanUp()
  36. })
  37. return <div data-component="diff" style={styleVariables} ref={container} />
  38. }