'use client'; import { useState } from 'react'; import type { Meta, StoryObj } from "@storybook/react"; import { getPublicClient } from "../api"; const client = getPublicClient('f5d86791-d513-48de-8fd2-b6dbdd4abe9b'); const QuizGenerator = () => { const [topic, setTopic] = useState(''); const [quizQuestions, setQuizQuestions] = useState([]) as any; const [userAnswers, setUserAnswers] = useState({}) as any; const [quizSubmitted, setQuizSubmitted] = useState(false); const [isLoading, setIsLoading] = useState(false); const [score, setScore] = useState(0); const handleTopicChange = (e: any) => { setTopic(e.target.value); }; const generateQuiz = async () => { setIsLoading(true); try { const response = await client?.getCompletion({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: `Generate a 5-question multiple-choice quiz on the topic of ${topic}. Each question should be structured as follows: a question followed by four options marked A, B, C, D and the correct answer marked as 'Correct answer: X'.` }], }); if (response && response.choices && response.choices.length > 0) { const rawQuestions = response.choices[0].message.content; console.log('Raw questions:', rawQuestions); // Parse the structured text into question objects const questions = parseQuestions(rawQuestions); console.log('Parsed questions:', questions); setQuizQuestions(questions); } } catch (error) { console.error("Error generating quiz:", error); } setIsLoading(false); }; const parseQuestions = (text: string) => { const questionBlocks = text.split('\n\n').map(block => block.trim()); return questionBlocks.map(block => { const lines = block.split('\n'); const question = lines[0]; const options = lines.slice(1, 5); const correctAnswer = lines[5].replace('Correct answer: ', ''); return { question, options, correctAnswer }; }); }; const handleAnswerChange = (questionIndex: any, answer: any) => { setUserAnswers((prev: any) => ({ ...prev, [questionIndex]: answer })); }; const handleSubmitQuiz = () => { let correctCount = 0; console.log('User answers:', userAnswers); quizQuestions.forEach((question: any, index: any) => { if (question.correctAnswer === userAnswers[index]) { correctCount++; } }); setScore(correctCount); setQuizSubmitted(true); }; const restartQuiz = () => { setTopic(''); setQuizQuestions([]); setUserAnswers({}); setQuizSubmitted(false); setScore(0); }; return (
{!quizSubmitted ? ( <> { quizQuestions.length === 0 && } {quizQuestions.map((question: any, index: any) => (
{question.question}
{question.options.map((option: any, idx: any) => ( ))}
))} { quizQuestions.length > 0 && } ) : ( <>
Your Score: {score} / {quizQuestions.length}
)}
); }; function TextCompleterStory() { // client?.getCompletion({ // model: "gpt-3.5-turbo", // messages: [ // { // role: "system", // content: "You are a friendly bot.", // }, // ], // }).then((response) => { // console.log(response); // }).catch((error) => { // console.error(error); // }); // React.useEffect(() => { // const getCompletion = async () => { // client?.getCompletion({ // model: "gpt-3.5-turbo", // messages: [ // { // role: "system", // content: "You are a friendly bot.", // }, // ], // }).then((response) => { // console.log(response); // }); // }; // getCompletion(); // }, [botId]); return (

Quiz Generator

); } //👇 This default export determines where your story goes in the story list const meta: Meta = { component: TextCompleterStory, }; export default meta; export const DefaultStory: StoryObj = { // harded code values for now 😬 args: {}, };