/*eslint-disable*/ 'use client'; import MessageBox from '@/components/MessageBox'; import Card from '@/components/card/Card'; import DashboardLayout from '@/components/layout'; import { OpenAIModel, PremiumEssayBody } from '@/types/types'; import { Database } from '@/types_db'; import { Button, Flex, FormLabel, Select, Switch, Text, Textarea, useColorModeValue, useToast } from '@chakra-ui/react'; import { User } from '@supabase/supabase-js'; import { useState } from 'react'; type Subscription = Database['public']['Tables']['subscriptions']['Row']; type Product = Database['public']['Tables']['products']['Row']; type Price = Database['public']['Tables']['prices']['Row']; interface ProductWithPrices extends Product { prices: Price[]; } interface PriceWithProduct extends Price { products: Product | null; } interface SubscriptionWithProduct extends Subscription { prices: PriceWithProduct | null; } interface Props { user: User | null | undefined; products: ProductWithPrices[]; subscription: SubscriptionWithProduct | null; userDetails: { [x: string]: any } | null; } export default function PremiumEssayGenerator(props: Props) { // Input States const [words, setWords] = useState('200-300'); const [essayType, setEssayType] = useState< | '' | 'Argumentative' | 'Classic' | 'Persuasive' | 'Memoir' | 'Critique' | 'Compare/Contrast' | 'Narrative' | 'Descriptive' | 'Expository' | 'Cause and Effect' | 'Reflective' | 'Informative' >(''); const [topic, setTopic] = useState(''); const [tone, setTone] = useState(''); const [citation, setCitation] = useState(''); const [citations, setCitations] = useState(false); const [level, setLevel] = useState(''); // Response message const [outputCode, setOutputCode] = useState(''); // ChatGPT model const [model, setModel] = useState('gpt-3.5-turbo'); // Loading state const [loading, setLoading] = useState(false); // API Key // const [apiKey, setApiKey] = useState(); const textColor = useColorModeValue('#120F43', 'white'); const placeholderColor = useColorModeValue( { color: 'gray.500' }, { color: 'whiteAlpha.600' } ); const borderColor = useColorModeValue('gray.200', 'whiteAlpha.200'); const toast = useToast(); // -------------- Main API Handler -------------- const handleTranslate = async () => { const maxCodeLength = model === 'gpt-3.5-turbo' ? 700 : 700; // Chat post conditions(maximum number of characters, valid message etc.) if (!topic) { alert('Please enter your subject.'); return; } if (!essayType) { alert('Please choose a type of essay'); return; } if (!tone) { alert('Please choose a essay tone'); return; } if (!citation) { alert('Please choose a citation format'); return; } if (!level) { alert('Please choose an level'); return; } if (topic.length > maxCodeLength) { alert( `Please enter a topic less than ${maxCodeLength} characters. You are currently at ${topic.length} characters.` ); return; } setLoading(true); setOutputCode(''); const controller = new AbortController(); const body: PremiumEssayBody = { words, topic, essayType, tone, citation, level, citations, model }; // -------------- Fetch -------------- const response = await fetch('/api/premiumEssayAPI', { method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: controller.signal, body: JSON.stringify(body) }); if (!response.ok) { setLoading(false); if (response) { alert( 'Something went wrong went fetching from the API. Make sure to use a valid API key.' ); } return; } const data = response.body; if (!data) { setLoading(false); alert('Something went wrong'); return; } const reader = data.getReader(); const decoder = new TextDecoder(); let done = false; let code = ''; while (!done) { const { value, done: doneReading } = await reader.read(); done = doneReading; const chunkValue = decoder.decode(value); code += chunkValue; setOutputCode((prevCode) => prevCode + chunkValue); } setLoading(false); copyToClipboard(code); }; // -------------- Copy Response -------------- const copyToClipboard = (text: string) => { const el = document.createElement('textarea'); el.value = text; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el); }; // *** Initializing apiKey with .env.local value // useEffect(() => { // ENV file verison // const apiKeyENV = process.env.NEXT_PUBLIC_OPENAI_API_KEY; // if (apiKey === undefined || null) { // setApiKey(apiKeyENV); // } // }, []); // -------------- Input Value Handler -------------- const handleChangeWords = (Event: any) => { setWords(Event.target.value); }; const handleChange = (Event: any) => { setTopic(Event.target.value); }; const handleChangeEssayType = (Event: any) => { setEssayType(Event.target.value); }; const handleChangeEssayTone = (Event: any) => { setTone(Event.target.value); }; const handleChangeCitation = (Event: any) => { setCitation(Event.target.value); }; const handleChangeLevel = (Event: any) => { setLevel(Event.target.value); }; const handleCitations = (Event: any) => { setCitations(!citations); }; return ( <> Essay Topic What your premium essay will be about?