// CRM → Customers (plan step 6.13b; replaces the v1 CustomersPage): the venue's regular // customers. Search by name, nickname, phone, e-mail or notes (case and Greek accents ignored); // each with visits, what they spent (counted as in the reports: refunds off, tab lines in) and // their last visit. A tap opens their card: contact, notes (allergies, likes), their open tab, // every visit. New, edit, remove (a removed customer stays on old orders). Live: `customers.changed`. import { useDeferredValue, useState } from 'react' import { useMutation } from '@tanstack/react-query' import toast from 'react-hot-toast' import { Pencil, Plus, Search, Trash2, Users } from 'lucide-react' import { errorMessage } from '@xenia/api' import { useFormat, useTranslation } from '@xenia/i18n' import { Button, Dialog, EmptyState, IconButton, Input, Spinner } from '@xenia/ui' import client from '../../api/client' import { useLiveQuery } from '../../live/useLiveQuery' import { Avatar } from '../overview/parts' import { Panel, Table } from '../reports/kit/parts' import CustomerCard from './CustomerCard' import CustomerDialog from './CustomerDialog' export default function CustomersPage() { const { t } = useTranslation('manager') const fmt = useFormat() const [q, setQ] = useState('') const search = useDeferredValue(q.trim()) const [dialog, setDialog] = useState(null) // { kind: 'new' | 'edit' | 'card' | 'remove', customer } const { data, isPending, refetch } = useLiveQuery({ queryKey: ['customers', search], placeholderData: prev => prev, queryFn: () => client.get('/api/customers/', { params: search ? { search } : {} }).then(r => r.data), events: ['customers.changed', 'order_closed', 'order_paid'], }) const rows = data ?? [] const close = () => setDialog(null) const saved = () => { close(); refetch() } const remove = useMutation({ mutationFn: c => client.delete(`/api/customers/${c.id}`), onSuccess: (_r, c) => { toast.success(t('customers.removed', { name: c.name })); saved() }, onError: err => toast.error(errorMessage(err, t)), }) const stop = fn => ev => { ev.stopPropagation(); fn() } const columns = [ { key: 'name', label: t('customers.name'), render: c => ( {c.name}{c.nickname && «{c.nickname}»} {c.notes && {c.notes}} ) }, { key: 'phone', label: t('customers.phone'), render: c => (c.phone ? ev.stopPropagation()} className="text-accent-ink hover:underline">{c.phone} : '') }, { key: 'visit_count', label: t('customers.visits'), align: 'right' }, { key: 'total_spent', label: t('customers.spent'), align: 'right', tip: t('customers.spentTip'), render: c => (c.total_spent ? fmt.formatMoney(c.total_spent) : '') }, { key: 'last_visit', label: t('customers.lastVisit'), sort: c => c.last_visit ?? '', render: c => (c.last_visit ? fmt.formatDate(c.last_visit) : '') }, { key: 'actions', label: '', sort: false, render: c => ( setDialog({ kind: 'edit', customer: c }))}> setDialog({ kind: 'remove', customer: c }))}> ) }, ] return (

{t('customers.title')}

{t('customers.subtitle')}

{isPending ?
: !rows.length && !search ? } title={t('customers.none')} description={t('customers.noneHint')} /> : ( setDialog({ kind: 'card', customer: c })} rowTestId="customer-row" empty={t('customers.noMatch')} initialSort={{ key: 'name', dir: 'asc' }} /> )} {(dialog?.kind === 'new' || dialog?.kind === 'edit') && } {dialog?.kind === 'card' && setDialog({ kind: 'edit', customer: dialog.customer })} />} {dialog?.kind === 'remove' && ( } /> )} ) }