import { useCallback, useRef, useState } from 'react'; import type { AppDiagnostic } from '../../stores/useAppStore'; import type { WorkbenchNotification } from '../components/NotificationCenter'; export function diagnostic( category: AppDiagnostic['category'], error: unknown, path?: string, ): AppDiagnostic { const detail = error instanceof Error ? error.message : String(error); return { category, summary: `${category}失败`, detail, path, at: Date.now() }; } /** 通知状态唯一所有者;发布函数稳定,供加载事务与用户动作共享。 */ export function useWorkbenchFeedback() { const notificationId = useRef(0); const [notifications, setNotifications] = useState([]); const [toast, setToast] = useState(); const publishNotice = useCallback((input: Omit) => { const notice: WorkbenchNotification = { id: ++notificationId.current, ...input, at: Date.now(), }; setNotifications((items) => [notice, ...items].slice(0, 20)); setToast(notice); }, []); const notify = useCallback( (title: string, detail: string, tone: WorkbenchNotification['tone'] = 'success') => publishNotice({ title, detail, tone }), [publishNotice], ); return { notifications, setNotifications, toast, setToast, publishNotice, notify }; }