import { memo, useCallback, useRef } from 'react' import { Box, Typography } from '@mui/material' import type { ExposeLead } from '../../domain/exposeLead' import type { Property } from '../../domain/property' import { ObjectDeepLink } from '../team' import { LeadWorkspace } from './LeadWorkspace' import { DS_SLATE, DS_TEXT } from '../../lib/ds' export const LEAD_COLS = '110px 1.2fr 1.4fr 1fr 1.4fr' const COLUMN_LABELS = ['Datum', 'Möglicher Interessent', 'Kontaktdaten', 'Raum', 'Objektempfehlung'] interface RowProps { lead: ExposeLead properties: Property[] selected: boolean onSelect: (id: string, row: HTMLElement | null) => void } const LeadRow = memo(function LeadRow({ lead, properties, selected, onSelect }: RowProps) { const ref = useRef(null) const recommended = lead.propertyIds .map(id => properties.find(p => p.id === id)) .filter((p): p is Property => Boolean(p)) return ( onSelect(lead.id, ref.current)} sx={{ display: 'grid', gridTemplateColumns: LEAD_COLS, alignItems: 'flex-start', gap: 1, px: 2, py: 1.5, borderBottom: '1px solid #f1f5f9', bgcolor: selected ? '#f8fafc' : 'white', cursor: 'pointer', '&:hover': { bgcolor: DS_SLATE[50] }, transition: 'background-color 0.1s', }} > {new Date(lead.receivedAt).toLocaleDateString('de-CH')} {lead.prospect} {lead.contacts.length === 0 ? ( – ) : ( lead.contacts.map(c => ( {c} )) )} {lead.locationHint} {/* Mehrere Objekte je Lead — jedes verlinkt nach «Meine Objekte». */} e.stopPropagation()}> {recommended.length === 0 ? ( – ) : ( recommended.map(p => ( )) )} ) }) interface Props { leads: ExposeLead[] properties: Property[] selectedId: string | null onSelect: (id: string, row: HTMLElement | null) => void /** Arbeitsbereich nur bei aktiven Leads — Archiviertes wird nicht bearbeitet. */ withWorkspace: boolean emptyText: string } /** * Livias Leadliste im Grunddesign der Ferdi-Liste (Runde 4, §8.3). * * Der Arbeitsbereich öffnet sich zwischen der gewählten Zeile und den übrigen * Leads — nicht in einem Drawer. Das hält den Bezug zum Lead sichtbar, während * gearbeitet wird. */ export function LeadTable({ leads, properties, selectedId, onSelect, withWorkspace, emptyText }: Props) { const handleSelect = useCallback( (id: string, row: HTMLElement | null) => onSelect(id, row), [onSelect], ) if (leads.length === 0) { return ( {emptyText} ) } return ( {COLUMN_LABELS.map(h => ( {h} ))} {leads.map(lead => ( {withWorkspace && lead.id === selectedId && } ))} ) }