"use client"; // ============================================================================ // Dashboard Home Page // ============================================================================ import { useEffect, useState } from "react"; import { Loader2, ShoppingCart, Users, TrendingUp, BarChart3 } from "lucide-react"; import { DashboardHeader } from "@/components/layout/dashboard-header"; import { useWorkspace } from "@/components/providers/workspace-provider"; import { demoAwareAnalyticsApi } from "@/lib/demo"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import type { SpendingAnalytics } from "@/lib/types/api"; // ============================================================================ // Helpers // ============================================================================ const formatNumber = (value: number | null | undefined, showSign = false): string => { if (value === null || value === undefined) return "—"; const formatted = new Intl.NumberFormat("ru-RU").format(value); if (showSign && value > 0) return `+${formatted}`; return formatted; }; const formatCurrency = (value: number | null | undefined): string => { if (value === null || value === undefined) return "—"; return new Intl.NumberFormat("ru-RU", { style: "currency", currency: "RUB", minimumFractionDigits: 0, maximumFractionDigits: 0, }).format(value); }; // ============================================================================ // Component // ============================================================================ export default function DashboardPage() { const { currentWorkspace, currentProject, isLoading: workspaceLoading, projects } = useWorkspace(); const [spending, setSpending] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!currentWorkspace) { setLoading(false); return; } const loadData = async () => { try { setLoading(true); const data = await demoAwareAnalyticsApi.spending(currentWorkspace.id, { project_id: currentProject?.id, }); setSpending(data); } catch (err) { console.error("Failed to load spending:", err); } finally { setLoading(false); } }; loadData(); }, [currentWorkspace?.id, currentProject?.id]); if (workspaceLoading) { return ( <>
); } if (!currentWorkspace) { return ( <>

Нет доступных воркспейсов

Создайте новый воркспейс для начала работы

); } // Calculate totals and averages const totalCost = spending?.total_cost ?? 0; const totalSubscriptions = spending?.total_subscriptions ?? 0; const totalViews = spending?.total_views ?? 0; const avgCps = totalSubscriptions > 0 ? totalCost / totalSubscriptions : null; const avgCpm = totalViews > 0 ? (totalCost / totalViews) * 1000 : null; return ( <>
{/* Stats Cards */}
Всего потрачено {loading ? ( ) : (
{formatCurrency(totalCost)}
)}
Подписчиков {loading ? ( ) : (
{formatNumber(totalSubscriptions)}
)}
Средний CPS {loading ? ( ) : (
{formatCurrency(avgCps)}
)}
Средний CPM {loading ? ( ) : (
{formatCurrency(avgCpm)}
)}
{/* Quick Actions */}
Быстрый старт Начните с основных действий → Создать размещение → Создать креатив → Посмотреть аналитику Проекты {projects.length > 0 ? `${projects.length} активных проектов` : "Нет активных проектов"} {projects.length > 0 ? (
{projects.slice(0, 5).map((project) => (
{project.title} {project.username && ( @{project.username} )}
))}
) : (

Добавьте бота в канал через Telegram для создания проекта

)}
); }