/* TMD Portal — Clients: the group's brands and external clients (backed by public.tmd_brands).
Everyone on staff can see the list. Owners and Managers add, edit and remove.
"Managed by" comes from tmd_staff.brands (Brand managers assigned on the People page). */
const { useState: uSc, useEffect: uEc, useCallback: uCc } = React;
const slugify = s => (s || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60);
function ClientsDb({ app }) {
const [rows, setRows] = uSc(null);
const [err, setErr] = uSc('');
const [edit, setEdit] = uSc(null); // null | {} (new) | row
const [confirm, setConfirm] = uSc(null);
const isAdmin = app.role === 'Owner' || app.role === 'Manager';
const load = uCc(async () => {
const { data, error } = await SB.from('tmd_brands').select('*').order('kind').order('name');
if (error) { setErr(error.message); return; }
setErr(''); setRows(data || []);
}, []);
uEc(() => { load(); }, [load]);
const managersOf = slug => (app.staffList || []).filter(s => s.active && (s.brands || []).includes(slug)).map(s => s.name);
const remove = async r => {
const { error } = await SB.from('tmd_brands').delete().eq('id', r.id);
if (error) { app.toast('Not removed: ' + error.message); return; }
app.toast(r.name + ' removed'); load();
};
const internal = (rows || []).filter(r => r.kind === 'internal'), external = (rows || []).filter(r => r.kind === 'external');
return (
setEdit({})}>Add client : null} />
{err ? Could not load clients. {err}
: null}
{!rows && !err ? Loading
: null}
{rows ? [['In-house brands', internal], ['External clients', external]].map(([label, list], gi) => (
{label}
{list.length ? (
Client Industry Status Managed by Contact {isAdmin ? Actions : null}
{list.map(r => (
isAdmin && setEdit(r)}>
{r.name}
{r.industry || '—'}
{managersOf(r.slug).join(', ') || — }
{r.contact_name ? r.contact_name + (r.contact_channel ? ' · ' + r.contact_channel : '') : '—'}
{isAdmin ? { e.stopPropagation(); setEdit(r); }}>Edit { e.stopPropagation(); setConfirm(r); }}>Remove : null}
))}
) : }
)) : null}
setEdit(null)} onSaved={msg => { setEdit(null); app.toast(msg); load(); }} />
This deletes the client record. Brand managers assigned to it keep their other brands. Use status “Ended” if you only want to close the account.}
confirmLabel="Remove" onConfirm={async () => { const r = confirm; setConfirm(null); await remove(r); }} onClose={() => setConfirm(null)} />
);
}
function ClientSheet({ row, onClose, onSaved }) {
const isNew = row && !row.id;
const [f, setF] = uSc({});
const [busy, setBusy] = uSc(false);
const [err, setErr] = uSc('');
uEc(() => {
if (row) { setF({ name: row.name || '', kind: row.kind || 'external', industry: row.industry || '', status: row.status || 'Active', colour: row.colour || '#7D1AFF', contact_name: row.contact_name || '', contact_channel: row.contact_channel || '', notes: row.notes || '' }); setErr(''); setBusy(false); }
}, [row && (row.id || 'new')]);
if (!row) return null;
const set = k => e => setF(x => Object.assign({}, x, { [k]: e.target.value }));
const save = async () => {
setErr('');
if (!f.name.trim()) { setErr('A name is needed.'); return; }
setBusy(true);
const payload = { name: f.name.trim(), kind: f.kind, industry: f.industry.trim() || null, status: f.status, colour: /^#[0-9a-fA-F]{6}$/.test(f.colour) ? f.colour : '#7D1AFF', contact_name: f.contact_name.trim() || null, contact_channel: f.contact_channel.trim() || null, notes: f.notes.trim() || null };
let error;
if (isNew) ({ error } = await SB.from('tmd_brands').insert(Object.assign({ slug: slugify(f.name) || ('client-' + Date.now()) }, payload)));
else ({ error } = await SB.from('tmd_brands').update(payload).eq('id', row.id));
setBusy(false);
if (error) { setErr(error.message); return; }
onSaved(isNew ? f.name.trim() + ' added' : 'Saved');
};
return (
Cancel {busy ? 'Working' : isNew ? 'Add client' : 'Save'} >}>
Name
Type
In-house brand External client
Status
Active Paused Ended
Industry
Calendar colour
Contact name
Contact channel
Notes
{err ? {err}
: null}
);
}
Object.assign(window, { ClientsDb, ClientSheet });