113 lines · 4.0 KB
import { ClientOnly } from "@tanstack/react-router";
import { parsePatchFiles } from "@pierre/diffs";
import type { DiffLineAnnotation, FileDiffMetadata } from "@pierre/diffs";
import { FileDiff } from "@pierre/diffs/react";
import { useState } from "react";
import { Button } from "./ui/Button";
import { EmptyState } from "./ui/Primitives";
import type { DiffResult } from "~/lib/types";

export type DiffSide = "LEFT" | "RIGHT";

export type LineAnchor<T> = { path: string; line: number; side: DiffSide; data: T };

type Props<T> = {
  diff: DiffResult;
  anchors?: LineAnchor<T>[];
  renderAnchor?: (anchor: LineAnchor<T>) => React.ReactNode;
  onLineAction?: (path: string, line: number, side: DiffSide) => void;
};

const DIFF_STYLES = ["split", "unified"] as const;

function toAnnotationSide(side: DiffSide): "additions" | "deletions" {
  return side === "RIGHT" ? "additions" : "deletions";
}

function FileBlock<T>({
  fileDiff,
  diffStyle,
  anchors,
  renderAnchor,
  onLineAction,
}: {
  fileDiff: FileDiffMetadata;
  diffStyle: (typeof DIFF_STYLES)[number];
  anchors: LineAnchor<T>[];
  renderAnchor?: (anchor: LineAnchor<T>) => React.ReactNode;
  onLineAction?: (path: string, line: number, side: DiffSide) => void;
}) {
  const annotations: DiffLineAnnotation<LineAnchor<T>>[] = anchors.map((anchor) => ({