'use client' import { useMemo } from 'react' import { Card, CardContent } from '@/components/ui/card' import { TrendingUp, TrendingDown, Minus } from 'lucide-react' import { AreaChart, Area, XAxis, YAxis, Tooltip, ResponsiveContainer, } from 'recharts' interface MonthlyData { month: string income: number expenses: number } interface IncomeChartProps { monthlyData: MonthlyData[] } export function IncomeChart({ monthlyData }: IncomeChartProps) { const { trendPercent, trendDirection } = useMemo(() => { if (monthlyData.length < 2) return { trendPercent: 0, trendDirection: 'flat' as const } const current = monthlyData[monthlyData.length - 1] const previous = monthlyData[monthlyData.length - 2] if (previous.income === 0) return { trendPercent: 0, trendDirection: 'flat' as const } const percent = Math.round(((current.income - previous.income) / previous.income) * 100) return { trendPercent: Math.abs(percent), trendDirection: percent > 0 ? 'up' as const : percent < 0 ? 'down' as const : 'flat' as const, } }, [monthlyData]) if (monthlyData.length === 0) return null const formatAmount = (value: number) => { if (value >= 1000000) return `${(value / 1000000).toFixed(1)}M` if (value >= 1000) return `${Math.round(value / 1000)}k` return String(value) } return (
Intäkter vs kostnader {trendDirection !== 'flat' && (
{trendDirection === 'up' ? ( ) : ( )} {trendPercent}% vs förra månaden
)} {trendDirection === 'flat' && (
Oförändrat
)}
{ if (value === undefined) return ['-', name || ''] return [ new Intl.NumberFormat('sv-SE', { style: 'currency', currency: 'SEK', maximumFractionDigits: 0 }).format(value), name === 'income' ? 'Intäkter' : 'Kostnader', ] }} />
Intäkter
Kostnader
) }