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 { Table } from '/static/admin2/ui/table.js'; const html = htm.bind(h); const CAT = {wrong_answer: 'неверный ответ', off_topic: 'не по делу', failure: 'сбой', praise: 'похвала'}; const ST = {new: 'новое', resolved: 'разобрано', deferred: 'отложено', ignored: 'игнор'}; const when = (ts) => new Date(ts * 1000).toLocaleString('ru-RU'); function FeedbackTab() { const [status, setStatus] = useState('new'); const [category, setCategory] = useState(''); const [page, setPage] = useState(1); const [data, setData] = useState(null); const [error, setError] = useState(''); const [reload, setReload] = useState(0); useEffect(() => { let alive = true; const p = new URLSearchParams({page: String(page), per_page: '50'}); if (status) p.set('status', status); if (category) p.set('category', category); api('/api/admin/v2/feedback?' + p).then((d) => { if (alive) { setData(d); setError(''); } }).catch((e) => { if (alive) setError(e.message || 'Жалобы не загрузились'); }); return () => { alive = false; }; }, [status, category, page, reload]); const mark = async (id, next) => { try { await api('/api/admin/v2/feedback/' + id + '/status', {method: 'POST', body: JSON.stringify({status: next})}); setReload((n) => n + 1); } catch (e) { setError(e.message || 'Не удалось разобрать'); } }; const columns = [ {key: 'timestamp', label: 'Когда', render: (r) => when(r.timestamp)}, {key: 'username', label: 'Клиент'}, {key: 'category', label: 'Категория', render: (r) => CAT[r.category] || 'без разбора'}, {key: 'comment', label: 'Комментарий', render: (r) => html`
${r.comment || 'без текста'}${r.answer ? html`${String(r.answer).slice(0, 160)}` : null}
`}, {key: 'status', label: 'Статус', render: (r) => html`${ST[r.status] || r.status}`}, {key: 'actions', label: 'Действие', render: (r) => html`
`}, ]; return html`
${data ? 'Всего: ' + data.total : ''}
${error ? html`` : null} ${data ? html`<${Table} columns=${columns} rows=${data.items.map((r) => ({...r, key: r.id}))} empty="Жалоб нет" />` : html`
Загрузка…
`}`; } function Paged({ title, path, columns, search = true, extra = null, params = {} }) { const [q, setQ] = useState(''); const [page, setPage] = useState(1); const [data, setData] = useState(null); const [error, setError] = useState(''); useEffect(() => { let alive = true; const p = new URLSearchParams({page: String(page), per_page: '50', ...params}); if (q.trim()) p.set('q', q.trim()); const timer = setTimeout(() => api(path + '?' + p).then((d) => { if (alive) { setData(d); setError(''); } }) .catch((e) => { if (alive) setError(e.message || 'Не загрузилось'); }), 300); return () => { alive = false; clearTimeout(timer); }; }, [path, q, page, JSON.stringify(params)]); return html`
${search ? html` { setQ(e.currentTarget.value); setPage(1); }} />` : null} ${extra} ${data ? 'Всего: ' + data.total : ''}
${error ? html`` : null} ${data ? html`<${Table} columns=${columns} rows=${data.items.map((r) => ({...r, key: r.id || (r.username + r.tab_id)}))} empty="Пусто" />` : html`
Загрузка…
`}`; } function DialogsTab() { return html`<${Paged} title="Диалоги" path="/api/admin/v2/work/dialogs" columns=${[ {key: 'username', label: 'Клиент'}, {key: 'title', label: 'Название', render: (r) => r.title || 'без названия'}, {key: 'last_question', label: 'Последний вопрос', render: (r) => String(r.last_question || '').slice(0, 120)}, {key: 'last_ts', label: 'Был(а)', render: (r) => new Date(r.last_ts * 1000).toLocaleString('ru-RU')}, {key: 'msg_count', label: 'Сообщений', num: true}, ]} />`; } function FailuresTab() { const [days, setDays] = useState(7); return html`<${Paged} title="Сбои" path="/api/admin/v2/work/failures" search=${false} params=${{days: String(days)}} extra=${html``} columns=${[ {key: 'timestamp', label: 'Когда', render: (r) => new Date(r.timestamp * 1000).toLocaleString('ru-RU')}, {key: 'username', label: 'Клиент'}, {key: 'detail', label: 'Что случилось'}, {key: 'path', label: 'Где'}, ]} />`; } function JournalTab() { return html`<${Paged} title="Журнал" path="/api/admin/v2/work/journal" columns=${[ {key: 'timestamp', label: 'Когда', render: (r) => new Date(r.timestamp * 1000).toLocaleString('ru-RU')}, {key: 'label', label: 'Событие'}, {key: 'username', label: 'Кто'}, {key: 'detail', label: 'Подробности'}, ]} />`; } export function WorkPage() { const [tab, setTab] = useState('feedback'); const tabs = [['feedback', 'Жалобы'], ['dialogs', 'Диалоги'], ['failures', 'Сбои'], ['journal', 'Журнал']]; return html` ${tab === 'feedback' ? html`<${FeedbackTab} />` : tab === 'dialogs' ? html`<${DialogsTab} />` : tab === 'failures' ? html`<${FailuresTab} />` : html`<${JournalTab} />`}`; }