import { useCallback, useEffect, useMemo, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import PageHeader from '@dukang/shared-ui/PageHeader'; import { AccountStatus, PARTNER_STAFF_ROLE_LABELS, PartnerStaffRole, type PartnerStaffItem, } from '@dukang/shared-types'; import { deletePartnerStaff, listPartnerStaff, updatePartnerStaff } from '../lib/staff'; export default function StaffListPage() { const navigate = useNavigate(); const [staff, setStaff] = useState([]); const [q, setQ] = useState(''); const [error, setError] = useState(''); const [busyId, setBusyId] = useState(null); const loadStaff = useCallback(() => { return listPartnerStaff().then(setStaff).catch((e) => { setError(e instanceof Error ? e.message : '加载失败'); }); }, []); useEffect(() => { void loadStaff(); }, [loadStaff]); const filtered = useMemo(() => staff.filter((item) => { if (!q.trim()) return true; const keyword = q.trim(); return item.name.includes(keyword) || item.phone.includes(keyword); }), [staff, q]); async function toggleStatus(item: PartnerStaffItem) { const next = item.status === AccountStatus.ACTIVE ? AccountStatus.DISABLED : AccountStatus.ACTIVE; setBusyId(item.id); setError(''); try { await updatePartnerStaff(item.id, { status: next }); await loadStaff(); } catch (e) { setError(e instanceof Error ? e.message : '操作失败'); } finally { setBusyId(null); } } async function removeStaff(item: PartnerStaffItem) { const ok = window.confirm(`确认删除子账号「${item.name}」?`); if (!ok) return; setBusyId(item.id); setError(''); try { await deletePartnerStaff(item.id); await loadStaff(); } catch (e) { setError(e instanceof Error ? e.message : '删除失败'); } finally { setBusyId(null); } } async function editStaff(item: PartnerStaffItem) { const name = window.prompt('修改姓名', item.name); if (name == null || !name.trim()) return; const roleOptions = Object.values(PartnerStaffRole); const roleLabels = roleOptions.map((r) => PARTNER_STAFF_ROLE_LABELS[r]).join(' / '); const roleInput = window.prompt(`分配角色(${roleLabels})`, item.staffRole); if (roleInput == null) return; const staffRole = roleOptions.find((r) => r === roleInput || PARTNER_STAFF_ROLE_LABELS[r] === roleInput); if (!staffRole) { setError('无效的角色'); return; } setBusyId(item.id); setError(''); try { await updatePartnerStaff(item.id, { name: name.trim(), staffRole }); await loadStaff(); } catch (e) { setError(e instanceof Error ? e.message : '保存失败'); } finally { setBusyId(null); } } return (
navigate('/center')} />
search setQ(e.target.value)} />
{error &&

{error}

}
{filtered.length === 0 && (

暂无子账号

点击下方按钮添加首个子账号

)} {filtered.map((item) => { const active = item.status === AccountStatus.ACTIVE; const busy = busyId === item.id; return (
{item.name.slice(0, 1)}
{item.name} {PARTNER_STAFF_ROLE_LABELS[item.staffRole]}

{item.phone}

); })}
person_add 添加子账号
); }