"use client"; import { useEffect, useRef, useState } from "react"; import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import { useAuthStore } from "@/features/auth/stores/auth.store"; import { useDashboardSummary, useDashboardTasks } from "@/features/dashboard/hooks"; import { TaskList, QuickStats, ActivityFeed } from "@/features/dashboard/components"; import { ErrorState } from "@/components/atoms/error-state"; import { PageLayout } from "@/components/templates"; import { cn } from "@/shared/utils"; import { InlineToast } from "@/components/atoms/inline-toast"; import { useInternetEligibility } from "@/features/services/hooks"; export function DashboardView() { const { user, isAuthenticated, loading: authLoading, clearLoading } = useAuthStore(); const hideToastTimeout = useRef(null); const [eligibilityToast, setEligibilityToast] = useState<{ visible: boolean; text: string; tone: "info" | "success" | "warning" | "error"; }>({ visible: false, text: "", tone: "info" }); // Clear auth loading state when dashboard loads (after successful login) useEffect(() => { clearLoading(); }, [clearLoading]); const { data: summary, isLoading: summaryLoading, error } = useDashboardSummary(); const { tasks, isLoading: tasksLoading, taskCount } = useDashboardTasks(); const { data: eligibility } = useInternetEligibility({ enabled: isAuthenticated }); // Combined loading state - AppShell handles unauthenticated redirect const isLoading = authLoading || summaryLoading; useEffect(() => { if (!isAuthenticated || !user?.id) return; const eligibilityData = eligibility as | { status?: string; eligibility?: string } | null | undefined; const status = eligibilityData?.status; if (!status) return; // query not ready yet const key = `cp:internet-eligibility:last:${user.id}`; const last = localStorage.getItem(key); if (status === "pending") { localStorage.setItem(key, "PENDING"); return; } const eligibilityValue = eligibilityData?.eligibility; if (status === "eligible" && typeof eligibilityValue === "string") { const current = eligibilityValue.trim(); if (last === "PENDING") { setEligibilityToast({ visible: true, text: "We've finished reviewing your address — you can now choose personalized internet plans.", tone: "success", }); if (hideToastTimeout.current) window.clearTimeout(hideToastTimeout.current); hideToastTimeout.current = window.setTimeout(() => { setEligibilityToast(t => ({ ...t, visible: false })); hideToastTimeout.current = null; }, 3500); } localStorage.setItem(key, current); } }, [eligibility, isAuthenticated, user?.id]); useEffect(() => { return () => { if (hideToastTimeout.current) window.clearTimeout(hideToastTimeout.current); }; }, []); if (isLoading) { return (
{/* Greeting skeleton */}
{/* Tasks skeleton */}
{/* Bottom section skeleton */}
); } // Handle error state if (error) { const errorMessage = (() => { if (typeof error === "string") return error; if (error instanceof Error) return error.message; if (typeof error === "object" && "message" in error) { return String((error as { message?: unknown }).message); } return "An unexpected error occurred"; })(); return ( ); } // Get user's display name const displayName = user?.firstname || user?.email?.split("@")[0] || "there"; // Determine urgency level for task badge const hasUrgentTask = tasks.some(t => t.tone === "critical"); return ( {/* Greeting Section */}

Welcome back

{displayName}

{/* Task status badge */} {taskCount > 0 ? (
{hasUrgentTask && } {taskCount === 1 ? "1 task needs attention" : `${taskCount} tasks need attention`}
) : (

Everything is up to date

)}
{/* Tasks Section - Main focus area */}

Your Tasks

{/* Bottom Section: Quick Stats + Recent Activity */}
); }