195 lines
5.4 KiB
TypeScript
Raw Permalink Normal View History

2024-05-26 12:18:09 -07:00
"use client"
2024-05-02 17:38:37 -07:00
2024-05-26 12:18:09 -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({
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-14 01:13:57 -07:00
onClose,
2024-05-02 17:38:37 -07:00
}: {
2024-05-26 12:18:09 -07:00
user: User
socket: Socket
width: number
2024-05-03 00:52:01 -07:00
data: {
2024-05-26 12:18:09 -07:00
fileName: string
code: string
line: number
}
2024-05-03 00:52:01 -07:00
editor: {
2024-05-26 12:18:09 -07:00
language: string
}
onExpand: () => void
onAccept: (code: string) => void
onClose: () => void
2024-05-02 17:38:37 -07:00
}) {
2024-05-26 12:18:09 -07:00
const pathname = usePathname()
const router = useRouter()
const inputRef = useRef<HTMLInputElement>(null)
2024-05-02 17:38:37 -07:00
2024-05-26 12:18:09 -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-26 12:18:09 -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-26 12:18:09 -07:00
inputRef.current?.focus()
}, 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-26 12:18:09 -07:00
regenerate?: boolean
2024-05-03 00:52:01 -07:00
}) => {
2024-05-17 22:25:55 -07:00
if (user.generations >= 10) {
2024-05-26 18:37:36 -07:00
toast.error("You reached the maximum # of generations.")
2024-05-26 12:18:09 -07:00
return
}
2024-05-26 12:18:09 -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,
2024-05-14 00:18:27 -07:00
(res: { response: string; success: boolean }) => {
2024-05-26 12:18:09 -07:00
console.log("Generated code", res.response, res.success)
2024-05-14 01:13:57 -07:00
// if (!res.success) {
// toast.error("Failed to generate code.");
// return;
// }
2024-05-02 17:38:37 -07:00
2024-05-26 12:18:09 -07:00
setCode(res.response)
router.refresh()
2024-05-03 00:52:01 -07:00
}
2024-05-26 12:18:09 -07:00
)
}
2024-05-02 17:38:37 -07:00
2024-05-03 00:52:01 -07:00
useEffect(() => {
if (code) {
2024-05-26 12:18:09 -07:00
setExpanded(true)
onExpand()
setLoading({ generate: false, regenerate: false })
2024-05-03 00:52:01 -07:00
}
2024-05-26 12:18:09 -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-26 12:18:09 -07:00
placeholder="Generate code with a prompt"
2024-05-02 17:38:37 -07:00
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>
2024-05-14 01:13:57 -07:00
<Button
onClick={onClose}
variant="outline"
size="smIcon"
className="bg-transparent shrink-0 border-muted-foreground"
>
<X className="h-3 w-3" />
</Button>
2024-05-02 17:38:37 -07:00
</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-26 12:18:09 -07:00
)
2024-05-02 17:38:37 -07:00
}