feat: all project setup
This commit is contained in:
294
app/(dashboard)/analytics/costs/page.tsx
Normal file
294
app/(dashboard)/analytics/costs/page.tsx
Normal file
@@ -0,0 +1,294 @@
|
||||
"use client";
|
||||
|
||||
// ============================================================================
|
||||
// Analytics Costs Page
|
||||
// ============================================================================
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { DashboardHeader } from "@/components/layout/dashboard-header";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
import { analyticsApi, channelsApi } from "@/lib/api";
|
||||
import { formatCurrency, formatNumber } from "@/lib/utils/format";
|
||||
import { Loader2, AlertCircle, DollarSign } from "lucide-react";
|
||||
import type { CostsReport } from "@/lib/types/api";
|
||||
import type { TargetChannel } from "@/lib/types/api";
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from "recharts";
|
||||
|
||||
export default function AnalyticsCostsPage() {
|
||||
const [report, setReport] = useState<CostsReport | null>(null);
|
||||
const [channels, setChannels] = useState<TargetChannel[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [period, setPeriod] = useState<"day" | "week" | "month">("week");
|
||||
const [targetChannelId, setTargetChannelId] = useState<string | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
}, [period, targetChannelId]);
|
||||
|
||||
const loadData = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const [reportData, channelsData] = await Promise.all([
|
||||
analyticsApi.costs({ period, target_channel_id: targetChannelId }),
|
||||
channels.length > 0
|
||||
? Promise.resolve({ data: channels })
|
||||
: channelsApi.list({}),
|
||||
]);
|
||||
setReport(reportData);
|
||||
if (channels.length === 0) {
|
||||
setChannels(channelsData.data);
|
||||
}
|
||||
} catch (err: any) {
|
||||
setError(err?.error?.message || "Ошибка загрузки аналитики");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<DashboardHeader
|
||||
breadcrumbs={[
|
||||
{ label: "Главная", href: "/" },
|
||||
{ label: "Аналитика", href: "/analytics" },
|
||||
{ label: "Затраты" },
|
||||
]}
|
||||
/>
|
||||
<div className="flex flex-1 flex-col gap-4 p-4 pt-0 mt-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight">
|
||||
Затраты на рекламу
|
||||
</h1>
|
||||
<p className="text-muted-foreground mt-1">
|
||||
Динамика расходов по периодам
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<Alert variant="destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription>{error}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Фильтры</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">
|
||||
Период группировки
|
||||
</label>
|
||||
<Select
|
||||
value={period}
|
||||
onValueChange={(value: any) => setPeriod(value)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="day">По дням</SelectItem>
|
||||
<SelectItem value="week">По неделям</SelectItem>
|
||||
<SelectItem value="month">По месяцам</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">Целевой канал</label>
|
||||
<Select
|
||||
value={targetChannelId || "all"}
|
||||
onValueChange={(value) =>
|
||||
setTargetChannelId(value === "all" ? undefined : value)
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">Все каналы</SelectItem>
|
||||
{channels.map((channel) => (
|
||||
<SelectItem key={channel.id} value={channel.id}>
|
||||
{channel.title}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{loading ? (
|
||||
<Card>
|
||||
<CardContent className="flex items-center justify-center p-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : report ? (
|
||||
<>
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Всего затрат
|
||||
</CardTitle>
|
||||
<DollarSign className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatCurrency(report.totalCost)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
За выбранный период
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Средние затраты
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatCurrency(report.averageCost)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
В среднем за период
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Периодов
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatNumber(report.periods.length)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
С активностью
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>График затрат</CardTitle>
|
||||
<CardDescription>Динамика расходов на рекламу</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ResponsiveContainer width="100%" height={350}>
|
||||
<BarChart data={report.periods}>
|
||||
<CartesianGrid
|
||||
strokeDasharray="3 3"
|
||||
className="stroke-muted"
|
||||
/>
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
className="text-xs"
|
||||
tick={{ fill: "hsl(var(--muted-foreground))" }}
|
||||
/>
|
||||
<YAxis
|
||||
className="text-xs"
|
||||
tick={{ fill: "hsl(var(--muted-foreground))" }}
|
||||
tickFormatter={(value) => `₽${formatNumber(value)}`}
|
||||
/>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: "hsl(var(--background))",
|
||||
border: "1px solid hsl(var(--border))",
|
||||
borderRadius: "8px",
|
||||
}}
|
||||
formatter={(value: any) => [
|
||||
`₽${formatNumber(value)}`,
|
||||
"Затраты",
|
||||
]}
|
||||
/>
|
||||
<Legend />
|
||||
<Bar
|
||||
dataKey="total_cost"
|
||||
name="Затраты"
|
||||
fill="hsl(var(--primary))"
|
||||
radius={[8, 8, 0, 0]}
|
||||
/>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Детализация по периодам</CardTitle>
|
||||
<CardDescription>Затраты и количество закупов</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{report.periods.map((period) => (
|
||||
<div
|
||||
key={period.date}
|
||||
className="flex items-center justify-between p-3 rounded-lg border"
|
||||
>
|
||||
<div>
|
||||
<p className="font-medium">{period.date}</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{formatNumber(period.purchases_count)} закупов
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="text-lg font-bold">
|
||||
{formatCurrency(period.total_cost)}
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
ср. ₽{formatNumber(period.avg_cost)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
348
app/(dashboard)/analytics/page.tsx
Normal file
348
app/(dashboard)/analytics/page.tsx
Normal file
@@ -0,0 +1,348 @@
|
||||
"use client";
|
||||
|
||||
// ============================================================================
|
||||
// Analytics Overview Page
|
||||
// ============================================================================
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { DashboardHeader } from "@/components/layout/dashboard-header";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
import { analyticsApi } from "@/lib/api";
|
||||
import {
|
||||
formatNumber,
|
||||
formatMetric,
|
||||
formatCurrency,
|
||||
formatPercent,
|
||||
} from "@/lib/utils/format";
|
||||
import {
|
||||
BarChart3,
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
TrendingUp,
|
||||
TrendingDown,
|
||||
Users,
|
||||
ShoppingCart,
|
||||
DollarSign,
|
||||
} from "lucide-react";
|
||||
import type { AnalyticsOverview } from "@/lib/types/api";
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from "recharts";
|
||||
|
||||
export default function AnalyticsPage() {
|
||||
const [overview, setOverview] = useState<AnalyticsOverview | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadOverview();
|
||||
}, []);
|
||||
|
||||
const loadOverview = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await analyticsApi.overview();
|
||||
setOverview(data);
|
||||
} catch (err: any) {
|
||||
setError(err?.error?.message || "Ошибка загрузки аналитики");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<>
|
||||
<DashboardHeader
|
||||
breadcrumbs={[
|
||||
{ label: "Главная", href: "/" },
|
||||
{ label: "Аналитика" },
|
||||
]}
|
||||
/>
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !overview) {
|
||||
return (
|
||||
<>
|
||||
<DashboardHeader
|
||||
breadcrumbs={[
|
||||
{ label: "Главная", href: "/" },
|
||||
{ label: "Аналитика" },
|
||||
]}
|
||||
/>
|
||||
<div className="flex flex-1 flex-col gap-4 p-4 pt-0 mt-4">
|
||||
<Alert variant="destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription>
|
||||
{error || "Не удалось загрузить аналитику"}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<DashboardHeader
|
||||
breadcrumbs={[
|
||||
{ label: "Главная", href: "/" },
|
||||
{ label: "Аналитика" },
|
||||
{ label: "Обзор" },
|
||||
]}
|
||||
/>
|
||||
<div className="flex flex-1 flex-col gap-4 p-4 pt-0 mt-4">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight">Аналитика</h1>
|
||||
<p className="text-muted-foreground mt-1">
|
||||
Сводная статистика за последний месяц
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Всего закупов
|
||||
</CardTitle>
|
||||
<ShoppingCart className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatNumber(overview.totalPurchases)}
|
||||
</div>
|
||||
<div className="flex items-center text-xs mt-1">
|
||||
{overview.purchasesChange >= 0 ? (
|
||||
<TrendingUp className="h-3 w-3 text-green-600 mr-1" />
|
||||
) : (
|
||||
<TrendingDown className="h-3 w-3 text-red-600 mr-1" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
overview.purchasesChange >= 0
|
||||
? "text-green-600"
|
||||
: "text-red-600"
|
||||
}
|
||||
>
|
||||
{overview.purchasesChange >= 0 ? "+" : ""}
|
||||
{formatNumber(overview.purchasesChange)}
|
||||
</span>
|
||||
<span className="text-muted-foreground ml-1">
|
||||
за прошлый месяц
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Всего подписчиков
|
||||
</CardTitle>
|
||||
<Users className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{formatNumber(overview.totalFollowers)}
|
||||
</div>
|
||||
<div className="flex items-center text-xs mt-1">
|
||||
{overview.followersChange >= 0 ? (
|
||||
<TrendingUp className="h-3 w-3 text-green-600 mr-1" />
|
||||
) : (
|
||||
<TrendingDown className="h-3 w-3 text-red-600 mr-1" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
overview.followersChange >= 0
|
||||
? "text-green-600"
|
||||
: "text-red-600"
|
||||
}
|
||||
>
|
||||
{overview.followersChange >= 0 ? "+" : ""}
|
||||
{formatNumber(overview.followersChange)}
|
||||
</span>
|
||||
<span className="text-muted-foreground ml-1">
|
||||
за прошлый месяц
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">Средний CPF</CardTitle>
|
||||
<BarChart3 className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
₽{formatMetric(overview.averageCpf)}
|
||||
</div>
|
||||
<div className="flex items-center text-xs mt-1">
|
||||
{overview.cpfChange <= 0 ? (
|
||||
<TrendingDown className="h-3 w-3 text-green-600 mr-1" />
|
||||
) : (
|
||||
<TrendingUp className="h-3 w-3 text-red-600 mr-1" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
overview.cpfChange <= 0 ? "text-green-600" : "text-red-600"
|
||||
}
|
||||
>
|
||||
{formatPercent(overview.cpfChange, true)}
|
||||
</span>
|
||||
<span className="text-muted-foreground ml-1">
|
||||
от прошлого месяца
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">Средний CPM</CardTitle>
|
||||
<DollarSign className="h-4 w-4 text-muted-foreground" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
₽{formatMetric(overview.averageCpm)}
|
||||
</div>
|
||||
<div className="flex items-center text-xs mt-1">
|
||||
{overview.cpmChange <= 0 ? (
|
||||
<TrendingDown className="h-3 w-3 text-green-600 mr-1" />
|
||||
) : (
|
||||
<TrendingUp className="h-3 w-3 text-red-600 mr-1" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
overview.cpmChange <= 0 ? "text-green-600" : "text-red-600"
|
||||
}
|
||||
>
|
||||
{formatPercent(overview.cpmChange, true)}
|
||||
</span>
|
||||
<span className="text-muted-foreground ml-1">
|
||||
от прошлого месяца
|
||||
</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Топ каналов по CPF</CardTitle>
|
||||
<CardDescription>
|
||||
Самые эффективные внешние каналы
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{overview.topChannelsByCpf?.length > 0 ? (
|
||||
overview.topChannelsByCpf.slice(0, 5).map((item, index) => (
|
||||
<div
|
||||
key={item.channel_id}
|
||||
className="flex items-center gap-3"
|
||||
>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="w-8 h-8 rounded-full p-0 flex items-center justify-center"
|
||||
>
|
||||
{index + 1}
|
||||
</Badge>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium truncate">
|
||||
{item.channel_name}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{formatNumber(item.total_purchases)} закупов
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="font-medium">
|
||||
₽{formatMetric(item.avg_cpf)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">CPF</p>
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<p className="text-center text-muted-foreground py-4">
|
||||
Нет данных
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Топ креативов</CardTitle>
|
||||
<CardDescription>
|
||||
Самые эффективные рекламные креативы
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{overview.topCreativesByCpf?.length > 0 ? (
|
||||
overview.topCreativesByCpf.slice(0, 5).map((item, index) => (
|
||||
<div
|
||||
key={item.creative_id}
|
||||
className="flex items-center gap-3"
|
||||
>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="w-8 h-8 rounded-full p-0 flex items-center justify-center"
|
||||
>
|
||||
{index + 1}
|
||||
</Badge>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium truncate">
|
||||
{item.creative_name}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{formatNumber(item.total_subscriptions)} подписчиков
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="font-medium">
|
||||
₽{formatMetric(item.avg_cpf)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">CPF</p>
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<p className="text-center text-muted-foreground py-4">
|
||||
Нет данных
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user