import { useEffect, useMemo, useRef, useState } from "react"; import { Empty, Tag, Tooltip } from "antd"; import { GitBranch, GitCommitHorizontal } from "lucide-react"; import { FixedSizeList, type ListChildComponentProps } from "react-window"; import { useTranslation } from "react-i18next"; import type { CheckpointNode } from "@/api/types/checkpoints"; import type { GraphRow } from "./graphLayout"; import styles from "./index.module.less"; const ROW_HEIGHT = 58; const LANE_GAP = 18; const LANE_START = 15; const COLORS = [ "#3178c6", "#2d8a68", "#b26a1b", "#8b5fbf", "#c34f67", "#3b8793", ]; function stableColor(value: string): string { let hash = 0; for (let i = 0; i < value.length; i++) hash = (hash * 31 + value.charCodeAt(i)) | 0; return COLORS[Math.abs(hash) % COLORS.length]; } function useElementSize() { const ref = useRef(null); const [size, setSize] = useState({ width: 0, height: 0 }); useEffect(() => { const element = ref.current; if (!element) return; const update = () => setSize({ width: element.clientWidth, height: element.clientHeight }); update(); const observer = new ResizeObserver(update); observer.observe(element); return () => observer.disconnect(); }, []); return { ref, size }; } interface RowData { rows: GraphRow[]; graphWidth: number; selectedCommit: string | null; onSelect: (node: CheckpointNode) => void; colors: Map; locale: string; labels: Record; } function GraphLines({ row, width, colors, }: { row: GraphRow; width: number; colors: Map; }) { const x = (lane: number) => LANE_START + lane * LANE_GAP; const center = ROW_HEIGHT / 2; const nodeColor = stableColor(row.node.session_key); return ( ); } function VirtualRow({ index, style, data }: ListChildComponentProps) { const { rows, graphWidth, selectedCommit, onSelect, colors, locale, labels } = data; const row = rows[index]; const node = row.node; const title = node.query || node.name || node.subject; const kindLabel = labels[node.kind] ?? node.kind; return ( ); } interface CheckpointGraphProps { rows: GraphRow[]; laneCount: number; selectedCommit: string | null; onSelect: (node: CheckpointNode) => void; emptyDescription: string; } export function CheckpointGraph({ rows, laneCount, selectedCommit, onSelect, emptyDescription, }: CheckpointGraphProps) { const { t, i18n } = useTranslation(); const { ref, size } = useElementSize(); const graphWidth = Math.max( 72, Math.min(224, LANE_START * 2 + laneCount * LANE_GAP), ); const colors = useMemo( () => new Map( rows.map(({ node }) => [node.commit, stableColor(node.session_key)]), ), [rows], ); const labels = useMemo( () => ({ auto: t("checkpoints.kind.auto"), snap: t("checkpoints.kind.snapshot"), "pre-restore": t("checkpoints.kind.safety"), sha: t("checkpoints.kind.commit"), }), [t], ); const data = useMemo( () => ({ rows, graphWidth, selectedCommit, onSelect, colors, locale: i18n.resolvedLanguage || "en", labels, }), [ rows, graphWidth, selectedCommit, onSelect, colors, i18n.resolvedLanguage, labels, ], ); return (
{t("checkpoints.graph")} {t("checkpoints.checkpoint")} {t("checkpoints.type")} {t("checkpoints.commit")} {t("checkpoints.createdAt")}
{!rows.length ? ( ) : size.width > 0 && size.height > 0 ? ( itemData.rows[index].node.ref} overscanCount={8} > {VirtualRow} ) : null}
); }