Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 16x 16x 16x 16x 16x 16x 16x 15x 5x 4x 16x 15x 8x 16x 16x 16x 16x 8x 8x 8x 8x 8x 8x 8x 4x 16x 64x 30x 8x | import { useMemo } from "react";
import { Link } from "@tanstack/react-router";
import {
FolderKanban,
Users,
AlertCircle,
Target,
IndianRupee,
Trophy,
Home,
} from "lucide-react";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "../components/ui/card";
import { EmptyState } from "../components/ui/empty-state";
import { ProfileCompletenessCard } from "../components/dashboard/ProfileCompletenessCard";
import { ThemeSwitcher } from "../components/ui/theme-switcher";
import { StickyPageHeader } from "../components/ui/sticky-page-header";
import { PushPermissionBanner } from "../components/pwa/PushPermissionBanner";
import { IosBanner } from "../components/pwa/IosBanner";
import { useAuth } from "../lib/auth-context";
import { usePro } from "../lib/pro-context";
import { useProjects } from "../hooks/queries/useProjectQueries";
import {
useKanbanData,
useCrmAnalytics,
} from "../hooks/queries/useCrmQueries";
import { abbreviatePaise } from "../lib/currency-utils";
export function DashboardPage() {
const { user } = useAuth();
const { proId, pro } = usePro();
const { data: projects = [], isLoading: projectsLoading } =
useProjects(proId);
const { data: kanbanData, isLoading: kanbanLoading } =
useKanbanData(proId);
const { data: crmDashboard, isLoading: crmLoading } =
useCrmAnalytics(proId, "this_month");
const statsLoaderActive = projectsLoading || crmLoading;
// Recent leads: sorted by dateCreated descending, top 5
const recentLeads = useMemo(() => {
if (!kanbanData?.leads) return [];
return [...kanbanData.leads]
.sort(
(a, b) =>
new Date(b.dateCreated).getTime() - new Date(a.dateCreated).getTime(),
)
.slice(0, 5);
}, [kanbanData]);
// Build a stage map for showing stage names on recent leads
const stageMap = useMemo(() => {
if (!kanbanData?.stages) return new Map<number, string>();
return new Map(kanbanData.stages.map((s) => [s.id, s.name]));
}, [kanbanData]);
const pipeline = crmDashboard?.pipeline;
const publishedProjects =
projects?.filter((p) => p.status === "published").length ?? 0;
const statCards = [
{
name: "Total Projects",
value: projects?.length ?? "-",
subtext: `${publishedProjects} published`,
icon: FolderKanban,
href: "/projects",
},
{
name: "Active Leads",
value: pipeline?.totalActive ?? "-",
subtext: `${pipeline?.newLeadsInPeriod ?? 0} new this month`,
icon: Target,
href: "/crm",
},
{
name: "Pipeline Value",
value: pipeline
? `₹${abbreviatePaise(pipeline.totalPipelineValue)}`
: "-",
subtext: `Avg ₹${pipeline ? abbreviatePaise(pipeline.avgDealSize) : "0"}`,
icon: IndianRupee,
href: "/crm/analytics",
},
{
name: "Won This Month",
value: pipeline?.wonInPeriod ?? "-",
subtext: pipeline
? `₹${abbreviatePaise(pipeline.wonValueInPeriod)}`
: "₹0",
icon: Trophy,
href: "/crm/analytics",
},
];
// Format relative time
const formatRelativeTime = (dateStr: string) => {
const date = new Date(dateStr);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
if (diffMins < 60) return `${diffMins} min ago`;
Eif (diffHours < 24) return `${diffHours} hours ago`;
if (diffDays === 1) return "1 day ago";
return `${diffDays} days ago`;
};
return (
<div className="space-y-8">
<StickyPageHeader>
<h1 className="text-2xl font-bold text-foreground-default flex items-center gap-2">
<Home className="h-6 w-6" />
Welcome back, {user?.name || "there"}!
</h1>
</StickyPageHeader>
{/* Notification banners — only on Dashboard */}
<PushPermissionBanner />
<IosBanner />
{/* Draft Status Banner */}
{pro?.status === "draft" && (
<div className="flex items-start gap-3 rounded-lg border border-amber-200 bg-amber-50 p-4">
<AlertCircle className="h-5 w-5 text-amber-600 mt-0.5 flex-shrink-0" />
<div>
<h3 className="font-medium text-amber-800">
Your profile is pending review
</h3>
<p className="mt-1 text-sm text-amber-700">
Your profile and projects will appear in the marketplace once
your account is published. Contact admin for more details.
</p>
</div>
</div>
)}
{/* Profile Completeness Card */}
{pro && (
<ProfileCompletenessCard
pro={pro}
projectCount={projects?.length || 0}
/>
)}
{/* Stats Grid */}
<div className="grid grid-cols-2 gap-3 sm:gap-6 lg:grid-cols-4">
{statCards.map((stat) => (
<Link key={stat.name} to={stat.href} className="block">
<Card className="transition-all duration-200 hover:shadow-card-hover hover:border-primary-200 hover:-translate-y-0.5 cursor-pointer">
<CardHeader className="flex flex-row items-center justify-between pb-2 p-3 sm:p-5">
<CardTitle className="text-xs sm:text-sm font-medium text-foreground-muted">
{stat.name}
</CardTitle>
<stat.icon className="h-4 w-4 sm:h-5 sm:w-5 text-foreground-subtle" />
</CardHeader>
<CardContent className="p-3 pt-0 sm:p-5 sm:pt-0">
<div className="text-xl sm:text-2xl font-bold">
{statsLoaderActive ? (
<div className="h-6 sm:h-8 w-14 sm:w-16 bg-background-muted rounded animate-pulse" />
) : (
stat.value
)}
</div>
<p className="text-xs text-foreground-muted">
{stat.subtext}
</p>
</CardContent>
</Card>
</Link>
))}
</div>
{/* Recent Activity */}
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
<Card>
<CardHeader>
<CardTitle>Recent Leads</CardTitle>
</CardHeader>
<CardContent>
{kanbanLoading ? (
<div className="space-y-4">
{[1, 2, 3].map((i) => (
<div
key={i}
className="flex items-center justify-between border-b pb-4 last:border-0"
>
<div className="space-y-2">
<div className="h-4 w-32 bg-background-muted rounded animate-pulse" />
<div className="h-3 w-24 bg-background-muted rounded animate-pulse" />
</div>
<div className="h-3 w-16 bg-background-muted rounded animate-pulse" />
</div>
))}
</div>
) : recentLeads.length === 0 ? (
<EmptyState
icon={Target}
title="No leads yet"
description="New leads from inquiries and manual entries will appear here."
className="py-8"
/>
) : (
<div className="space-y-4">
{recentLeads.map((lead) => (
<Link
key={lead.id}
to={`/crm/leads/${lead.id}`}
className="flex items-center justify-between border-b pb-4 last:border-0 hover:bg-background-subtle rounded -mx-2 px-2 transition-colors"
>
<div className="min-w-0 flex-1">
<p className="font-medium text-foreground-default truncate">
{lead.customerName}
</p>
<p className="text-sm text-foreground-muted truncate">
{lead.requirement ||
lead.projectType ||
"No details"}
</p>
</div>
<div className="text-right flex-shrink-0 ml-3">
<span className="inline-block px-2 py-1 text-xs font-medium rounded-full bg-primary-100 text-primary-800">
{stageMap.get(lead.currentStageId) || "Unknown"}
</span>
<p className="text-xs text-foreground-subtle mt-1">
{formatRelativeTime(lead.dateCreated)}
</p>
</div>
</Link>
))}
<Link
to="/crm"
className="block text-center text-sm text-primary-600 hover:text-primary-700 pt-2"
>
View CRM pipeline →
</Link>
</div>
)}
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Quick Actions</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-2 gap-4">
<Link
to="/projects"
className="flex flex-col items-center justify-center rounded-lg border border-border-default p-4 hover:bg-background-muted hover:shadow-card-hover transition-all duration-200"
>
<FolderKanban className="h-8 w-8 text-primary-600 mb-2" />
<span className="text-sm font-medium">Manage Projects</span>
</Link>
<Link
to="/crm"
className="flex flex-col items-center justify-center rounded-lg border border-border-default p-4 hover:bg-background-muted hover:shadow-card-hover transition-all duration-200"
>
<Target className="h-8 w-8 text-green-600 mb-2" />
<span className="text-sm font-medium">CRM Pipeline</span>
</Link>
<Link
to="/profile"
className="flex flex-col items-center justify-center rounded-lg border border-border-default p-4 hover:bg-background-muted hover:shadow-card-hover transition-all duration-200"
>
<Users className="h-8 w-8 text-secondary-600 mb-2" />
<span className="text-sm font-medium">Edit Profile</span>
</Link>
<ThemeSwitcher variant="card" />
</div>
</CardContent>
</Card>
</div>
</div>
);
}
|