mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
section 8: Phase 8: Goal mode in the composer, goal status bar and goal
This commit is contained in:
1 parent
4e9f27ab0b
commit
8cd00e1a8a
19 files changed
+877
-11
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import {
|
||||
cancelSessionGoal,
|
||||
getLatestSessionGoal,
|
||||
pauseSessionGoal,
|
||||
resumeSessionGoal,
|
||||
startSessionGoal,
|
||||
} from '@/api/sessionGoals'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import type { SessionGoal, StartSessionGoalRequest } from '@opencode-manager/shared/schemas'
|
||||
|
||||
export function sessionGoalQueryKey(sessionId: string) {
|
||||
return ['session-goal', sessionId] as const
|
||||
}
|
||||
|
||||
export function useSessionGoal(sessionId: string) {
|
||||
return useQuery({
|
||||
queryKey: sessionGoalQueryKey(sessionId),
|
||||
queryFn: () => getLatestSessionGoal(sessionId),
|
||||
refetchInterval: (query) => {
|
||||
const status = query.state.data?.status
|
||||
return status === 'active' || status === 'paused' ? 3000 : false
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function useGoalMutation<TInput>(
|
||||
mutationFn: (input: TInput) => Promise<SessionGoal>,
|
||||
errorMessage: string,
|
||||
) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn,
|
||||
onSuccess: (goal: SessionGoal) => {
|
||||
queryClient.setQueryData(sessionGoalQueryKey(goal.sessionId), goal)
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
showToast.error(error instanceof Error ? error.message : errorMessage)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useStartSessionGoal() {
|
||||
return useGoalMutation((input: StartSessionGoalRequest) => startSessionGoal(input), 'Failed to start goal')
|
||||
}
|
||||
|
||||
export function usePauseSessionGoal() {
|
||||
return useGoalMutation(pauseSessionGoal, 'Failed to pause goal')
|
||||
}
|
||||
|
||||
export function useResumeSessionGoal() {
|
||||
return useGoalMutation(resumeSessionGoal, 'Failed to resume goal')
|
||||
}
|
||||
|
||||
export function useCancelSessionGoal() {
|
||||
return useGoalMutation(cancelSessionGoal, 'Failed to cancel goal')
|
||||
}
|
||||
Reference in new issue
Block a user