import { useMemo } from "react"; import { TrendingUp, TrendingDown } from "lucide-react"; import type { Holding } from "@/types/finance"; import { cn } from "@/lib/utils"; import { usePrivacy } from "@/lib/privacy"; import { formatMoney } from "@/lib/format"; interface Props { holdings: Holding[]; currency: string; } function fmt(v: number, c: string) { return formatMoney(v, c, { decimals: 2 }); } export function TopMovers({ holdings, currency }: Props) { // Local formatter reads privacy mode; subscribe so toggling it re-renders. usePrivacy(); // "Movers" means today's movement. Ranking by `changePercent` (total return // since purchase) just listed the oldest winners every day. const move = (h: Holding) => h.dayChangePercent ?? 0; const { gainers, losers } = useMemo(() => { const sorted = [...holdings].sort((a, b) => move(b) - move(a)); return { gainers: sorted.filter((h) => move(h) > 0).slice(0, 3), losers: sorted.filter((h) => move(h) < 0).slice(-3).reverse(), }; }, [holdings]); const renderList = (items: Holding[], isGainer: boolean) => (
No {isGainer ? "gainers" : "losers"}
)} {items.map((h, i) => ({h.symbol}
{fmt(h.price, h.currency ?? currency)}