diff options
| author | soryu <soryu@soryu.co> | 2026-01-18 02:58:27 +0000 |
|---|---|---|
| committer | soryu <soryu@soryu.co> | 2026-01-18 02:58:27 +0000 |
| commit | fcf9e70d54bd737d2dea848d25314120f37db503 (patch) | |
| tree | bc304a9e153485f7686830614b2ddae4d4ff182e /apps/mobile/hooks/useQuestions.ts | |
| parent | f84a7f2d820f6f432be2b1d78d6bf833b5b19380 (diff) | |
| download | soryu-fcf9e70d54bd737d2dea848d25314120f37db503.tar.gz soryu-fcf9e70d54bd737d2dea848d25314120f37db503.zip | |
[WIP] Heartbeat checkpoint - 2026-01-18 02:58:27 UTC
Diffstat (limited to 'apps/mobile/hooks/useQuestions.ts')
| -rw-r--r-- | apps/mobile/hooks/useQuestions.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/apps/mobile/hooks/useQuestions.ts b/apps/mobile/hooks/useQuestions.ts new file mode 100644 index 0000000..af77a51 --- /dev/null +++ b/apps/mobile/hooks/useQuestions.ts @@ -0,0 +1,44 @@ +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; +import { listPendingQuestions, answerQuestion, type PendingQuestion } from '../lib/api'; + +// Query keys for questions +export const questionKeys = { + all: ['questions'] as const, + lists: () => [...questionKeys.all, 'list'] as const, + list: () => [...questionKeys.lists()] as const, +}; + +/** + * Hook to fetch pending questions + * Polls every 5 seconds for updates + */ +export function usePendingQuestions() { + return useQuery({ + queryKey: questionKeys.list(), + queryFn: listPendingQuestions, + refetchInterval: 5000, + }); +} + +/** + * Hook to answer a pending question + */ +export function useAnswerQuestion() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ questionId, response }: { questionId: string; response: string }) => + answerQuestion(questionId, response), + onSuccess: () => { + // Invalidate questions list to refetch + queryClient.invalidateQueries({ queryKey: questionKeys.lists() }); + }, + }); +} + +/** + * Get the count of pending questions + */ +export function getQuestionCount(questions: PendingQuestion[] | undefined): number { + return questions?.length ?? 0; +} |
