1.AI 协作文档体系重构(objections/worklines/contracts+matrix.md) 2.coord 仲裁文档(final-decisions/cross-review/final-rulings/orchestration) 3.各服务 01/02 文档补全 4.共享包初始化(shared-ts/shared-go/hooks/ui-components/ui-tokens) 5.Proto 契约补全 6.004 架构影响地图更新 7.端口分配表 8.设计规格文档
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { useCallback, useState } from "react";
|
|
import type { ToastMessage } from "./types.js";
|
|
|
|
/**
|
|
* useToast - 全局 Toast 通知管理
|
|
*
|
|
* 用途:成功/错误/警告/信息提示,自动关闭。
|
|
* 与 Zustand ui-store 解耦,提供 hook 层 API。
|
|
*
|
|
* @example
|
|
* const { toasts, showToast, dismissToast } = useToast();
|
|
* showToast({ type: "success", title: "保存成功" });
|
|
*/
|
|
|
|
export interface UseToastReturn {
|
|
/** 当前活跃的 toast 列表 */
|
|
toasts: ToastMessage[];
|
|
/** 展示 toast */
|
|
showToast: (toast: Omit<ToastMessage, "id">) => string;
|
|
/** 关闭指定 toast */
|
|
dismissToast: (id: string) => void;
|
|
/** 关闭全部 toast */
|
|
dismissAll: () => void;
|
|
}
|
|
|
|
export function useToast(): UseToastReturn {
|
|
const [toasts, setToasts] = useState<ToastMessage[]>([]);
|
|
|
|
const dismissToast = useCallback((id: string): void => {
|
|
setToasts((prev) => prev.filter((t) => t.id !== id));
|
|
}, []);
|
|
|
|
const showToast = useCallback(
|
|
(toast: Omit<ToastMessage, "id">): string => {
|
|
const id = `toast-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
const fullToast: ToastMessage = {
|
|
id,
|
|
duration: 4000,
|
|
...toast,
|
|
};
|
|
|
|
setToasts((prev) => [...prev, fullToast]);
|
|
|
|
// 自动关闭
|
|
if (fullToast.duration && fullToast.duration > 0) {
|
|
setTimeout(() => {
|
|
dismissToast(id);
|
|
}, fullToast.duration);
|
|
}
|
|
|
|
return id;
|
|
},
|
|
[dismissToast],
|
|
);
|
|
|
|
const dismissAll = useCallback((): void => {
|
|
setToasts([]);
|
|
}, []);
|
|
|
|
return {
|
|
toasts,
|
|
showToast,
|
|
dismissToast,
|
|
dismissAll,
|
|
};
|
|
}
|