Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffRendering.ts

112 lines3.1 KB

1import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2import type { FileDiffMetadata } from "@pierre/diffs/types";
3​
4export const DIFF_THEME_NAMES = {
5 light: "pierre-light",
6 dark: "pierre-dark",
7} as const;
8​
9export type DiffThemeName = (typeof DIFF_THEME_NAMES)[keyof typeof DIFF_THEME_NAMES];
10​
11export function resolveDiffThemeName(theme: "light" | "dark"): DiffThemeName {
12 return theme === "dark" ? DIFF_THEME_NAMES.dark : DIFF_THEME_NAMES.light;
14​
15const FNV_OFFSET_BASIS_32 = 0x811c9dc5;
16const FNV_PRIME_32 = 0x01000193;
17const SECONDARY_HASH_SEED = 0x9e3779b9;
18const SECONDARY_HASH_MULTIPLIER = 0x85ebca6b;
19​
20export function fnv1a32(
21 input: string,
22 seed = FNV_OFFSET_BASIS_32,
23 multiplier = FNV_PRIME_32,
24): number {
25 let hash = seed >>> 0;
26 for (let index = 0; index < input.length; index += 1) {
27 hash ^= input.charCodeAt(index);
28 hash = Math.imul(hash, multiplier) >>> 0;
29 }
30 return hash >>> 0;
32​
33export function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
34 const normalizedPatch = patch.trim();
35 const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
36 const secondary = fnv1a32(
37 normalizedPatch,
38 SECONDARY_HASH_SEED,
39 SECONDARY_HASH_MULTIPLIER,
40 ).toString(36);
41 return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
43​
44export type RenderablePatch =
45 | {
46 kind: "files";
47 files: FileDiffMetadata[];
48 }
49 | {
50 kind: "raw";
51 text: string;
52 reason: string;
53 };
54​
55export function getRenderablePatch(
56 patch: string | undefined,
57 cacheScope = "diff-panel",
58): RenderablePatch | null {
59 if (!patch) return null;
60 const normalizedPatch = patch.trim();
61 if (normalizedPatch.length === 0) return null;
62​
63 try {
64 const parsedPatches = parsePatchFiles(
65 normalizedPatch,
66 buildPatchCacheKey(normalizedPatch, cacheScope),
67 );
68 const files = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
69 if (files.length > 0) {
70 return { kind: "files", files };
71 }
72​
73 return {
74 kind: "raw",
75 text: normalizedPatch,
76 reason: "Unsupported diff format. Showing raw patch.",
77 };
78 } catch {
79 return {
80 kind: "raw",
81 text: normalizedPatch,
82 reason: "Failed to parse patch. Showing raw patch.",
83 };
84 }
86​
87export function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
88 const raw = fileDiff.name ?? fileDiff.prevName ?? "";
89 if (raw.startsWith("a/") || raw.startsWith("b/")) {
90 return raw.slice(2);
91 }
92 return raw;
94​
95export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
96 return fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
98​
99export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
100 switch (fileDiff.type) {
101 case "new":
102 return "text-[var(--diffs-addition-base)]";
103 case "deleted":
104 return "text-[var(--diffs-deletion-base)]";
105 case "change":
106 case "rename-pure":
107 case "rename-changed":
108 return "text-[var(--diffs-modified-base)]";
109 default:
110 return "text-muted-foreground/80";
111 }