summaryrefslogblamecommitdiff
path: root/apps/mobile/hooks/useQuestions.ts
blob: af77a51c18ec937e5f2bf3076045eb35165d878e (plain) (tree)











































                                                                                        
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;
}