55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { DashboardStats } from "@/types"
|
|
import { leads } from "./leads"
|
|
import { filterLeadsByPeriod, groupLeadsByInterval } from "@/lib/date-utils"
|
|
|
|
const periodLabels: Record<string, string> = {
|
|
"7days": "Last 7 days",
|
|
"30days": "Last 30 days",
|
|
"6months": "Last 6 months",
|
|
"12months": "Last 12 months",
|
|
}
|
|
|
|
export function getDashboardStats(period: string): DashboardStats {
|
|
const filtered = filterLeadsByPeriod(leads, period)
|
|
const label = periodLabels[period] ?? "Selected period"
|
|
|
|
const totalLeads = filtered.length
|
|
const openLeads = filtered.filter((l) => l.status === "open").length
|
|
const contactedLeads = filtered.filter((l) => l.status === "contacted").length
|
|
const pendingLeads = filtered.filter((l) => l.status === "pending").length
|
|
const closedLeads = filtered.filter((l) => l.status === "closed").length
|
|
const ignoredLeads = filtered.filter((l) => l.status === "ignored").length
|
|
const conversionRate = totalLeads > 0
|
|
? Math.round((closedLeads / totalLeads) * 100)
|
|
: 0
|
|
|
|
function getStatusDistribution() {
|
|
const statusCounts: Record<string, number> = { open: 0, contacted: 0, pending: 0, closed: 0, ignored: 0 }
|
|
filtered.forEach((l) => {
|
|
statusCounts[l.status]++
|
|
})
|
|
|
|
return [
|
|
{ name: "Open", value: statusCounts.open, color: "#3b82f6" },
|
|
{ name: "Contacted", value: statusCounts.contacted, color: "#f59e0b" },
|
|
{ name: "Pending", value: statusCounts.pending, color: "#8b5cf6" },
|
|
{ name: "Closed", value: statusCounts.closed, color: "#10b981" },
|
|
{ name: "Ignored", value: statusCounts.ignored, color: "#6B7280" },
|
|
]
|
|
}
|
|
|
|
return {
|
|
totalLeads,
|
|
openLeads,
|
|
contactedLeads,
|
|
pendingLeads,
|
|
closedLeads,
|
|
ignoredLeads,
|
|
conversionRate,
|
|
leadsPerMonth: groupLeadsByInterval(filtered, period),
|
|
recentLeads: filtered.slice(0, 10),
|
|
statusDistribution: getStatusDistribution(),
|
|
periodLabel: label,
|
|
}
|
|
}
|