import type { FC } from 'react';
import { useMemo } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCircleCheck, faTriangleExclamation, faCircleExclamation } from '@fortawesome/pro-duotone-svg-icons';
import { cx } from '@/utils/cx';
import type { Lead } from '@/types/entities';
const CheckCircle: FC<{ className?: string }> = ({ className }) => ;
const AlertTriangle: FC<{ className?: string }> = ({ className }) => ;
const AlertCircle: FC<{ className?: string }> = ({ className }) => ;
interface SlaMetricsProps {
leads: Lead[];
}
const SLA_TARGET_HOURS = 2;
export const SlaMetrics = ({ leads }: SlaMetricsProps) => {
const metrics = useMemo(() => {
const responseTimes: number[] = [];
let withinSla = 0;
let total = leads.length;
for (const lead of leads) {
if (lead.createdAt && lead.firstContactedAt) {
const created = new Date(lead.createdAt).getTime();
const contacted = new Date(lead.firstContactedAt).getTime();
const diffHours = (contacted - created) / (1000 * 60 * 60);
responseTimes.push(diffHours);
if (diffHours <= SLA_TARGET_HOURS) {
withinSla++;
}
}
// Leads without firstContactedAt are counted as outside SLA
}
const avgHours =
responseTimes.length > 0
? responseTimes.reduce((sum, h) => sum + h, 0) / responseTimes.length
: 0;
const slaPercent = total > 0 ? (withinSla / total) * 100 : 0;
return { avgHours, withinSla, total, slaPercent };
}, [leads]);
const getTargetStatus = (): { icon: FC<{ className?: string }>; label: string; colorClass: string } => {
const diff = metrics.avgHours - SLA_TARGET_HOURS;
if (diff <= 0) {
return {
icon: CheckCircle,
label: 'Below target',
colorClass: 'text-success-primary',
};
}
if (diff <= 0.5) {
return {
icon: AlertTriangle,
label: 'Near target',
colorClass: 'text-warning-primary',
};
}
return {
icon: AlertCircle,
label: 'Above target',
colorClass: 'text-error-primary',
};
};
const status = getTargetStatus();
const StatusIcon = status.icon;
return (
Response SLA
{metrics.avgHours.toFixed(1)}h
Target: {SLA_TARGET_HOURS}h
{status.label}
SLA Compliance
{Math.round(metrics.slaPercent)}%
= 80
? 'bg-success-500'
: metrics.slaPercent >= 60
? 'bg-warning-500'
: 'bg-error-500',
)}
style={{ width: `${metrics.slaPercent}%` }}
/>
{metrics.withinSla} of {metrics.total} leads within SLA ({Math.round(metrics.slaPercent)}%)
);
};