229 lines
9.1 KiB
TypeScript
229 lines
9.1 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useParams, Link, useOutletContext } from 'react-router-dom';
|
|
import { ArrowLeft, Package, DollarSign } from 'lucide-react';
|
|
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
|
|
import DateRangePicker from '../components/DateRangePicker';
|
|
import RefreshStatus from '../components/RefreshStatus';
|
|
import type { DateRange, ProductDetailsAnalytics } from '../types';
|
|
import { fetchProductDetailsAnalytics } from '../dataService';
|
|
|
|
const CHART_GRID_COLOR = 'var(--chart-grid)';
|
|
const CHART_AXIS_COLOR = 'var(--chart-axis)';
|
|
const CHART_CURSOR_COLOR = 'var(--chart-cursor)';
|
|
const CHART_DETAIL_BAR_COLOR = 'var(--chart-detail-bar)';
|
|
|
|
const formatDateKey = (date: Date) => {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
return `${year}-${month}-${day}`;
|
|
};
|
|
|
|
type CustomTooltipProps = {
|
|
active?: boolean;
|
|
payload?: Array<{ value: number }>;
|
|
label?: string;
|
|
};
|
|
|
|
const CustomTooltip = ({ active, payload, label }: CustomTooltipProps) => {
|
|
if (active && payload && payload.length) {
|
|
return (
|
|
<div
|
|
className="rounded-xl border p-3 shadow-lg"
|
|
style={{ backgroundColor: 'var(--chart-tooltip-bg)', borderColor: 'var(--chart-tooltip-border)' }}
|
|
>
|
|
<p className="font-bold mb-1" style={{ color: CHART_DETAIL_BAR_COLOR }}>{label}</p>
|
|
<p className="m-0" style={{ color: 'var(--chart-tooltip-text)' }}>Vendas: {payload[0].value}</p>
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const ProductDetailsSkeleton = () => (
|
|
<div className="space-y-6" aria-label="Carregando produto">
|
|
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
|
|
<div className="flex flex-col gap-4">
|
|
<div className="skeleton h-4 w-20" />
|
|
<div className="flex items-center gap-4">
|
|
<div className="skeleton h-16 w-16 rounded-2xl" />
|
|
<div>
|
|
<div className="skeleton h-3 w-24" />
|
|
<div className="skeleton mt-3 h-7 w-80 max-w-[70vw]" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="skeleton h-10 w-64" />
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
{[0, 1].map(item => (
|
|
<div key={`product-details-kpi-skeleton-${item}`} className="bg-dark-card p-6 rounded-2xl border border-dark-border shadow-sm">
|
|
<div className="flex justify-between gap-6">
|
|
<div className="w-full">
|
|
<div className="skeleton h-3 w-36" />
|
|
<div className="skeleton mt-3 h-8 w-32" />
|
|
</div>
|
|
<div className="skeleton h-12 w-12 shrink-0 rounded-xl" />
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<div className="bg-white dark:bg-dark-card border border-zinc-200 dark:border-dark-border rounded-2xl p-6 shadow-sm">
|
|
<div className="skeleton h-5 w-56" />
|
|
<div className="mt-8 skeleton h-[400px] w-full" />
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
const ProductDetails = () => {
|
|
const { id } = useParams<{ id: string }>();
|
|
const { dateRange, setDateRange } = useOutletContext<{
|
|
dateRange: DateRange,
|
|
setDateRange: (range: DateRange) => void
|
|
}>();
|
|
const [details, setDetails] = useState<ProductDetailsAnalytics | null>(null);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
let isMounted = true;
|
|
|
|
const loadProductDetails = async () => {
|
|
if (!id) {
|
|
if (isMounted) {
|
|
setDetails(null);
|
|
setIsLoading(false);
|
|
}
|
|
return;
|
|
}
|
|
|
|
setIsLoading(true);
|
|
const productDetails = await fetchProductDetailsAnalytics(id, dateRange);
|
|
|
|
if (isMounted) {
|
|
setDetails(productDetails);
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
void loadProductDetails();
|
|
|
|
return () => {
|
|
isMounted = false;
|
|
};
|
|
}, [dateRange, id]);
|
|
|
|
const formatCurrency = (value: number) => {
|
|
return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(value);
|
|
};
|
|
|
|
if (isLoading && !details) {
|
|
return <ProductDetailsSkeleton />;
|
|
}
|
|
|
|
if (!details?.productInfo) {
|
|
return (
|
|
<div className="text-center py-12">
|
|
<p className="text-zinc-500 dark:text-dark-muted font-medium">Produto não encontrado.</p>
|
|
<Link to="/products" className="text-brand-primary hover:underline mt-4 inline-block font-bold">Voltar para produtos</Link>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const { productInfo, chartData, totalSold, totalRevenue } = details;
|
|
const isRefreshing = isLoading && Boolean(details);
|
|
const isSingleDayRange = formatDateKey(dateRange.start) === formatDateKey(dateRange.end);
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
|
|
<div className="flex flex-col gap-4">
|
|
<Link to="/products" className="inline-flex items-center text-sm font-bold text-zinc-400 dark:text-dark-muted hover:text-zinc-900 dark:hover:text-dark-text transition-colors w-fit">
|
|
<ArrowLeft className="w-4 h-4 mr-2" />
|
|
Voltar
|
|
</Link>
|
|
|
|
<div className="flex items-center gap-4">
|
|
<div className="w-16 h-16 rounded-2xl bg-white dark:bg-dark-card border border-zinc-200 dark:border-dark-border flex items-center justify-center shadow-sm text-brand-primary">
|
|
<Package className="w-8 h-8" />
|
|
</div>
|
|
<div>
|
|
<div className="text-[10px] font-bold text-zinc-400 dark:text-dark-muted uppercase tracking-widest">ID: #{productInfo.id}</div>
|
|
<h1 className="text-2xl font-bold text-zinc-900 dark:text-dark-text">{productInfo.name}</h1>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<DateRangePicker
|
|
dateRange={dateRange}
|
|
onChange={setDateRange}
|
|
/> </div>
|
|
|
|
<RefreshStatus isRefreshing={isRefreshing} />
|
|
|
|
<div className={isRefreshing ? 'refreshing-content space-y-6' : 'space-y-6'} aria-busy={isRefreshing}>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div className="bg-dark-card p-6 rounded-2xl border border-dark-border flex items-center justify-between shadow-sm">
|
|
<div>
|
|
<p className="text-xs font-bold text-dark-muted uppercase tracking-widest mb-1">Unidades Vendidas</p>
|
|
<p className="text-3xl font-bold text-dark-text">{totalSold}</p>
|
|
</div>
|
|
<div className="p-3 bg-brand-primary/10 rounded-xl text-brand-primary">
|
|
<Package size={24} />
|
|
</div>
|
|
</div>
|
|
<div className="bg-dark-card p-6 rounded-2xl border border-dark-border flex items-center justify-between shadow-sm">
|
|
<div>
|
|
<p className="text-xs font-bold text-dark-muted uppercase tracking-widest mb-1">Receita Total</p>
|
|
<p className="text-3xl font-bold text-brand-primary">{formatCurrency(totalRevenue)}</p>
|
|
</div>
|
|
<div className="p-3 bg-emerald-500/10 rounded-xl text-emerald-500">
|
|
<DollarSign size={24} />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="bg-white dark:bg-dark-card border border-zinc-200 dark:border-dark-border rounded-2xl p-6 shadow-sm">
|
|
<h3 className="text-lg font-bold mb-8 text-zinc-900 dark:text-dark-text">
|
|
Volume de Vendas por {isSingleDayRange ? 'Horário' : 'Data'}
|
|
</h3>
|
|
<div className="h-[400px] w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<AreaChart data={chartData} margin={{ top: 5, right: 30, left: 20, bottom: isSingleDayRange ? 24 : 80 }}>
|
|
<defs>
|
|
<linearGradient id="productVolumeGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_DETAIL_BAR_COLOR} stopOpacity={0.38} />
|
|
<stop offset="95%" stopColor={CHART_DETAIL_BAR_COLOR} stopOpacity={0.04} />
|
|
</linearGradient>
|
|
</defs>
|
|
<CartesianGrid strokeDasharray="3 3" stroke={CHART_GRID_COLOR} vertical={false} />
|
|
<XAxis
|
|
dataKey="date" stroke={CHART_AXIS_COLOR} fontSize={10} tickLine={false} axisLine={false}
|
|
interval={isSingleDayRange ? 2 : 0}
|
|
angle={isSingleDayRange ? 0 : -45}
|
|
textAnchor={isSingleDayRange ? 'middle' : 'end'}
|
|
height={isSingleDayRange ? 24 : 80}
|
|
/>
|
|
<YAxis stroke={CHART_AXIS_COLOR} fontSize={12} tickLine={false} axisLine={false} />
|
|
<Tooltip content={<CustomTooltip />} cursor={{ fill: CHART_CURSOR_COLOR }} />
|
|
<Area
|
|
type="monotone"
|
|
dataKey="value"
|
|
stroke={CHART_DETAIL_BAR_COLOR}
|
|
strokeWidth={2.25}
|
|
fill="url(#productVolumeGradient)"
|
|
dot={{ r: 3, strokeWidth: 2, fill: 'var(--color-dark-card)', stroke: CHART_DETAIL_BAR_COLOR }}
|
|
activeDot={{ r: 5, strokeWidth: 2, fill: CHART_DETAIL_BAR_COLOR, stroke: 'var(--color-dark-card)' }}
|
|
/>
|
|
</AreaChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ProductDetails;
|