feat(админка): таблица клиентов v2

This commit is contained in:
anor
2026-11-06 21:35:34 +10:00
parent 47c1a93b99
commit c7558f2772
7 changed files with 220 additions and 1 deletions
+3 -1
View File
@@ -2,6 +2,7 @@ import { h, render } from '/static/admin2/vendor/preact.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
import { SECTIONS, sectionFromPath, hrefFor } from '/static/admin2/router.js';
import { DayPage } from '/static/admin2/pages/day.js';
import { CustomersPage } from '/static/admin2/pages/customers.js';
const html = htm.bind(h);
const TITLES = {
@@ -25,7 +26,8 @@ const App = () => html`
</nav>
<main class="main">
<header class="top"><div><h1>${title}</h1><p>${subtitle}</p></div></header>
${section === 'today' ? html`<${DayPage} />` : html`
${section === 'today' ? html`<${DayPage} />`
: section === 'customers' ? html`<${CustomersPage} />` : html`
<section class="card" aria-labelledby="placeholder">
<h2 id="placeholder">Раздел готовится</h2>
<p>Данные и действия подключит следующий этап.</p>
+64
View File
@@ -0,0 +1,64 @@
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 money = (v) => v == null ? '—' : Number(v).toLocaleString('ru-RU',
{minimumFractionDigits: 0, maximumFractionDigits: 2}) + ' ₽';
const when = (ts) => ts ? new Date(ts * 1000).toLocaleString('ru-RU',
{day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit'}) : '—';
const STATUS = {active: 'активен', blocked: 'заблокирован', deleted: 'удалён',
read_only: 'только просмотр', exhausted: 'исчерпан', trial: 'пробный',
unverified: 'не подтверждён'};
export function CustomersPage() {
const initial = new URLSearchParams(location.search);
const [query, setQuery] = useState(initial.get('q') || '');
const [status, setStatus] = useState(initial.get('status') || '');
const [page, setPage] = useState(Math.max(1, parseInt(initial.get('page'), 10) || 1));
const [attempt, setAttempt] = useState(0);
const [data, setData] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
let alive = true;
const p = new URLSearchParams({page: String(page), per_page: '50'});
if (query.trim()) p.set('q', query.trim());
if (status) p.set('status', status);
history.replaceState(null, '', '/admin/v2/customers?' + p.toString());
api('/api/admin/v2/customers?' + p).then((d) => {
if (alive) { setData(d); setError(''); }
}).catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить'); });
return () => { alive = false; };
}, [query, status, page, attempt]);
const columns = [
{key: 'email', label: 'Клиент', render: (r) => html`
<div class="row-client"><b>${r.email}</b>
<small>${r.role}${r.is_owner ? ' · владелец' : ''}${r.telegram ? ' · ' + r.telegram : ''}</small></div>`},
{key: 'status', label: 'Статус', render: (r) => html`<span class=${'badge s-' + r.status}>${STATUS[r.status] || r.status}</span>`},
{key: 'last_seen', label: 'Был(а)', render: (r) => when(r.last_seen)},
{key: 'spent_rub_24h', label: '24 ч', num: true, render: (r) => money(r.spent_rub_24h)},
{key: 'balance_rub', label: 'Баланс', num: true, render: (r) => money(r.balance_rub)},
{key: 'access', label: 'Доступ', render: (r) => (r.agent_access ? 'агент' : 'справка') + ' · ' + (r.agent_provider || 'по умолч.')},
];
return html`
<div class="toolbar">
<input type="search" placeholder="Поиск по email или Telegram" aria-label="Поиск клиента"
value=${query} onInput=${(e) => { setQuery(e.currentTarget.value); setPage(1); }} />
<select aria-label="Статус" value=${status} onChange=${(e) => { setStatus(e.currentTarget.value); setPage(1); }}>
<option value="">Все статусы</option>${Object.entries(STATUS).map(([k, v]) => html`<option key=${k} value=${k}>${v}</option>`)}
</select>
<span class="note" role="status">${data ? 'Всего: ' + data.total + ' · стр. ' + data.page : ''}</span>
<button type="button" disabled=${!data || page <= 1} onClick=${() => setPage(page - 1)}>←</button>
<button type="button" disabled=${!data || page * 50 >= data.total} onClick=${() => setPage(page + 1)}>→</button>
</div>
${error ? html`<div class="card error" role="alert">${error}
<button type="button" onClick=${() => { setError(''); setData(null); setAttempt(a => a + 1); }}>Повторить</button></div>`
: data ? html`<${Table} columns=${columns} rows=${data.items.map((r) => ({...r, key: r.email}))} />`
: html`<div class="card">Загрузка…</div>`}`;
}
+17
View File
@@ -29,3 +29,20 @@ body{margin:0;background:var(--bg);color:var(--ink);font:14px/1.45 -apple-system
.task-actions{display:flex;gap:6px;flex-shrink:0}
.task button{font:inherit;padding:6px 10px;border:1px solid var(--line);border-radius:8px;background:#fff;cursor:pointer}
.error{color:#b42318;border-color:#fda29b}
.toolbar{display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-bottom:12px}
.toolbar input{flex:1;min-width:180px;padding:8px 10px;border:1px solid var(--line);border-radius:8px;font:inherit}
.toolbar select,.toolbar button{font:inherit;padding:8px 10px;border:1px solid var(--line);border-radius:8px;background:#fff}
.toolbar button:disabled{opacity:.45}
.table-wrap{overflow-x:auto;background:var(--card);border:1px solid var(--line);border-radius:12px}
table{width:100%;border-collapse:collapse;min-width:820px}
th,td{padding:9px 10px;text-align:left;white-space:nowrap;border-bottom:1px solid var(--line)}
th{font-size:12px;color:var(--muted);background:#f9fafb;position:sticky;top:0}
tr:last-child td{border-bottom:0}
td.num,th.num{text-align:right;font-variant-numeric:tabular-nums}
.row-client{display:flex;flex-direction:column;min-width:0}
.row-client small{color:var(--muted);font-size:11px}
.badge{padding:2px 8px;border-radius:999px;font-size:11px;background:#eef2f6;color:#475467}
.s-active{background:#e7f6ec;color:#067647}.s-blocked,.s-exhausted{background:#fde8e7;color:#b42318}
.s-trial{background:#fff4e5;color:#b54708}.s-deleted{background:#f2f4f7;color:#667085}
.empty{text-align:center;color:var(--muted);padding:24px}
+14
View File
@@ -0,0 +1,14 @@
import { h } from '/static/admin2/vendor/preact.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
const html = htm.bind(h);
export const Table = ({ columns, rows, empty = 'Никого не нашлось' }) => html`
<div class="table-wrap">
<table>
<thead><tr>${columns.map((c) => html`<th key=${c.key} class=${c.num ? 'num' : ''}>${c.label}</th>`)}</tr></thead>
<tbody>${rows.length ? rows.map((row) => html`
<tr key=${row.key}>${columns.map((c) => html`
<td key=${c.key} class=${c.num ? 'num' : ''}>${c.render ? c.render(row) : row[c.key]}</td>`)}</tr>`)
: html`<tr><td colSpan=${columns.length} class="empty">${empty}</td></tr>`}</tbody>
</table>
</div>`;