import { h } from '/static/admin2/vendor/preact.module.js'; import { useEffect, useState } from '/static/admin2/vendor/hooks.module.js'; import htm from '/static/admin2/vendor/htm.module.js'; import { api } from '/static/admin2/api.js'; import { Drawer } from '/static/admin2/ui/drawer.js'; const html = htm.bind(h); const rub = (v) => v == null ? '—' : Number(v).toLocaleString('ru-RU', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' ₽'; const when = (ts) => ts ? new Date(ts * 1000).toLocaleString('ru-RU') : '—'; const TABS = [['overview', 'Обзор'], ['dialogs', 'Диалоги'], ['ledger', 'Счёт'], ['files', 'Файлы'], ['history', 'История']]; export function CustomerDrawer({ email, onClose }) { const [card, setCard] = useState(null); const [tab, setTab] = useState('overview'); const [rows, setRows] = useState([]); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); useEffect(() => { let alive = true; setCard(null); setError(''); setTab('overview'); setRows([]); setLoading(false); api('/api/admin/v2/customers/' + encodeURIComponent(email)) .then((d) => { if (alive) setCard(d); }) .catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить'); }); return () => { alive = false; }; }, [email]); useEffect(() => { if (tab === 'overview' || !card) return; let alive = true; const suffix = {dialogs: 'dialogs', ledger: 'ledger', files: 'files', history: 'history'}[tab]; setError(''); setLoading(true); api('/api/admin/v2/customers/' + encodeURIComponent(email) + '/' + suffix) .then((d) => { if (alive) setRows(d.items || d.entries || d); }) .catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить вкладку'); }) .finally(() => { if (alive) setLoading(false); }); return () => { alive = false; }; }, [tab, card, email]); const title = card ? card.profile.email : 'Карточка клиента'; return html` <${Drawer} title=${title} onClose=${onClose}> ${error ? html`
${error}
` : null} ${!card ? html`Загрузка…
` : html` ${tab === 'overview' ? html`Загрузка…
` : rows.length === 0 ? html`Пусто.
` : html`${JSON.stringify(rows.slice(0, 20), null, 2)}`}