diff.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { FileDiff } from "@pierre/diffs"
  2. import { createEffect, createMemo, onCleanup, splitProps } from "solid-js"
  3. import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
  4. import { workerPool } from "../pierre/worker"
  5. // interface ThreadMetadata {
  6. // threadId: string
  7. // }
  8. //
  9. //
  10. export function Diff<T>(props: DiffProps<T>) {
  11. let container!: HTMLDivElement
  12. const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
  13. const fileDiff = createMemo(
  14. () =>
  15. new FileDiff<T>(
  16. {
  17. ...createDefaultOptions(props.diffStyle),
  18. ...others,
  19. },
  20. workerPool,
  21. ),
  22. )
  23. const cleanupFunctions: Array<() => void> = []
  24. createEffect(() => {
  25. container.innerHTML = ""
  26. fileDiff().render({
  27. oldFile: local.before,
  28. newFile: local.after,
  29. lineAnnotations: local.annotations,
  30. containerWrapper: container,
  31. })
  32. })
  33. onCleanup(() => {
  34. // Clean up FileDiff event handlers and dispose SolidJS components
  35. fileDiff()?.cleanUp()
  36. cleanupFunctions.forEach((dispose) => dispose())
  37. })
  38. return <div data-component="diff" style={styleVariables} ref={container} />
  39. }