Files
paseo/packages/app/src/git/diff-document/index.tsx
T

57 lines
1.8 KiB
TypeScript

import { useCallback, useMemo, useRef, useState } from "react";
import { withUnistyles } from "react-native-unistyles";
import { RenderProfile } from "@/utils/render-profiler";
import { createDiffPalette, retainDiffPalette } from "./palette";
import { DiffSurface } from "./surface";
import type { DiffDocumentProps, DiffPalette } from "./types";
export type { DiffDocumentProps, WorkingDiffMode } from "./types";
type ThemedDiffDocumentProps = DiffDocumentProps & {
palette: DiffPalette;
};
const EMPTY_PATHS: string[] = [];
function ThemedDiffDocument(props: ThemedDiffDocumentProps) {
const [selectedPath, setSelectedPath] = useState<string | null>(null);
const paletteRef = useRef(props.palette);
paletteRef.current = retainDiffPalette(paletteRef.current, props.palette);
const palette = paletteRef.current;
const collapseState = props.mode.kind === "working" ? props.collapseState : null;
const paths = collapseState?.paths ?? EMPTY_PATHS;
const collapsedFilePaths = useMemo(() => new Set(paths), [paths]);
const toggleFile = useCallback(
(path: string) => {
if (!collapseState) return;
const next = collapsedFilePaths.has(path)
? paths.filter((entry) => entry !== path)
: [...paths, path];
collapseState.onChange(next);
},
[collapseState, collapsedFilePaths, paths],
);
return (
<DiffSurface
{...props}
palette={palette}
collapsedFilePaths={collapsedFilePaths}
onToggleFile={toggleFile}
selectedPath={selectedPath}
onSelectPath={setSelectedPath}
/>
);
}
const StyledDiffDocument = withUnistyles(ThemedDiffDocument, (theme) => ({
palette: createDiffPalette(theme),
}));
export function DiffDocument(props: DiffDocumentProps) {
return (
<RenderProfile id="DiffDocument">
<StyledDiffDocument {...props} />
</RenderProfile>
);
}