Add Gateway device inventory panel
This commit is contained in:
@@ -79,6 +79,13 @@ export const api = {
|
||||
body: JSON.stringify({ rules, expectedRulesRevision }),
|
||||
}),
|
||||
},
|
||||
devices: {
|
||||
list: () => request('/api/devices'),
|
||||
update: (id, patch, expectedRevision) => request(`/api/devices/${id}`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ ...patch, expectedRevision }),
|
||||
}),
|
||||
},
|
||||
singbox: {
|
||||
stop: () => request('/api/singbox/stop', { method: 'POST' }),
|
||||
restart: () => request('/api/singbox/restart', { method: 'POST' }),
|
||||
|
||||
@@ -15,6 +15,7 @@ import { formatBytes } from '../utils/format.js';
|
||||
import { instructionBlocks } from '../instructions.js';
|
||||
import { operationBlocked } from '../state/operations.js';
|
||||
import { ConfirmationPopup } from './ConfirmationPopup.jsx';
|
||||
import { DevicesPanel } from './DevicesPanel.jsx';
|
||||
import { ServerPicker } from './ServerPicker.jsx';
|
||||
import { ERROR_DEFINITIONS } from '../../shared/errors.js';
|
||||
import { canAppendRouteRule } from '../../shared/routingRules.js';
|
||||
@@ -640,6 +641,7 @@ export function ClientOverviewPage({
|
||||
const [serversLeaving, setServersLeaving] = useState(false);
|
||||
const [instructionsOpen, setInstructionsOpen] = useState(false);
|
||||
const [localRulesOpen, setLocalRulesOpen] = useState(false);
|
||||
const [devicesOpen, setDevicesOpen] = useState(false);
|
||||
const [localRulesDraft, setLocalRulesDraft] = useState([]);
|
||||
const [localRulesRevision, setLocalRulesRevision] = useState(state?.route?.localRulesRevision || 0);
|
||||
const [confirmingLocalRulesClose, setConfirmingLocalRulesClose] = useState(false);
|
||||
@@ -655,6 +657,9 @@ export function ClientOverviewPage({
|
||||
const localRulesPanelRef = useRef(null);
|
||||
const localRulesToggleRef = useRef(null);
|
||||
const localRulesCloseRef = useRef(null);
|
||||
const devicesPanelRef = useRef(null);
|
||||
const devicesToggleRef = useRef(null);
|
||||
const devicesCloseRef = useRef(null);
|
||||
const localRulesBaselineRef = useRef('[]');
|
||||
const previousHasSubscriptionRef = useRef(hasSubscription);
|
||||
const gatewayAddress = isGateway ? window.location.hostname : '127.0.0.1';
|
||||
@@ -849,6 +854,28 @@ export function ClientOverviewPage({
|
||||
};
|
||||
}, [instructionsOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!devicesOpen) return undefined;
|
||||
const frame = requestAnimationFrame(() => devicesCloseRef.current?.focus());
|
||||
const closeDevices = (event) => {
|
||||
if (event.type === 'keydown' && event.key !== 'Escape') return;
|
||||
if (event.type !== 'keydown' && (
|
||||
devicesPanelRef.current?.contains(event.target) || devicesToggleRef.current?.contains(event.target)
|
||||
)) return;
|
||||
setDevicesOpen(false);
|
||||
};
|
||||
document.addEventListener('pointerdown', closeDevices);
|
||||
document.addEventListener('keydown', closeDevices);
|
||||
return () => {
|
||||
cancelAnimationFrame(frame);
|
||||
document.removeEventListener('pointerdown', closeDevices);
|
||||
document.removeEventListener('keydown', closeDevices);
|
||||
requestAnimationFrame(() => {
|
||||
if (devicesPanelRef.current?.contains(document.activeElement)) devicesToggleRef.current?.focus();
|
||||
});
|
||||
};
|
||||
}, [devicesOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!localRulesOpen) return undefined;
|
||||
const frame = requestAnimationFrame(() => localRulesCloseRef.current?.focus());
|
||||
@@ -993,6 +1020,7 @@ export function ClientOverviewPage({
|
||||
function openLocalRules() {
|
||||
const rules = state?.route?.localRules || [];
|
||||
setInstructionsOpen(false);
|
||||
setDevicesOpen(false);
|
||||
localRulesBaselineRef.current = localRulesSignature(rules);
|
||||
setLocalRulesDraft(rules.map(createLocalRuleDraft));
|
||||
setLocalRulesRevision(state?.route?.localRulesRevision || 0);
|
||||
@@ -1078,6 +1106,7 @@ export function ClientOverviewPage({
|
||||
aria-label={instructionsOpen ? 'Закрыть инструкции' : 'Как использовать'}
|
||||
onClick={() => {
|
||||
if (localRulesOpen && !requestCloseLocalRules()) return;
|
||||
setDevicesOpen(false);
|
||||
setInstructionsOpen((open) => !open);
|
||||
}}
|
||||
>
|
||||
@@ -1087,6 +1116,26 @@ export function ClientOverviewPage({
|
||||
</svg>
|
||||
<span>Как использовать</span>
|
||||
</button>
|
||||
{isGateway && <button
|
||||
ref={devicesToggleRef}
|
||||
className={`client-instructions-toggle client-devices-toggle${devicesOpen ? ' is-open' : ''}`}
|
||||
type="button"
|
||||
aria-expanded={devicesOpen}
|
||||
aria-controls="client-devices"
|
||||
aria-label={devicesOpen ? 'Закрыть устройства' : 'Устройства Gateway'}
|
||||
onClick={() => {
|
||||
if (localRulesOpen && !requestCloseLocalRules()) return;
|
||||
setInstructionsOpen(false);
|
||||
setDevicesOpen((open) => !open);
|
||||
}}
|
||||
>
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<rect x="3.5" y="5" width="7" height="10" rx="1.5" />
|
||||
<rect x="13.5" y="8" width="7" height="7" rx="1.5" />
|
||||
<path d="M6 19h12M7 15v4M17 15v4" />
|
||||
</svg>
|
||||
<span>Устройства</span>
|
||||
</button>}
|
||||
<button
|
||||
ref={localRulesToggleRef}
|
||||
className={`client-local-rules-toggle${localRulesOpen ? ' is-open' : ''}${localRulesPendingRestart ? ' has-pending' : ''}`}
|
||||
@@ -1429,6 +1478,13 @@ export function ClientOverviewPage({
|
||||
</div>
|
||||
</aside>}
|
||||
|
||||
{hasSubscription && subscriptionContentReady && isGateway && <DevicesPanel
|
||||
open={devicesOpen}
|
||||
panelRef={devicesPanelRef}
|
||||
closeRef={devicesCloseRef}
|
||||
onClose={() => setDevicesOpen(false)}
|
||||
/>}
|
||||
|
||||
{hasSubscription && subscriptionContentReady && <LocalRulesPanel
|
||||
open={localRulesOpen}
|
||||
rules={localRulesDraft}
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { api } from '../api.js';
|
||||
|
||||
const STATUS_LABELS = {
|
||||
online: 'В сети',
|
||||
recent: 'Недавно',
|
||||
offline: 'Не в сети',
|
||||
};
|
||||
|
||||
const CONFIDENCE_LABELS = {
|
||||
high: 'точная MAC',
|
||||
medium: 'частная MAC',
|
||||
low: 'приблизительно',
|
||||
};
|
||||
|
||||
function seenAt(value) {
|
||||
if (!value) return 'нет данных';
|
||||
return new Date(value).toLocaleString('ru-RU', {
|
||||
day: '2-digit',
|
||||
month: 'short',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
}
|
||||
|
||||
export function DevicesPanel({ open, panelRef, closeRef, onClose }) {
|
||||
const [snapshot, setSnapshot] = useState(null);
|
||||
const [status, setStatus] = useState('idle');
|
||||
const [error, setError] = useState(null);
|
||||
const [editingId, setEditingId] = useState('');
|
||||
const [alias, setAlias] = useState('');
|
||||
const [savingId, setSavingId] = useState('');
|
||||
|
||||
async function load(quiet = false) {
|
||||
if (!quiet) setStatus(snapshot ? 'refreshing' : 'loading');
|
||||
try {
|
||||
const next = await api.devices.list();
|
||||
setSnapshot((current) => !current || next.revision >= current.revision ? next : current);
|
||||
setError(null);
|
||||
setStatus('ready');
|
||||
} catch (requestError) {
|
||||
setError(requestError);
|
||||
setStatus('error');
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return undefined;
|
||||
load();
|
||||
const timer = setInterval(() => load(true), 15_000);
|
||||
return () => clearInterval(timer);
|
||||
}, [open]);
|
||||
|
||||
async function updateDevice(device, patch) {
|
||||
setSavingId(device.id);
|
||||
try {
|
||||
const next = await api.devices.update(device.id, patch, snapshot.revision);
|
||||
setSnapshot((current) => !current || next.revision >= current.revision ? next : current);
|
||||
setError(null);
|
||||
return true;
|
||||
} catch (requestError) {
|
||||
if (requestError.code === 'STATE_CONFLICT') await load(true);
|
||||
setError(requestError);
|
||||
return false;
|
||||
} finally {
|
||||
setSavingId('');
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAlias(event, device) {
|
||||
event.preventDefault();
|
||||
if (!await updateDevice(device, { alias })) return;
|
||||
setEditingId('');
|
||||
}
|
||||
|
||||
const devices = snapshot?.devices || [];
|
||||
return (
|
||||
<aside
|
||||
ref={panelRef}
|
||||
id="client-devices"
|
||||
className={`client-instructions client-devices${open ? ' is-open' : ''}`}
|
||||
aria-labelledby="client-devices-title"
|
||||
aria-hidden={!open}
|
||||
inert={!open ? true : undefined}
|
||||
>
|
||||
<div className="client-instructions-sheet client-devices-sheet">
|
||||
<button
|
||||
ref={closeRef}
|
||||
className="client-drawer-close"
|
||||
type="button"
|
||||
aria-label="Закрыть устройства"
|
||||
onClick={onClose}
|
||||
>×</button>
|
||||
<header className="client-instructions-header client-devices-header">
|
||||
<span>Gateway · {devices.length}</span>
|
||||
<h2 id="client-devices-title">Устройства</h2>
|
||||
<div className="client-instructions-intro">
|
||||
<p>Устройства, которые Gateway видит в локальной таблице соседей.</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{snapshot?.source?.error && (
|
||||
<p className="client-devices-source" role="status">
|
||||
Источник временно недоступен. Показаны последние сохранённые данные.
|
||||
</p>
|
||||
)}
|
||||
{error && (
|
||||
<div className="client-devices-error" role="alert">
|
||||
<span>{error.message}</span>
|
||||
<button type="button" onClick={() => load()}>Повторить</button>
|
||||
</div>
|
||||
)}
|
||||
{status === 'loading' && <p className="client-devices-empty" role="status">Ищем устройства…</p>}
|
||||
{status !== 'loading' && !devices.length && !error && (
|
||||
<p className="client-devices-empty">Gateway пока не видит устройств.</p>
|
||||
)}
|
||||
|
||||
<div className="client-devices-list" aria-live="polite" aria-busy={status === 'loading' || status === 'refreshing'}>
|
||||
{devices.map((device) => {
|
||||
const title = device.alias || device.hostname || device.manufacturer || device.ip;
|
||||
const editing = editingId === device.id;
|
||||
const saving = savingId === device.id;
|
||||
return <article className={`client-device is-${device.status}`} key={device.id}>
|
||||
<div className="client-device-heading">
|
||||
<div>
|
||||
<span className="client-device-status">{STATUS_LABELS[device.status]}</span>
|
||||
<h3>{title}</h3>
|
||||
{device.manufacturer && device.manufacturer !== title && <p>{device.manufacturer}</p>}
|
||||
</div>
|
||||
<button
|
||||
className="client-device-pin"
|
||||
type="button"
|
||||
aria-pressed={device.pinned}
|
||||
aria-label={device.pinned ? `Открепить ${title}` : `Закрепить ${title}`}
|
||||
disabled={saving}
|
||||
onClick={() => updateDevice(device, { pinned: !device.pinned })}
|
||||
>{device.pinned ? '◆' : '◇'}</button>
|
||||
</div>
|
||||
|
||||
<dl className="client-device-meta">
|
||||
<div><dt>IP</dt><dd>{device.ip || '—'}</dd></div>
|
||||
<div><dt>MAC</dt><dd>{device.mac || '—'}</dd></div>
|
||||
<div><dt>Интерфейс</dt><dd>{device.interface || '—'}</dd></div>
|
||||
<div><dt>Последний раз</dt><dd><time dateTime={device.lastSeenAt}>{seenAt(device.lastSeenAt)}</time></dd></div>
|
||||
<div><dt>Источник</dt><dd>{device.source} · {CONFIDENCE_LABELS[device.confidence]}</dd></div>
|
||||
</dl>
|
||||
|
||||
{editing ? (
|
||||
<form className="client-device-alias" onSubmit={(event) => saveAlias(event, device)}>
|
||||
<label>
|
||||
<span>Название</span>
|
||||
<input value={alias} maxLength="64" autoFocus onChange={(event) => setAlias(event.target.value)} />
|
||||
</label>
|
||||
<button type="submit" disabled={saving}>Сохранить</button>
|
||||
<button type="button" disabled={saving} onClick={() => setEditingId('')}>Отмена</button>
|
||||
</form>
|
||||
) : (
|
||||
<button
|
||||
className="client-device-rename"
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setEditingId(device.id);
|
||||
setAlias(device.alias || '');
|
||||
}}
|
||||
>Изменить название</button>
|
||||
)}
|
||||
</article>;
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -715,6 +715,187 @@ p {
|
||||
transition: opacity 350ms ease, filter 350ms ease, transform 500ms cubic-bezier(0.16, 1, 0.3, 1);
|
||||
}
|
||||
|
||||
.client-devices {
|
||||
width: min(560px, 100vw);
|
||||
}
|
||||
|
||||
.client-devices-header {
|
||||
margin-bottom: 28px;
|
||||
}
|
||||
|
||||
.client-devices-source,
|
||||
.client-devices-error,
|
||||
.client-devices-empty {
|
||||
margin: 0 8px 20px;
|
||||
color: var(--client-muted);
|
||||
font-size: 10px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.client-devices-source {
|
||||
color: oklch(0.62 0.1 72);
|
||||
}
|
||||
|
||||
.client-devices-error {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
color: oklch(0.62 0.13 28);
|
||||
}
|
||||
|
||||
.client-devices-error button,
|
||||
.client-device-pin,
|
||||
.client-device-rename,
|
||||
.client-device-alias button {
|
||||
padding: 0;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--client-accent);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.client-devices-list {
|
||||
display: grid;
|
||||
}
|
||||
|
||||
.client-device {
|
||||
display: grid;
|
||||
gap: 14px;
|
||||
padding: 20px 8px;
|
||||
border-top: 1px solid color-mix(in oklch, var(--client-border) 72%, transparent);
|
||||
}
|
||||
|
||||
.client-device-heading {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 32px;
|
||||
gap: 12px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.client-device-heading > div {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.client-device-heading h3 {
|
||||
overflow: hidden;
|
||||
margin: 4px 0 0;
|
||||
font-size: 15px;
|
||||
letter-spacing: -0.03em;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.client-device-heading p,
|
||||
.client-device-status {
|
||||
color: var(--client-muted);
|
||||
font-size: 9px;
|
||||
}
|
||||
|
||||
.client-device-status::before {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
display: inline-block;
|
||||
margin-right: 7px;
|
||||
border-radius: 50%;
|
||||
background: currentColor;
|
||||
content: '';
|
||||
}
|
||||
|
||||
.client-device.is-online .client-device-status {
|
||||
color: var(--client-accent);
|
||||
}
|
||||
|
||||
.client-device.is-offline .client-device-status {
|
||||
opacity: 0.56;
|
||||
}
|
||||
|
||||
.client-device-pin {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
color: var(--client-muted);
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.client-device-pin[aria-pressed="true"] {
|
||||
color: var(--client-accent);
|
||||
}
|
||||
|
||||
.client-device-meta {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 9px 18px;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.client-device-meta div {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.client-device-meta dt {
|
||||
color: var(--client-muted);
|
||||
font-size: 8px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.07em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.client-device-meta dd {
|
||||
overflow: hidden;
|
||||
margin: 3px 0 0;
|
||||
font-size: 9px;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.client-device-rename,
|
||||
.client-device-alias button,
|
||||
.client-devices-error button {
|
||||
justify-self: start;
|
||||
font-size: 9px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.client-device-alias {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto auto;
|
||||
align-items: end;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.client-device-alias label {
|
||||
display: grid;
|
||||
gap: 5px;
|
||||
color: var(--client-muted);
|
||||
font-size: 8px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.client-device-alias input {
|
||||
min-width: 0;
|
||||
padding: 8px 0;
|
||||
border: 0;
|
||||
border-bottom: 1px solid var(--client-border);
|
||||
outline: 0;
|
||||
background: transparent;
|
||||
color: var(--client-text);
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.client-device-alias input:focus {
|
||||
border-color: var(--client-accent);
|
||||
}
|
||||
|
||||
.client-devices button:focus-visible {
|
||||
outline: 2px solid var(--client-accent);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.client-devices button:disabled {
|
||||
cursor: default;
|
||||
opacity: 0.35;
|
||||
}
|
||||
|
||||
.client-instructions-toggle:hover,
|
||||
.client-instructions-toggle:focus-visible,
|
||||
.client-local-rules-toggle:hover,
|
||||
@@ -3556,6 +3737,10 @@ p {
|
||||
padding: 40px 58px 60px 18px;
|
||||
}
|
||||
|
||||
.client-device-meta {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.client-local-rules-sheet {
|
||||
padding: 40px 58px 60px 18px;
|
||||
}
|
||||
@@ -3654,6 +3839,9 @@ p {
|
||||
.client-instruction-reveal,
|
||||
.client-instruction-summary > i::before,
|
||||
.client-instruction-summary > i::after,
|
||||
.client-device,
|
||||
.client-device-pin,
|
||||
.client-device-rename,
|
||||
.client-subscription-edit,
|
||||
.client-subscription-edit::after,
|
||||
.client-subscription-submit,
|
||||
|
||||
Reference in New Issue
Block a user