Filter Mission Core events with the shared header switch and acknowledge on close

This commit is contained in:
DCCONSTRUCTIONS
2026-12-05 14:52:01 +03:00
parent 1b67d5f7df
commit 7fb78eeb2a
2 changed files with 22 additions and 8 deletions
@@ -1,5 +1,5 @@
import {useEffect, useRef, useState, type ReactNode} from 'react';
import {ApplicationSidePanel, Button, Description, Icon, IconButton, ResourceList, ResourceRow, StatusBadge, ToastStack} from '@nodedc/ui-react';
import {ApplicationSidePanel, Button, Description, Icon, IconButton, ResourceList, ResourceRow, StatusBadge, Switch, Tooltip, ToastStack} from '@nodedc/ui-react';
import {NotificationContext, useNotificationHistory, useNotifications} from '../../core/notifications/useNotificationHistory';
import type {NotificationTone} from '../../core/notifications/history';
import {EventSettings} from './EventSettings';
@@ -27,15 +27,20 @@ export function EventPanel() {
if (!state.open) return;
gear.current?.focus({preventScroll: true});
}, [state.open]);
return <ApplicationSidePanel id="operator-events" eyebrow="Mission Core" title="Центр событий"
return <ApplicationSidePanel id="operator-events" className="nodedc-event-center" eyebrow="Mission Core" title="Центр событий"
inert={!state.open} aria-label="Центр событий" closeLabel="Закрыть центр событий" onClose={close}
onKeyDown={event => {
if (event.key === 'Escape' && !settings) { event.stopPropagation(); close(); }
}}
headerActions={<IconButton ref={gear} label="Настройки центра событий" onClick={() => setSettings(true)}><Icon name="settings"/></IconButton>}>
headerActions={<><IconButton ref={gear} label="Настройки центра событий" onClick={() => setSettings(true)}><Icon name="settings"/></IconButton>
<Tooltip content={state.onlyNew ? "Показать все" : "Показать только новые"}>
<Switch label="Показывать только новые" checked={state.onlyNew} onChange={state.setOnlyNew}/>
</Tooltip>
</>}>
{state.error && <Description role="status">{state.error}</Description>}
{!state.items.length && <Description>{state.loading ? 'Получаем события…' : 'Событий пока нет.'}</Description>}
<ResourceList aria-label="История событий">{state.items.map(item => <li key={item.identity}>
{state.onlyNew && state.items.length>0 && state.items.every(i=>i.read) && <Description>Новых событий нет.</Description>}
<ResourceList aria-label="История событий">{state.items.filter(item=>!state.onlyNew || !item.read).map(item => <li key={item.identity}>
<ResourceRow title={item.title} description={item.description}
metadata={new Date(item.at * 1000).toLocaleString('ru-RU')}
statusPlacement="leading" status={<StatusBadge variant="indicator" tone={tones[item.tone]}
@@ -32,9 +32,18 @@ export function useNotificationHistory(){
void connect();
return()=>{live=false;stream?.close();clearTimeout(retry);};
},[]);
const maximum=items[0]?.sequence??0;
const [onlyNew,setOnlyNew]=useState(false);
// Capture the journal boundary while visible. New arrivals after close
// must not be acknowledged together with the preceding page.
const shown=useRef(0);
const [maximum,setMaximum]=useState(0);
const latest=items[0]?.sequence??0;
useEffect(()=>{
if(!open||!unread||maximum<=readThrough.current)return;
if(open)shown.current=latest;
else setMaximum(shown.current);
},[open,latest]);
useEffect(()=>{
if(maximum<=readThrough.current)return;
let active=true,timer:ReturnType<typeof setTimeout>|undefined;
async function markRead(){
try{
@@ -44,13 +53,13 @@ export function useNotificationHistory(){
}
void markRead();
return()=>{active=false;clearTimeout(timer);};
},[open,maximum,unread]);
},[maximum]);
const loadMore=async()=>{
if(loading||!items.length)return;setLoading(true);
try{const page=await notificationRequest<NotificationPage>('?before='+items[items.length-1].sequence);setItems(old=>mergeNotifications(old,page.items));setMore(page.items.length===100);setError('');}
catch(e){setError(e instanceof Error?e.message:'История недоступна.');}finally{setLoading(false);}
};
return {items,unread,open,setOpen,error,loading,more,loadMore,toasts,dismiss:(id:string)=>setToasts(old=>old.filter(item=>item.id!==id))};
return {items,unread,open,setOpen,onlyNew,setOnlyNew,error,loading,more,loadMore,toasts,dismiss:(id:string)=>setToasts(old=>old.filter(item=>item.id!==id))};
}
export const NotificationContext=createContext<ReturnType<typeof useNotificationHistory>|null>(null);
export function useNotifications(){const context=useContext(NotificationContext);if(!context)throw new Error('Notification host missing');return context;}