Add document AI gen support

This commit is contained in:
Joey Yakimowich-Payne 2026-01-13 22:48:43 -07:00
commit 028bab23fd
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
11 changed files with 1270 additions and 170 deletions

View file

@ -1,9 +1,11 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import { Quiz, Player, GameState, GameRole, NetworkMessage, AnswerOption, Question } from '../types';
import { Quiz, Player, GameState, GameRole, NetworkMessage, AnswerOption, Question, GenerateQuizOptions, ProcessedDocument } from '../types';
import { generateQuiz } from '../services/geminiService';
import { POINTS_PER_QUESTION, QUESTION_TIME, QUESTION_TIME_MS, PLAYER_COLORS } from '../constants';
import { Peer, DataConnection } from 'peerjs';
const BACKEND_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3001';
export const useGame = () => {
const [role, setRole] = useState<GameRole>('HOST');
const [gameState, setGameState] = useState<GameState>('LANDING');
@ -43,16 +45,50 @@ export const useGame = () => {
// -- HOST LOGIC --
const startQuizGen = async (topic: string) => {
const uploadDocument = async (file: File, useOcr: boolean = false): Promise<ProcessedDocument> => {
const formData = new FormData();
formData.append('document', file);
formData.append('useOcr', String(useOcr));
const response = await fetch(`${BACKEND_URL}/api/upload`, {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Failed to upload document');
}
return response.json();
};
const startQuizGen = async (options: { topic?: string; questionCount?: number; files?: File[]; useOcr?: boolean }) => {
try {
setGameState('GENERATING');
setError(null);
setRole('HOST');
const generatedQuiz = await generateQuiz(topic);
setPendingQuizToSave({ quiz: generatedQuiz, topic });
let documents: ProcessedDocument[] | undefined;
if (options.files && options.files.length > 0) {
documents = await Promise.all(
options.files.map(file => uploadDocument(file, options.useOcr))
);
}
const generateOptions: GenerateQuizOptions = {
topic: options.topic,
questionCount: options.questionCount,
documents
};
const generatedQuiz = await generateQuiz(generateOptions);
const saveLabel = options.topic || options.files?.map(f => f.name).join(', ') || '';
setPendingQuizToSave({ quiz: generatedQuiz, topic: saveLabel });
initializeHostGame(generatedQuiz);
} catch (e) {
setError("Failed to generate quiz.");
const message = e instanceof Error ? e.message : "Failed to generate quiz.";
setError(message);
setGameState('LANDING');
}
};