feat(ui): support notification history and selectable side-panel actions
This commit is contained in:
@@ -7,6 +7,7 @@ export interface ApplicationSidePanelProps extends Omit<HTMLAttributes<HTMLEleme
|
||||
title: ReactNode;
|
||||
description?: ReactNode;
|
||||
closeLabel?: string;
|
||||
headerActions?: ReactNode;
|
||||
footer?: ReactNode;
|
||||
onClose: () => void;
|
||||
children: ReactNode;
|
||||
@@ -23,6 +24,7 @@ export function ApplicationSidePanel({
|
||||
title,
|
||||
description,
|
||||
closeLabel = "Закрыть панель",
|
||||
headerActions,
|
||||
footer,
|
||||
onClose,
|
||||
children,
|
||||
@@ -42,6 +44,7 @@ export function ApplicationSidePanel({
|
||||
{description ? <span className="nodedc-application-side-panel__description">{description}</span> : null}
|
||||
</div>
|
||||
<div className="nodedc-admin-panel__head-actions">
|
||||
{headerActions}
|
||||
<button type="button" className="nodedc-admin-panel__close" aria-label={closeLabel} onClick={onClose}>
|
||||
<Icon name="close" size={16} strokeWidth={1.6} />
|
||||
</button>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef, type HTMLAttributes } from "react";
|
||||
import { createContext, useContext, useEffect, useRef, type HTMLAttributes, type ReactNode } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { cn } from "./cn.js";
|
||||
import { Icon, type IconName } from "./Icon.js";
|
||||
@@ -13,6 +13,13 @@ export type ToastItem = {
|
||||
durationMs?: number | null;
|
||||
};
|
||||
|
||||
const ToastObservation = createContext<((items: ToastItem[]) => void) | null>(null);
|
||||
|
||||
/** The application owns history/storage; this only reports terminal presentations. */
|
||||
export function ToastObserver({onPresent, children}: {onPresent: (items: ToastItem[]) => void; children: ReactNode}) {
|
||||
return <ToastObservation.Provider value={onPresent}>{children}</ToastObservation.Provider>;
|
||||
}
|
||||
|
||||
const toastIcons: Record<ToastTone, IconName> = {
|
||||
success: "check",
|
||||
error: "alert",
|
||||
@@ -69,7 +76,21 @@ function TimedToastCard({ item, onDismiss }: { item: ToastItem; onDismiss: (id:
|
||||
return <ToastCard item={item} onDismiss={onDismiss} />;
|
||||
}
|
||||
|
||||
export function ToastStack({ items, onDismiss }: { items: ToastItem[]; onDismiss: (id: string) => void }) {
|
||||
export function ToastStack({ items, onDismiss, observe = true }: { items: ToastItem[]; onDismiss: (id: string) => void; observe?: boolean }) {
|
||||
const observer = useContext(ToastObservation);
|
||||
const presented = useRef(new Map<string, string>());
|
||||
useEffect(() => {
|
||||
const next = new Map<string, string>();
|
||||
const fresh: ToastItem[] = [];
|
||||
for (const item of items) {
|
||||
if (item.tone === 'loading') continue;
|
||||
const identity = JSON.stringify([item.tone, item.title, item.description]);
|
||||
next.set(item.id, identity);
|
||||
if (presented.current.get(item.id) !== identity) fresh.push(item);
|
||||
}
|
||||
presented.current = next;
|
||||
if (observe && fresh.length) observer?.(fresh);
|
||||
}, [items, observer, observe]);
|
||||
if (typeof document === "undefined" || items.length === 0) return null;
|
||||
return createPortal(
|
||||
<div className="nodedc-toast-viewport nodedc-ui-root" aria-live="polite" aria-relevant="additions removals">
|
||||
|
||||
@@ -24,6 +24,8 @@ export interface UserProfileMenuProps {
|
||||
triggerLabel?: ReactNode;
|
||||
/** Optional status glyph between the label and avatar. */
|
||||
triggerIcon?: IconName;
|
||||
/** Independent header action; the avatar still opens the profile menu. */
|
||||
headerAction?: { label: string; pressed?: boolean; controls?: string; onSelect: () => void };
|
||||
actions: readonly UserProfileMenuAction[];
|
||||
}
|
||||
|
||||
@@ -35,6 +37,7 @@ export function UserProfileMenu({
|
||||
coverImageUrl,
|
||||
triggerLabel,
|
||||
triggerIcon,
|
||||
headerAction,
|
||||
actions,
|
||||
}: UserProfileMenuProps) {
|
||||
const label = displayName || "NODE.DC";
|
||||
@@ -51,11 +54,15 @@ export function UserProfileMenu({
|
||||
</HeaderProfileButton>
|
||||
<IconButton
|
||||
ref={setTriggerRef}
|
||||
label={`Меню пользователя ${label}`}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={open}
|
||||
aria-controls={surfaceId}
|
||||
onClick={(event) => { setTriggerRef(event.currentTarget); toggle(); }}
|
||||
label={headerAction?.label ?? `Меню пользователя ${label}`}
|
||||
aria-haspopup={headerAction ? undefined : "menu"}
|
||||
aria-pressed={headerAction?.pressed}
|
||||
aria-expanded={headerAction ? headerAction.pressed : open}
|
||||
aria-controls={headerAction ? headerAction.controls : surfaceId}
|
||||
onClick={(event) => {
|
||||
if (headerAction) { if (open) toggle(); headerAction.onSelect(); }
|
||||
else { setTriggerRef(event.currentTarget); toggle(); }
|
||||
}}
|
||||
><Icon name={triggerIcon ?? "inbox"} size={20} strokeWidth={1.7} /></IconButton>
|
||||
<button type="button" className="nodedc-user-profile-menu__trigger" aria-label={`Профиль ${label}`} aria-haspopup="menu" aria-expanded={open} aria-controls={surfaceId}
|
||||
onClick={(event) => { setTriggerRef(event.currentTarget); toggle(); }}>
|
||||
|
||||
Reference in New Issue
Block a user