2024-05-12 22:06:11 -07:00
|
|
|
"use client";
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-12 22:06:11 -07:00
|
|
|
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";
|
2024-05-03 00:52:01 -07:00
|
|
|
// import monaco from "monaco-editor"
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
export default function GenerateInput({
|
2024-05-05 16:51:30 -07:00
|
|
|
user,
|
2024-05-03 00:52:01 -07:00
|
|
|
socket,
|
2024-05-02 17:38:37 -07:00
|
|
|
width,
|
2024-05-03 00:52:01 -07:00
|
|
|
data,
|
|
|
|
editor,
|
2024-05-02 17:38:37 -07:00
|
|
|
onExpand,
|
2024-05-02 18:05:18 -07:00
|
|
|
onAccept,
|
2024-05-02 17:38:37 -07:00
|
|
|
}: {
|
2024-05-12 22:06:11 -07:00
|
|
|
user: User;
|
|
|
|
socket: Socket;
|
|
|
|
width: number;
|
2024-05-03 00:52:01 -07:00
|
|
|
data: {
|
2024-05-12 22:06:11 -07:00
|
|
|
fileName: string;
|
|
|
|
code: string;
|
|
|
|
line: number;
|
|
|
|
};
|
2024-05-03 00:52:01 -07:00
|
|
|
editor: {
|
2024-05-12 22:06:11 -07:00
|
|
|
language: string;
|
|
|
|
};
|
|
|
|
onExpand: () => void;
|
|
|
|
onAccept: (code: string) => void;
|
2024-05-02 17:38:37 -07:00
|
|
|
}) {
|
2024-05-12 22:06:11 -07:00
|
|
|
const pathname = usePathname();
|
|
|
|
const router = useRouter();
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-12 22:06:11 -07:00
|
|
|
const [code, setCode] = useState("");
|
|
|
|
const [expanded, setExpanded] = useState(false);
|
2024-05-03 00:52:01 -07:00
|
|
|
const [loading, setLoading] = useState({
|
|
|
|
generate: false,
|
|
|
|
regenerate: false,
|
2024-05-12 22:06:11 -07:00
|
|
|
});
|
|
|
|
const [input, setInput] = useState("");
|
|
|
|
const [currentPrompt, setCurrentPrompt] = useState("");
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-02 18:05:18 -07:00
|
|
|
setTimeout(() => {
|
2024-05-12 22:06:11 -07:00
|
|
|
inputRef.current?.focus();
|
2024-05-13 23:22:06 -07:00
|
|
|
}, 100);
|
|
|
|
}, [inputRef.current]);
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-03 00:52:01 -07:00
|
|
|
const handleGenerate = async ({
|
|
|
|
regenerate = false,
|
|
|
|
}: {
|
2024-05-12 22:06:11 -07:00
|
|
|
regenerate?: boolean;
|
2024-05-03 00:52:01 -07:00
|
|
|
}) => {
|
2024-05-05 16:51:30 -07:00
|
|
|
if (user.generations >= 30) {
|
|
|
|
toast.error(
|
|
|
|
"You reached the maximum # of generations. Contact @ishaandey_ on X/Twitter to reset :)"
|
2024-05-12 22:06:11 -07:00
|
|
|
);
|
2024-05-05 16:51:30 -07:00
|
|
|
}
|
|
|
|
|
2024-05-12 22:06:11 -07:00
|
|
|
setLoading({ generate: !regenerate, regenerate });
|
|
|
|
setCurrentPrompt(input);
|
2024-05-03 00:52:01 -07:00
|
|
|
socket.emit(
|
|
|
|
"generateCode",
|
|
|
|
data.fileName,
|
|
|
|
data.code,
|
|
|
|
data.line,
|
|
|
|
regenerate ? currentPrompt : input,
|
|
|
|
(res: {
|
|
|
|
result: {
|
2024-05-12 22:06:11 -07:00
|
|
|
response: string;
|
|
|
|
};
|
|
|
|
success: boolean;
|
|
|
|
errors: any[];
|
|
|
|
messages: any[];
|
2024-05-03 00:52:01 -07:00
|
|
|
}) => {
|
|
|
|
if (!res.success) {
|
2024-05-12 22:06:11 -07:00
|
|
|
console.error(res.errors);
|
|
|
|
return;
|
2024-05-03 00:52:01 -07:00
|
|
|
}
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-12 22:06:11 -07:00
|
|
|
setCode(res.result.response);
|
|
|
|
router.refresh();
|
2024-05-03 00:52:01 -07:00
|
|
|
}
|
2024-05-12 22:06:11 -07:00
|
|
|
);
|
|
|
|
};
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-03 00:52:01 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (code) {
|
2024-05-12 22:06:11 -07:00
|
|
|
setExpanded(true);
|
|
|
|
onExpand();
|
|
|
|
setLoading({ generate: false, regenerate: false });
|
2024-05-03 00:52:01 -07:00
|
|
|
}
|
2024-05-12 22:06:11 -07:00
|
|
|
}, [code]);
|
2024-05-03 00:52:01 -07:00
|
|
|
|
2024-05-02 17:38:37 -07:00
|
|
|
return (
|
|
|
|
<div className="w-full pr-4 space-y-2">
|
|
|
|
<div className="flex items-center font-sans space-x-2">
|
|
|
|
<input
|
|
|
|
ref={inputRef}
|
|
|
|
style={{
|
|
|
|
width: width + "px",
|
|
|
|
}}
|
2024-05-02 18:05:18 -07:00
|
|
|
value={input}
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
2024-05-02 17:38:37 -07:00
|
|
|
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"
|
|
|
|
/>
|
|
|
|
|
2024-05-02 18:05:18 -07:00
|
|
|
<Button
|
|
|
|
size="sm"
|
2024-05-03 00:52:01 -07:00
|
|
|
disabled={loading.generate || loading.regenerate || input === ""}
|
|
|
|
onClick={() => handleGenerate({})}
|
2024-05-02 18:05:18 -07:00
|
|
|
>
|
2024-05-03 00:52:01 -07:00
|
|
|
{loading.generate ? (
|
2024-05-02 17:38:37 -07:00
|
|
|
<>
|
|
|
|
<Loader2 className="animate-spin h-3 w-3 mr-2" />
|
|
|
|
Generating...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Sparkles className="h-3 w-3 mr-2" />
|
|
|
|
Generate Code
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{expanded ? (
|
|
|
|
<>
|
|
|
|
<div className="rounded-md border border-muted-foreground w-full h-28 overflow-y-scroll p-2">
|
2024-05-03 00:52:01 -07:00
|
|
|
<Editor
|
|
|
|
height="100%"
|
|
|
|
defaultLanguage={editor.language}
|
|
|
|
value={code}
|
|
|
|
options={{
|
|
|
|
minimap: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
scrollBeyondLastLine: false,
|
|
|
|
fontFamily: "var(--font-geist-mono)",
|
|
|
|
domReadOnly: true,
|
|
|
|
readOnly: true,
|
|
|
|
lineNumbers: "off",
|
|
|
|
glyphMargin: false,
|
|
|
|
folding: false,
|
|
|
|
// Undocumented see https://github.com/Microsoft/vscode/issues/30795#issuecomment-410998882
|
|
|
|
lineDecorationsWidth: 0,
|
|
|
|
lineNumbersMinChars: 0,
|
|
|
|
}}
|
|
|
|
theme="vs-dark"
|
|
|
|
/>
|
2024-05-02 17:38:37 -07:00
|
|
|
</div>
|
2024-05-03 00:52:01 -07:00
|
|
|
<div className="flex space-x-2 font-sans">
|
|
|
|
<Button
|
|
|
|
disabled={loading.generate || loading.regenerate}
|
|
|
|
onClick={() => onAccept(code)}
|
|
|
|
size="sm"
|
|
|
|
>
|
2024-05-02 17:38:37 -07:00
|
|
|
<Check className="h-3 w-3 mr-2" />
|
|
|
|
Accept
|
|
|
|
</Button>
|
|
|
|
<Button
|
2024-05-03 00:52:01 -07:00
|
|
|
onClick={() => handleGenerate({ regenerate: true })}
|
|
|
|
disabled={loading.generate || loading.regenerate}
|
2024-05-02 17:38:37 -07:00
|
|
|
variant="outline"
|
|
|
|
size="sm"
|
|
|
|
className="bg-transparent border-muted-foreground"
|
|
|
|
>
|
2024-05-03 00:52:01 -07:00
|
|
|
{loading.regenerate ? (
|
|
|
|
<>
|
|
|
|
<Loader2 className="animate-spin h-3 w-3 mr-2" />
|
|
|
|
Generating...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<RotateCw className="h-3 w-3 mr-2" />
|
|
|
|
Re-Generate
|
|
|
|
</>
|
|
|
|
)}
|
2024-05-02 17:38:37 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2024-05-12 22:06:11 -07:00
|
|
|
);
|
2024-05-02 17:38:37 -07:00
|
|
|
}
|