Update PredictionsChart.tsx
All checks were successful
Build Frontend / build (push) Successful in 1m5s
All checks were successful
Build Frontend / build (push) Successful in 1m5s
This commit is contained in:
@@ -65,6 +65,7 @@ import {
|
|||||||
TooltipTrigger,
|
TooltipTrigger,
|
||||||
} from "@/components/ui/tooltip";
|
} from "@/components/ui/tooltip";
|
||||||
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
||||||
|
import { Slider } from "@/components/ui/slider";
|
||||||
|
|
||||||
interface PredictionsChartProps {
|
interface PredictionsChartProps {
|
||||||
timeRange?: number;
|
timeRange?: number;
|
||||||
@@ -81,6 +82,7 @@ export default function PredictionsChart({
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [daysAhead, setDaysAhead] = useState(7);
|
const [daysAhead, setDaysAhead] = useState(7);
|
||||||
const [activeTab, setActiveTab] = useState<"overview" | "stock">("overview");
|
const [activeTab, setActiveTab] = useState<"overview" | "stock">("overview");
|
||||||
|
const [simulationFactor, setSimulationFactor] = useState(0);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
const fetchPredictions = async () => {
|
const fetchPredictions = async () => {
|
||||||
@@ -138,6 +140,16 @@ export default function PredictionsChart({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const simulatedData = useMemo(() => {
|
||||||
|
if (!predictions?.sales?.dailyPredictions) return [];
|
||||||
|
return predictions.sales.dailyPredictions.map((d) => ({
|
||||||
|
...d,
|
||||||
|
formattedDate: format(new Date(d.date), "MMM d"),
|
||||||
|
value: d.predicted * (1 + simulationFactor / 100),
|
||||||
|
originalValue: d.predicted,
|
||||||
|
}));
|
||||||
|
}, [predictions, simulationFactor]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
@@ -433,20 +445,46 @@ export default function PredictionsChart({
|
|||||||
{predictions.sales.dailyPredictions &&
|
{predictions.sales.dailyPredictions &&
|
||||||
predictions.sales.dailyPredictions.length > 0 && (
|
predictions.sales.dailyPredictions.length > 0 && (
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader className="pb-3">
|
<CardHeader className="pb-3 flex flex-row items-center justify-between space-y-0">
|
||||||
<CardTitle className="text-sm font-medium">
|
<CardTitle className="text-sm font-medium">
|
||||||
Daily Revenue Forecast
|
Daily Revenue Forecast
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<div className="flex flex-col items-end">
|
||||||
|
<span className="text-xs font-medium text-muted-foreground">
|
||||||
|
Simulate Traffic:{" "}
|
||||||
|
<span className={simulationFactor > 0 ? "text-green-600" : simulationFactor < 0 ? "text-red-600" : ""}>
|
||||||
|
{simulationFactor > 0 ? "+" : ""}
|
||||||
|
{simulationFactor}%
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<Slider
|
||||||
|
value={[simulationFactor]}
|
||||||
|
min={-50}
|
||||||
|
max={50}
|
||||||
|
step={5}
|
||||||
|
onValueChange={(val) => setSimulationFactor(val[0])}
|
||||||
|
className="w-[150px] mt-1.5"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{simulationFactor !== 0 && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="h-6 w-6"
|
||||||
|
onClick={() => setSimulationFactor(0)}
|
||||||
|
title="Reset simulation"
|
||||||
|
>
|
||||||
|
<RefreshCw className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<div className="h-[300px] w-full mt-4">
|
<div className="h-[300px] w-full mt-4">
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer width="100%" height="100%">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
data={predictions.sales.dailyPredictions.map(d => ({
|
data={simulatedData}
|
||||||
...d,
|
|
||||||
formattedDate: format(new Date(d.date), "MMM d"),
|
|
||||||
value: d.predicted
|
|
||||||
}))}
|
|
||||||
margin={{
|
margin={{
|
||||||
top: 5,
|
top: 5,
|
||||||
right: 10,
|
right: 10,
|
||||||
@@ -456,8 +494,16 @@ export default function PredictionsChart({
|
|||||||
>
|
>
|
||||||
<defs>
|
<defs>
|
||||||
<linearGradient id="colorValue" x1="0" y1="0" x2="0" y2="1">
|
<linearGradient id="colorValue" x1="0" y1="0" x2="0" y2="1">
|
||||||
<stop offset="5%" stopColor="#8884d8" stopOpacity={0.8} />
|
<stop
|
||||||
<stop offset="95%" stopColor="#8884d8" stopOpacity={0} />
|
offset="5%"
|
||||||
|
stopColor={simulationFactor !== 0 ? "#10b981" : "#8884d8"}
|
||||||
|
stopOpacity={0.8}
|
||||||
|
/>
|
||||||
|
<stop
|
||||||
|
offset="95%"
|
||||||
|
stopColor={simulationFactor !== 0 ? "#10b981" : "#8884d8"}
|
||||||
|
stopOpacity={0}
|
||||||
|
/>
|
||||||
</linearGradient>
|
</linearGradient>
|
||||||
</defs>
|
</defs>
|
||||||
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="hsl(var(--border))" />
|
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="hsl(var(--border))" />
|
||||||
@@ -486,9 +532,10 @@ export default function PredictionsChart({
|
|||||||
<Area
|
<Area
|
||||||
type="monotone"
|
type="monotone"
|
||||||
dataKey="value"
|
dataKey="value"
|
||||||
stroke="#8884d8"
|
stroke={simulationFactor !== 0 ? "#10b981" : "#8884d8"}
|
||||||
fillOpacity={1}
|
fillOpacity={1}
|
||||||
fill="url(#colorValue)"
|
fill="url(#colorValue)"
|
||||||
|
strokeDasharray={simulationFactor !== 0 ? "5 5" : "0"}
|
||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ResponsiveContainer>
|
</ResponsiveContainer>
|
||||||
|
|||||||
Reference in New Issue
Block a user