"use client"; import { useEffect, useRef, useState } from "react"; import { Button } from "../ui/button"; import { Check, Loader2, RotateCw, Sparkles, X } from "lucide-react"; import { Socket } from "socket.io-client"; import { Editor } from "@monaco-editor/react"; import { User } from "@/lib/types"; import { toast } from "sonner"; import { usePathname, useRouter } from "next/navigation"; // import monaco from "monaco-editor" export default function GenerateInput({ user, socket, width, data, editor, onExpand, onAccept, onClose, }: { user: User; socket: Socket; width: number; data: { fileName: string; code: string; line: number; }; editor: { language: string; }; onExpand: () => void; onAccept: (code: string) => void; onClose: () => void; }) { const pathname = usePathname(); const router = useRouter(); const inputRef = useRef(null); const [code, setCode] = useState(""); const [expanded, setExpanded] = useState(false); const [loading, setLoading] = useState({ generate: false, regenerate: false, }); const [input, setInput] = useState(""); const [currentPrompt, setCurrentPrompt] = useState(""); useEffect(() => { setTimeout(() => { inputRef.current?.focus(); }, 100); }, [inputRef.current]); const handleGenerate = async ({ regenerate = false, }: { regenerate?: boolean; }) => { if (user.generations >= 10) { toast.error( "You reached the maximum # of generations. Contact @ishaandey_ on X/Twitter to reset :)" ); return; } setLoading({ generate: !regenerate, regenerate }); setCurrentPrompt(input); socket.emit( "generateCode", data.fileName, data.code, data.line, regenerate ? currentPrompt : input, (res: { response: string; success: boolean }) => { console.log("Generated code", res.response, res.success); // if (!res.success) { // toast.error("Failed to generate code."); // return; // } setCode(res.response); router.refresh(); } ); }; useEffect(() => { if (code) { setExpanded(true); onExpand(); setLoading({ generate: false, regenerate: false }); } }, [code]); return (
setInput(e.target.value)} placeholder="✨ Generate code with a prompt" className="h-8 w-full rounded-md border border-muted-foreground bg-transparent px-3 py-1 text-sm shadow-sm transition-all file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50" />
{expanded ? ( <>
) : null}
); }