import { useMemo, useState } from "react";
import {
LineChart,
Line,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
CartesianGrid,
} from "recharts";
import { CATEGORICAL_8 } from "@/lib/colors";
export default function SaeFeatureDriftChart({
timelines = [],
height = 220,
onPick,
pickedFeatureId,
}) {
const top = useMemo(
() => timelines.slice(0, 12),
[timelines],
);
const data = useMemo(() => {
if (top.length === 0) return [];
const n = top[0].activations.length;
return Array.from({ length: n }, (_, i) => {
const row = { step: `s${i + 1}` };
top.forEach((t) => {
row[`f${t.feature_id}`] = t.activations[i];
});
return row;
});
}, [top]);
const [hoverFid, setHoverFid] = useState(null);
return (
active && payload && payload.length ? (
{label}
{payload
.sort((a, b) => b.value - a.value)
.slice(0, 6)
.map((p) => (
{p.dataKey}: {p.value?.toFixed(2)}
))}
) : null
}
/>
{top.map((t, i) => {
const fid = `f${t.feature_id}`;
const c = CATEGORICAL_8[i % CATEGORICAL_8.length];
const active =
pickedFeatureId === t.feature_id ||
hoverFid === t.feature_id;
return (
);
})}
{/* feature legend */}
{top.map((t, i) => {
const c = CATEGORICAL_8[i % CATEGORICAL_8.length];
const picked = pickedFeatureId === t.feature_id;
return (
);
})}
);
}