| 1 | import { useCallback } from "react"; |
| 2 | import { app } from "./bridge"; |
| 3 | |
| 4 | export function useGoalControllerActions( |
| 5 | activeTabId: string | undefined, |
| 6 | refreshMetaForTab: (tabId: string) => Promise<unknown>, |
| 7 | ) { |
| 8 | const setGoalForTab = useCallback(async (tabId: string, goal: string): Promise<void> => { |
| 9 | if (!tabId) return; |
| 10 | // Propagate activation failures so a structured first Goal turn aborts |
| 11 | // instead of executing without an active persisted lifecycle. |
| 12 | try { |
| 13 | await app.SetGoalForTab(tabId, goal); |
| 14 | } finally { |
| 15 | await refreshMetaForTab(tabId); |
| 16 | } |
| 17 | }, [refreshMetaForTab]); |
| 18 | |
| 19 | const setGoal = useCallback(async (goal: string): Promise<void> => { |
| 20 | if (activeTabId) await setGoalForTab(activeTabId, goal); |
| 21 | }, [activeTabId, setGoalForTab]); |
| 22 | |
| 23 | const editGoalForTab = useCallback(async (tabId: string, objective: string, maxGoalRounds: number | null): Promise<void> => { |
| 24 | if (!tabId) return; |
| 25 | try { |
| 26 | await app.EditGoalForTab(tabId, objective, maxGoalRounds); |
| 27 | } finally { |
| 28 | await refreshMetaForTab(tabId); |
| 29 | } |
| 30 | }, [refreshMetaForTab]); |
| 31 | |
| 32 | const clearGoalForTab = useCallback(async (tabId: string): Promise<void> => { |
| 33 | if (!tabId) return; |
| 34 | try { |
| 35 | await app.ClearGoalForTab(tabId); |
| 36 | } finally { |
| 37 | await refreshMetaForTab(tabId); |
| 38 | } |
| 39 | }, [refreshMetaForTab]); |
| 40 | |
| 41 | const clearGoal = useCallback(async (): Promise<void> => { |
| 42 | if (activeTabId) await clearGoalForTab(activeTabId); |
| 43 | }, [activeTabId, clearGoalForTab]); |
| 44 | |
| 45 | const resumeGoalForTab = useCallback(async (tabId: string): Promise<boolean> => { |
| 46 | if (!tabId) return false; |
| 47 | try { |
| 48 | const resumed = await app.ResumeGoalForTab(tabId); |
| 49 | await refreshMetaForTab(tabId); |
| 50 | return resumed; |
| 51 | } catch { |
| 52 | return false; |
| 53 | } |
| 54 | }, [refreshMetaForTab]); |
| 55 | |
| 56 | const resumeGoal = useCallback(async (): Promise<boolean> => { |
| 57 | return activeTabId ? resumeGoalForTab(activeTabId) : false; |
| 58 | }, [activeTabId, resumeGoalForTab]); |
| 59 | |
| 60 | const pauseGoalForTab = useCallback(async (tabId: string): Promise<boolean> => { |
| 61 | if (!tabId) return false; |
| 62 | try { |
| 63 | const paused = await app.PauseGoalForTab(tabId); |
| 64 | await refreshMetaForTab(tabId); |
| 65 | return paused; |
| 66 | } catch { |
| 67 | return false; |
| 68 | } |
| 69 | }, [refreshMetaForTab]); |
| 70 | |
| 71 | const pauseGoal = useCallback(async (): Promise<boolean> => { |
| 72 | return activeTabId ? pauseGoalForTab(activeTabId) : false; |
| 73 | }, [activeTabId, pauseGoalForTab]); |
| 74 | |
| 75 | return { setGoalForTab, setGoal, editGoalForTab, clearGoalForTab, clearGoal, resumeGoalForTab, resumeGoal, pauseGoalForTab, pauseGoal }; |
| 76 | } |
| 77 |