1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}
|