import { useCallback, useEffect, useState } from 'react'; import { Box, Button, Text, Flex, VStack, Textarea, Link, Kbd, Spinner, } from '@chakra-ui/react'; const Home = () => { const [input, setInput] = useState(''); const [finishReason, setFinishReason] = useState(''); const [isGenerating, setIsGenerating] = useState(false); const onChange = (event) => { setInput(event.target.value); }; const generateAction = useCallback(async () => { setIsGenerating(true); const response = await fetch('/api/generate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ input }), }); const data = await response.json(); const { choice } = data; setIsGenerating(false); setFinishReason(choice.finish_reason); if (choice.finish_reason === 'stop') { console.log('end of the completion!'); } setInput((prevState) => { return prevState + choice.text; }); }, [input]); useEffect(() => { const keydownHandler = async (event) => { if ((event.metaKey || event.ctrlKey) && event.code === 'Enter') { event.preventDefault(); await generateAction(); } }; window.addEventListener('keydown', keydownHandler); return () => { window.removeEventListener('keydown', keydownHandler); }; }, [generateAction]); return ( ScratchPad Write your first story, movie script, or song in 2 minutes. Type a few sentences and just press ctrl or{' '} cmd +{' '} enter to have your AI writer generate the rest! {isGenerating && ( )} {!isGenerating && finishReason === 'stop' && ( Keep writing to generate more! )}