diff.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { FileDiff } from "@pierre/precision-diffs"
  2. import { getOrCreateWorkerPoolSingleton } from "@pierre/precision-diffs/worker"
  3. import { createEffect, onCleanup, splitProps } from "solid-js"
  4. import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
  5. import { workerFactory } from "../pierre/worker"
  6. const workerPool = getOrCreateWorkerPoolSingleton({
  7. poolOptions: {
  8. workerFactory,
  9. // poolSize defaults to 8. More workers = more parallelism but
  10. // also more memory. Too many can actually slow things down.
  11. // poolSize: 8,
  12. },
  13. highlighterOptions: {
  14. theme: "OpenCode",
  15. // Optionally preload languages to avoid lazy-loading delays
  16. // langs: ["typescript", "javascript", "css", "html"],
  17. },
  18. })
  19. // interface ThreadMetadata {
  20. // threadId: string
  21. // }
  22. //
  23. //
  24. export function Diff<T>(props: DiffProps<T>) {
  25. let container!: HTMLDivElement
  26. const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
  27. let fileDiffInstance: FileDiff<T> | undefined
  28. const cleanupFunctions: Array<() => void> = []
  29. createEffect(() => {
  30. container.innerHTML = ""
  31. if (!fileDiffInstance) {
  32. fileDiffInstance = new FileDiff<T>(
  33. {
  34. ...createDefaultOptions(props.diffStyle),
  35. ...others,
  36. },
  37. workerPool,
  38. )
  39. }
  40. fileDiffInstance.render({
  41. oldFile: local.before,
  42. newFile: local.after,
  43. lineAnnotations: local.annotations,
  44. containerWrapper: container,
  45. })
  46. })
  47. onCleanup(() => {
  48. // Clean up FileDiff event handlers and dispose SolidJS components
  49. fileDiffInstance?.cleanUp()
  50. cleanupFunctions.forEach((dispose) => dispose())
  51. })
  52. return <div data-component="diff" style={styleVariables} ref={container} />
  53. }