summaryrefslogtreecommitdiffhomepage
path: root/packages/ui/src/components/diff.tsx
blob: 8e19c3172c06ec7698c813f71eac6758b48fd49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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"],
  },
})

// interface ThreadMetadata {
//   threadId: string
// }
//
//

export function Diff<T>(props: DiffProps<T>) {
  let container!: HTMLDivElement
  const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])

  let fileDiffInstance: FileDiff<T> | undefined
  const cleanupFunctions: Array<() => void> = []

  createEffect(() => {
    container.innerHTML = ""
    if (!fileDiffInstance) {
      fileDiffInstance = new FileDiff<T>(
        {
          ...createDefaultOptions(props.diffStyle),
          ...others,
        },
        workerPool,
      )
    }
    fileDiffInstance.render({
      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())
  })

  return <div data-component="diff" style={styleVariables} ref={container} />
}