MCPcopy Create free account
hub / github.com/modem-dev/hunk / useHighlightedSource

Function useHighlightedSource

src/ui/diff/useHighlightedSource.ts:29–77  ·  view source on GitHub ↗
({
  file,
  text,
  theme,
  shouldLoadHighlight,
}: {
  file: DiffFile | undefined;
  text: string | undefined;
  theme: AppTheme;
  shouldLoadHighlight?: boolean;
})

Source from the content-addressed store, hash-verified

27
28/** Resolve highlighted full-source content for expanded unchanged rows. */
29export function useHighlightedSource({
30 file,
31 text,
32 theme,
33 shouldLoadHighlight,
34}: {
35 file: DiffFile | undefined;
36 text: string | undefined;
37 theme: AppTheme;
38 shouldLoadHighlight?: boolean;
39}) {
40 const [state, setState] = useState<HighlightedSourceState | null>(null);
41 const cacheKey = useMemo(
42 () => (file && text !== undefined ? buildSourceCacheKey(theme, file, text) : null),
43 [file, text, theme],
44 );
45
46 useLayoutEffect(() => {
47 if (!file || text === undefined || !cacheKey) {
48 setState(null);
49 return;
50 }
51
52 if (state?.cacheKey === cacheKey || !shouldLoadHighlight) {
53 return;
54 }
55
56 let cancelled = false;
57 setState(null);
58
59 loadHighlightedSourceLines({ file, text, theme })
60 .then((highlighted) => {
61 if (!cancelled) {
62 setState({ cacheKey, highlighted });
63 }
64 })
65 .catch(() => {
66 if (!cancelled) {
67 setState({ cacheKey, highlighted: { lines: [] } });
68 }
69 });
70
71 return () => {
72 cancelled = true;
73 };
74 }, [cacheKey, file, shouldLoadHighlight, state?.cacheKey, text]);
75
76 return state?.cacheKey === cacheKey ? state.highlighted : null;
77}

Callers 1

PierreDiffViewFunction · 0.90

Calls 2

buildSourceCacheKeyFunction · 0.85

Tested by

no test coverage detected