Ein Projekt lebt in genau einem Heimat-Bereich und wird einmal angelegt; Leads bekommen eine optionale Verknüpfung dorthin (lead.projectId), egal in welchem Bereich das Projekt wohnt — kein redundantes Anlegen mehr. - Migration 0011: lead.project_id (FK → project, ON DELETE SET NULL). - Lead anlegen / CSV-Import / Lead-Detail: Projekt zuordnen; CSV ordnet alle importierten Leads einem Projekt zu. - Projektseite: neue "Leads"-Sektion (Vertriebs-Sicht des Projekts). - Vertrieb: "Projekte mit Vertriebsaktivität" — bereichsübergreifend alle Projekte mit verknüpften Leads (so erscheint ein Entwicklungsprojekt hier, WEIL es Leads hat). Rein interne Projekte ohne Leads tauchen nicht auf. - Lead-Liste/Detail zeigen das verknüpfte Projekt. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
462 lines
16 KiB
TypeScript
462 lines
16 KiB
TypeScript
import { Link, NavLink, useParams } from 'react-router';
|
|
import { useState, useEffect } from 'react';
|
|
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 { toast } from 'sonner';
|
|
import { clsx } from 'clsx';
|
|
import { LEAD_STATUS_META, type LeadStatus } from '../lib/lead-meta';
|
|
import { MODUS_META, type ProjectModus } from '../lib/project-meta';
|
|
|
|
const BEREICHE: Array<{ slug: 'entwicklung' | 'vertrieb' | 'marketing'; label: string }> = [
|
|
{ slug: 'entwicklung', label: 'Entwicklung' },
|
|
{ slug: 'vertrieb', label: 'Vertrieb' },
|
|
{ slug: 'marketing', label: 'Marketing' },
|
|
];
|
|
|
|
function BereichTabs({ active }: { active: string }) {
|
|
return (
|
|
<nav aria-label="Bereich wählen" className="flex gap-1 border-b border-[var(--color-border)]">
|
|
{BEREICHE.map((b) => (
|
|
<NavLink
|
|
key={b.slug}
|
|
to={`/bereich/${b.slug}`}
|
|
className={clsx(
|
|
'inline-flex h-10 items-center rounded-t-[var(--radius-md)] px-4 text-sm font-medium',
|
|
active === b.slug
|
|
? 'border-b-2 border-[var(--color-ink)] text-[var(--color-ink)]'
|
|
: 'text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] hover:bg-black/5',
|
|
)}
|
|
>
|
|
{b.label}
|
|
</NavLink>
|
|
))}
|
|
</nav>
|
|
);
|
|
}
|
|
|
|
export function BereichRoute() {
|
|
const { slug } = useParams<{ slug?: string }>();
|
|
// Default: Entwicklung, wenn /bereich ohne Slug.
|
|
const effective = slug && BEREICHE.some((b) => b.slug === slug) ? slug : 'entwicklung';
|
|
return (
|
|
<div className="space-y-6">
|
|
<BereichTabs active={effective} />
|
|
{effective === 'entwicklung' && <EntwicklungDetail />}
|
|
{effective === 'vertrieb' && <VertriebDetail />}
|
|
{effective === 'marketing' && <MarketingDetail />}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function EntwicklungDetail() {
|
|
const q = trpc.project.byAreaSlug.useQuery({ slug: 'entwicklung' });
|
|
const areas = trpc.area.list.useQuery();
|
|
const [open, setOpen] = useState(false);
|
|
if (q.isPending) return <SkeletonCard lines={5} />;
|
|
const projects = q.data ?? [];
|
|
const kunden = projects.filter((p) => p.kind === 'kundenprojekt');
|
|
const intern = projects.filter((p) => p.kind === 'internes_projekt');
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex items-center justify-end">
|
|
<Button onClick={() => setOpen(true)}>{voice.cta.newProject}</Button>
|
|
</div>
|
|
<Section title="Kundenprojekte" projects={kunden} />
|
|
<Section title="Interne Projekte" projects={intern} />
|
|
{open && (
|
|
<ProjectCreateDialog
|
|
onClose={() => setOpen(false)}
|
|
areas={areas.data ?? []}
|
|
defaultSlug="entwicklung"
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Section({
|
|
title,
|
|
projects,
|
|
}: {
|
|
title: string;
|
|
projects: Array<{
|
|
id: string;
|
|
name: string;
|
|
statusCache: 'green' | 'amber' | 'red' | null;
|
|
deadline: string | null;
|
|
modus: ProjectModus;
|
|
pflichtMinimumState: Record<string, boolean>;
|
|
}>;
|
|
}) {
|
|
return (
|
|
<section>
|
|
<h2 className="mb-2 text-lg font-semibold">{title}</h2>
|
|
{projects.length === 0 ? (
|
|
<Card.Root>
|
|
<Card.Body>
|
|
<p className="text-sm text-[var(--color-ink-muted)]">{voice.empty.projects}</p>
|
|
</Card.Body>
|
|
</Card.Root>
|
|
) : (
|
|
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
|
{projects.map((p) => (
|
|
<Link key={p.id} to={`/projekt/${p.id}`} className="contents">
|
|
<Card.Root className="cursor-pointer hover:shadow-[var(--shadow-md)]">
|
|
<Card.Body className="flex items-center justify-between">
|
|
<div>
|
|
<p className="flex items-center gap-2 font-medium">
|
|
{p.name}
|
|
{p.modus !== 'standard' && (
|
|
<span
|
|
className={clsx(
|
|
'rounded-full px-2 py-0.5 text-[10px] font-medium',
|
|
MODUS_META[p.modus].badge,
|
|
)}
|
|
>
|
|
{MODUS_META[p.modus].label}
|
|
</span>
|
|
)}
|
|
</p>
|
|
<p className="text-xs text-[var(--color-ink-muted)]">
|
|
{p.deadline ? `bis ${p.deadline}` : 'kein Deadline'}
|
|
</p>
|
|
</div>
|
|
<PflichtMinimumIndicator state={p.pflichtMinimumState} />
|
|
</Card.Body>
|
|
</Card.Root>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function PflichtMinimumIndicator({ state }: { state: Record<string, boolean> }) {
|
|
const points = [
|
|
state.steckbrief_freigegeben,
|
|
state.verantwortlicher,
|
|
state.plane_board,
|
|
state.meilensteine,
|
|
state.status_aktuell,
|
|
state.retrospektive,
|
|
];
|
|
const filled = points.filter(Boolean).length;
|
|
return (
|
|
<div className="flex items-center gap-2" title={`Pflicht-Minimum ${filled}/6`}>
|
|
<div className="flex gap-0.5">
|
|
{points.map((p, i) => (
|
|
<span
|
|
key={i}
|
|
className={clsx(
|
|
'h-2 w-2 rounded-sm',
|
|
p ? 'bg-[var(--color-ink)]' : 'bg-[var(--color-border)]',
|
|
)}
|
|
/>
|
|
))}
|
|
</div>
|
|
<span className="text-xs text-[var(--color-ink-muted)]">{filled}/6</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function ProjectCreateDialog({
|
|
onClose,
|
|
areas,
|
|
defaultSlug,
|
|
}: {
|
|
onClose: () => void;
|
|
areas: Array<{ id: string; slug: string; name: string }>;
|
|
defaultSlug: string;
|
|
}) {
|
|
const defaultArea = areas.find((a) => a.slug === defaultSlug);
|
|
const [name, setName] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [areaId, setAreaId] = useState(defaultArea?.id ?? '');
|
|
|
|
// Areas werden asynchron geladen; sobald sie da sind und noch nichts gewählt
|
|
// wurde, sinnvollen Default setzen (Dropdown war sonst leer).
|
|
useEffect(() => {
|
|
if (!areaId && areas.length > 0) {
|
|
const fallback = areas.find((a) => a.slug === defaultSlug) ?? areas[0];
|
|
if (fallback) setAreaId(fallback.id);
|
|
}
|
|
}, [areas, areaId, defaultSlug]);
|
|
const [kind, setKind] = useState<'kundenprojekt' | 'internes_projekt'>('internes_projekt');
|
|
const [arbeitsmodus, setArbeitsmodus] = useState<'cycle' | 'kanban'>('kanban');
|
|
const [steckbrief, setSteckbrief] = useState('');
|
|
const [plane, setPlane] = useState('');
|
|
|
|
const utils = trpc.useUtils();
|
|
const create = trpc.project.create.useMutation({
|
|
onSuccess: async () => {
|
|
toast(voice.toast.projectCreated);
|
|
await utils.project.byAreaSlug.invalidate();
|
|
await utils.dashboard.overview.invalidate();
|
|
onClose();
|
|
},
|
|
});
|
|
|
|
function submit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (!areaId) return;
|
|
create.mutate({
|
|
areaId,
|
|
name,
|
|
description: description.trim() || undefined,
|
|
kind,
|
|
arbeitsmodus,
|
|
outlineSteckbriefUrl: steckbrief,
|
|
planeProjectUrl: plane,
|
|
milestones: [],
|
|
});
|
|
}
|
|
return (
|
|
<div
|
|
className="fixed inset-0 z-50 flex items-center justify-center bg-black/30"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className="w-full max-w-md rounded-[var(--radius-xl)] bg-[var(--color-bg)] p-6 shadow-[var(--shadow-lg)]"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<h2 className="text-xl font-semibold">Neues Projekt</h2>
|
|
<form onSubmit={submit} className="mt-4 space-y-3">
|
|
<Field label="Name">
|
|
<input
|
|
required
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
/>
|
|
</Field>
|
|
<Field label="Beschreibung (optional)">
|
|
<textarea
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
maxLength={2000}
|
|
rows={3}
|
|
placeholder="Worum geht's? Kurz beschreiben."
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
/>
|
|
</Field>
|
|
<Field label="Bereich">
|
|
<select
|
|
required
|
|
value={areaId}
|
|
onChange={(e) => setAreaId(e.target.value)}
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
>
|
|
<option value="" disabled>
|
|
{areas.length ? '— Bereich wählen —' : 'Bereiche werden geladen…'}
|
|
</option>
|
|
{areas.map((a) => (
|
|
<option key={a.id} value={a.id}>
|
|
{a.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Projektart">
|
|
<select
|
|
value={kind}
|
|
onChange={(e) => setKind(e.target.value as 'kundenprojekt' | 'internes_projekt')}
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
>
|
|
<option value="kundenprojekt">Kundenprojekt</option>
|
|
<option value="internes_projekt">Internes Projekt</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="Arbeitsmodus">
|
|
<select
|
|
value={arbeitsmodus}
|
|
onChange={(e) => setArbeitsmodus(e.target.value as 'cycle' | 'kanban')}
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
>
|
|
<option value="kanban">Kanban</option>
|
|
<option value="cycle">Cycle</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="Outline-Steckbrief-URL">
|
|
<input
|
|
required
|
|
type="url"
|
|
value={steckbrief}
|
|
onChange={(e) => setSteckbrief(e.target.value)}
|
|
placeholder="https://outline.tool-ctrl.de/..."
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
/>
|
|
</Field>
|
|
<Field label="Plane-Board-URL">
|
|
<input
|
|
required
|
|
type="url"
|
|
value={plane}
|
|
onChange={(e) => setPlane(e.target.value)}
|
|
placeholder="https://plane.tool-ctrl.de/..."
|
|
className="w-full rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm"
|
|
/>
|
|
</Field>
|
|
<div className="flex justify-end gap-2 pt-2">
|
|
<Button type="button" variant="ghost" onClick={onClose}>
|
|
Abbrechen
|
|
</Button>
|
|
<Button type="submit" disabled={create.isPending}>
|
|
Anlegen
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Field({ label, children }: { label: string; children: React.ReactNode }) {
|
|
return (
|
|
<label className="block">
|
|
<span className="text-sm font-medium">{label}</span>
|
|
<div className="mt-1">{children}</div>
|
|
</label>
|
|
);
|
|
}
|
|
|
|
function VertriebDetail() {
|
|
return (
|
|
<div className="space-y-6">
|
|
<LeadsTeaser />
|
|
<ProjekteMitLeads />
|
|
<ProjektListeFuerBereich slug="vertrieb" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Bereichsübergreifend: Projekte (egal aus welchem Bereich), die verknüpfte
|
|
// Leads haben — so taucht z. B. ein Entwicklungsprojekt hier auf, WEIL es
|
|
// Vertriebsaktivität hat, ohne redundant angelegt werden zu müssen.
|
|
function ProjekteMitLeads() {
|
|
const q = trpc.lead.list.useQuery();
|
|
const leads = q.data ?? [];
|
|
const map = new Map<string, { id: string; name: string; total: number; open: number }>();
|
|
for (const l of leads) {
|
|
if (!l.projectId || !l.projectName) continue;
|
|
const e = map.get(l.projectId) ?? { id: l.projectId, name: l.projectName, total: 0, open: 0 };
|
|
e.total++;
|
|
if (l.status !== 'won' && l.status !== 'lost') e.open++;
|
|
map.set(l.projectId, e);
|
|
}
|
|
const projects = Array.from(map.values()).sort((a, b) => b.open - a.open);
|
|
if (projects.length === 0) return null;
|
|
return (
|
|
<section>
|
|
<h2 className="mb-2 text-lg font-semibold">Projekte mit Vertriebsaktivität</h2>
|
|
<div className="grid grid-cols-1 gap-3 md:grid-cols-2 lg:grid-cols-3">
|
|
{projects.map((p) => (
|
|
<Link key={p.id} to={`/projekt/${p.id}`} className="contents">
|
|
<Card.Root className="cursor-pointer hover:shadow-[var(--shadow-md)]">
|
|
<Card.Body className="flex items-center justify-between gap-2">
|
|
<p className="font-medium">{p.name}</p>
|
|
<span className="shrink-0 text-xs text-[var(--color-ink-muted)]">
|
|
{p.total} {p.total === 1 ? 'Lead' : 'Leads'}
|
|
{p.open > 0 && ` · ${p.open} offen`}
|
|
</span>
|
|
</Card.Body>
|
|
</Card.Root>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function LeadsTeaser() {
|
|
const q = trpc.lead.list.useQuery();
|
|
const leads = q.data ?? [];
|
|
const offen = leads.filter((l) => l.status !== 'won' && l.status !== 'lost');
|
|
const top = [...offen]
|
|
.sort((a, b) => b.score - a.score)
|
|
.slice(0, 3);
|
|
return (
|
|
<section>
|
|
<div className="mb-2 flex items-center justify-between">
|
|
<h2 className="text-lg font-semibold">Leads</h2>
|
|
<Link to="/leads" className="text-sm text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] hover:underline">
|
|
Alle Leads ({leads.length}) →
|
|
</Link>
|
|
</div>
|
|
<Card.Root>
|
|
<Card.Body>
|
|
{q.isPending ? (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">Lädt…</p>
|
|
) : offen.length === 0 ? (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
Keine offenen Leads.{' '}
|
|
<Link to="/leads" className="hover:underline">
|
|
Lege einen an.
|
|
</Link>
|
|
</p>
|
|
) : (
|
|
<ul className="divide-y divide-[var(--color-border)]">
|
|
{top.map((l) => {
|
|
const meta = LEAD_STATUS_META[l.status as LeadStatus];
|
|
return (
|
|
<li key={l.id}>
|
|
<Link
|
|
to={`/lead/${l.id}`}
|
|
className="flex items-center justify-between gap-2 py-2 hover:opacity-80"
|
|
>
|
|
<span className="flex items-center gap-2 text-sm">
|
|
<span className={clsx('h-2 w-2 rounded-full', meta.dot)} />
|
|
<span className="font-medium">{l.firma}</span>
|
|
{l.subSegment && (
|
|
<span className="text-xs text-[var(--color-ink-muted)]">{l.subSegment}</span>
|
|
)}
|
|
</span>
|
|
<span className="font-mono text-xs tabular-nums text-[var(--color-ink-muted)]">
|
|
{l.score}/10
|
|
</span>
|
|
</Link>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
)}
|
|
</Card.Body>
|
|
</Card.Root>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function MarketingDetail() {
|
|
return <ProjektListeFuerBereich slug="marketing" />;
|
|
}
|
|
|
|
function ProjektListeFuerBereich({ slug }: { slug: 'vertrieb' | 'marketing' }) {
|
|
const q = trpc.project.byAreaSlug.useQuery({ slug });
|
|
const areas = trpc.area.list.useQuery();
|
|
const [open, setOpen] = useState(false);
|
|
if (q.isPending) return <SkeletonCard lines={5} />;
|
|
const projects = q.data ?? [];
|
|
const kunden = projects.filter((p) => p.kind === 'kundenprojekt');
|
|
const intern = projects.filter((p) => p.kind === 'internes_projekt');
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex items-center justify-end">
|
|
<Button onClick={() => setOpen(true)}>{voice.cta.newProject}</Button>
|
|
</div>
|
|
<Section title="Kundenprojekte" projects={kunden} />
|
|
<Section title="Interne Projekte" projects={intern} />
|
|
{open && (
|
|
<ProjectCreateDialog
|
|
onClose={() => setOpen(false)}
|
|
areas={areas.data ?? []}
|
|
defaultSlug={slug}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|