Files
summercms/admin/src/state/useToasts.ts
Jakub Zych 445404e394 feat(10-04): collapsible panel with flyout, user menu with logout, breadcrumbs and dark mode
- useSidebar: collapsed below 1100px (matchMedia) or by the admin's choice,
  persisted as a boolean under summer-admin.sidebar; the viewport never
  overwrites the stored choice
- SectionPanel collapse and rail expand buttons; SectionFlyout (role menu)
  opens on hover, focus, Enter or ArrowDown on a rail item, closes on Esc or
  about 200 ms after leaving and returns focus to the rail item
- Breadcrumbs with plugin, controller and record crumbs; UserMenu (Reka
  DropdownMenu) with initials, name, role and Wyloguj
- useAuth.logout POSTs /auth/logout, clears user, navigation and settings
  and routes to login even when the call fails
- applyColorScheme toggles .dark from prefers-color-scheme (no toggle, A6)
- newest toast first, 200 ms fade and scale for dialogs and toasts
- one relation-manager type constant in the registry
- shell smoke tests; tests default to a desktop, light matchMedia
2026-09-27 17:26:44 +02:00

54 lines
1.3 KiB
TypeScript

// Global toast queue (design: State Management). Toasts outlive route
// changes because AppShell mounts the renderer, so "Zapisz i zamknij" can
// show its toast on the list it returns to. One queue; the most recent toast
// comes first.
import { readonly, ref } from 'vue'
export type ToastTone = 'success' | 'danger'
export interface ToastItem {
id: number
text: string
tone: ToastTone
}
/** Auto-dismiss delay in milliseconds (design: about 4 s). */
export const TOAST_TIMEOUT = 4000
const items = ref<ToastItem[]>([])
const timers = new Map<number, ReturnType<typeof setTimeout>>()
let nextId = 1
export const toasts = readonly(items)
export function showToast(text: string, tone: ToastTone = 'success'): number {
const id = nextId++
items.value = [{ id, text, tone }, ...items.value]
timers.set(
id,
setTimeout(() => dismissToast(id), TOAST_TIMEOUT),
)
return id
}
export function dismissToast(id: number): void {
const timer = timers.get(id)
if (timer !== undefined) {
clearTimeout(timer)
timers.delete(id)
}
items.value = items.value.filter((item) => item.id !== id)
}
export function clearToasts(): void {
for (const timer of timers.values()) {
clearTimeout(timer)
}
timers.clear()
items.value = []
}
export function useToasts() {
return { toasts, showToast, dismissToast, clearToasts }
}