返回 DeepSeek-Reasonix
goalAction.ts
根目录 / desktop / frontend / src / lib / goalAction.ts
1 import { useCallback } from "react";
2 import { useToast } from "./toast";
3
4 export type GoalAction = () => void | Promise<void>;
5
6 /**
7 * Run a Goal-related UI/background action without leaking a rejected bridge
8 * Promise to the global crash handler. Submission paths that must fail closed
9 * should await the underlying action directly instead.
10 */
11 export function runGoalAction(action: GoalAction, onError: (error: unknown) => void): void {
12 try {
13 void Promise.resolve(action()).catch(onError);
14 } catch (error) {
15 onError(error);
16 }
17 }
18
19 export function useGoalActionHandler() {
20 const { showToast } = useToast();
21 const handleGoalActionError = useCallback((error: unknown) => {
22 showToast(error instanceof Error ? error.message : String(error), "error");
23 }, [showToast]);
24 const runHandledGoalAction = useCallback((action: GoalAction) => {
25 runGoalAction(action, handleGoalActionError);
26 }, [handleGoalActionError]);
27 return { runGoalAction: runHandledGoalAction, handleGoalActionError };
28 }
29
29 lines TYPESCRIPT