26bb5634bd
web-platform-ci / Standalone decision service (no cloud credentials) (push) Waiting to run
web-platform-ci / TypeScript, lint, unit, build (push) Waiting to run
web-platform-ci / Playwright E2E (push) Waiting to run
lekiwi-compatibility / cpu-compatibility (push) Waiting to run
35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
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<WorkbenchNotification[]>([]);
|
|
const [toast, setToast] = useState<WorkbenchNotification>();
|
|
const publishNotice = useCallback((input: Omit<WorkbenchNotification, 'id' | 'at'>) => {
|
|
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 };
|
|
}
|