import { BookOpenIcon, FileTextIcon, SparklesIcon, type LucideIcon, } from 'lucide-react'; import { useEffect, useId, useState } from 'react'; import { FormatItem } from './FormatItem'; import { isLoggedIn } from '../../lib/jwt'; import { showLoginPopup } from '../../lib/popup'; import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal'; import { useIsPaidUser } from '../../queries/billing'; import { clearQuestionAnswerChatMessages, storeQuestionAnswerChatMessages, } from '../../lib/ai-questions'; import { QuestionAnswerChat, type QuestionAnswerChatMessage, } from './QuestionAnswerChat'; import { useToast } from '../../hooks/use-toast'; const allowedFormats = ['course', 'guide', 'roadmap'] as const; export type AllowedFormat = (typeof allowedFormats)[number]; export function ContentGenerator() { const [isUpgradeModalOpen, setIsUpgradeModalOpen] = useState(false); const { isPaidUser, isLoading: isPaidUserLoading } = useIsPaidUser(); const toast = useToast(); const [title, setTitle] = useState(''); const [selectedFormat, setSelectedFormat] = useState('course'); // question answer chat options const [showFineTuneOptions, setShowFineTuneOptions] = useState(false); const [questionAnswerChatMessages, setQuestionAnswerChatMessages] = useState< QuestionAnswerChatMessage[] >([]); const titleFieldId = useId(); const fineTuneOptionsId = useId(); const allowedFormats: { label: string; icon: LucideIcon; value: AllowedFormat; }[] = [ { label: 'Course', icon: BookOpenIcon, value: 'course', }, { label: 'Guide', icon: FileTextIcon, value: 'guide', }, ]; const handleSubmit = () => { if (!isLoggedIn()) { showLoginPopup(); return; } let sessionId = ''; if (showFineTuneOptions) { clearQuestionAnswerChatMessages(); sessionId = storeQuestionAnswerChatMessages(questionAnswerChatMessages); } const trimmedTitle = title.trim(); if (selectedFormat === 'course') { window.location.href = `/ai/course?term=${encodeURIComponent(trimmedTitle)}&id=${sessionId}&format=${selectedFormat}`; } else if (selectedFormat === 'guide') { window.location.href = `/ai/guide?term=${encodeURIComponent(trimmedTitle)}&id=${sessionId}&format=${selectedFormat}`; } }; useEffect(() => { window?.fireEvent({ action: 'tutor_user', category: 'ai_tutor', label: 'Visited AI Course Page', }); }, []); const trimmedTitle = title.trim(); const canGenerate = trimmedTitle && trimmedTitle.length >= 3; return (
{isUpgradeModalOpen && ( setIsUpgradeModalOpen(false)} /> )} {!isPaidUser && !isPaidUserLoading && isLoggedIn() && (
You are on the free plan
)}

What can I help you learn?

Enter a topic below to generate a personalized course for it

{ e.preventDefault(); handleSubmit(); }} >
{ setTitle(e.target.value); setShowFineTuneOptions(false); }} className="block w-full rounded-xl border border-gray-200 bg-white p-4 outline-none placeholder:text-gray-500 focus:border-gray-500" required minLength={3} />
{allowedFormats.map((format) => { const isSelected = format.value === selectedFormat; return ( setSelectedFormat(format.value)} icon={format.icon} isSelected={isSelected} /> ); })}
{showFineTuneOptions && ( { handleSubmit(); }} /> )}
); }