import { useState } from 'react';
import { Link } from 'react-router';
import { clsx } from 'clsx';
import { toast } from 'sonner';
import { trpc } from '../lib/trpc';
import { voice } from '../lib/voice';
import { Card } from '../components/ui/Card';
import { Button } from '../components/ui/Button';
import { SkeletonCard } from '../components/ui/SkeletonCard';
import {
LEAD_STATUS_META,
LEAD_STATUS_ORDER,
type LeadStatus,
} from '../lib/lead-meta';
export function LeadsRoute() {
const q = trpc.lead.list.useQuery();
const [open, setOpen] = useState(false);
if (q.isPending) return ;
const leads = q.data ?? [];
return (
{leads.length === 0 ? (
Noch keine Leads. Leg einen an.
) : (
LEAD_STATUS_ORDER.map((status) => {
const group = leads.filter((l) => l.status === status);
if (group.length === 0) return null;
return (
{LEAD_STATUS_META[status].label}{' '}
({group.length})
{group.map((l) => (
))}
);
})
)}
{open &&
setOpen(false)} />}
);
}
function LeadCard({
lead,
}: {
lead: {
id: string;
firma: string;
status: LeadStatus;
score: number;
ownerName: string | null;
branche: string | null;
subSegment: string | null;
naechsterSchrittDatum: string | null;
};
}) {
const meta = LEAD_STATUS_META[lead.status];
const sub = [lead.branche, lead.subSegment].filter(Boolean).join(' · ');
return (
{lead.firma}
{meta.label}
{sub && {sub}
}
{lead.ownerName ?? 'ohne Owner'}
Score {lead.score}/10
{lead.naechsterSchrittDatum && (
Nächster Schritt: {lead.naechsterSchrittDatum}
)}
);
}
function LeadCreateDialog({ onClose }: { onClose: () => void }) {
const users = trpc.user.list.useQuery();
const [firma, setFirma] = useState('');
const [branche, setBranche] = useState('');
const [subSegment, setSubSegment] = useState('');
const [website, setWebsite] = useState('');
const [quelle, setQuelle] = useState('');
const [ownerUserId, setOwnerUserId] = useState('');
const [status, setStatus] = useState('cold');
const [score, setScore] = useState(0);
const utils = trpc.useUtils();
const create = trpc.lead.create.useMutation({
onSuccess: async () => {
toast(voice.toast.leadCreated);
await utils.lead.list.invalidate();
onClose();
},
});
function submit(e: React.FormEvent) {
e.preventDefault();
if (!firma.trim()) return;
create.mutate({
firma: firma.trim(),
branche: branche.trim() || undefined,
subSegment: subSegment.trim() || undefined,
website: website.trim() || undefined,
quelle: quelle.trim() || undefined,
ownerUserId: ownerUserId || undefined,
status,
score,
});
}
return (
e.stopPropagation()}
>
Neuer Lead
Nur die Firma ist Pflicht — alles Weitere füllst du im Dossier.
);
}
const inputCls =
'w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm';
function Field({ label, children }: { label: string; children: React.ReactNode }) {
return (
);
}