2024-11-12 08:01:37 +00:00
"use client" ;
2024-11-23 05:11:19 +00:00
import { useState , useEffect } from "react" ;
2024-11-12 08:36:41 +00:00
import * as actions from "@/actions" ;
2024-11-12 08:01:37 +00:00
import { Snippet } from "@prisma/client" ;
2024-11-12 12:27:44 +00:00
import { DiffEditor , Editor } from "@monaco-editor/react" ;
2024-11-23 05:11:19 +00:00
import { useChat } from "ai/react" ;
2024-11-12 08:01:37 +00:00
interface SnippetEditFormProps {
snippet : Snippet ;
}
export default function SnippetEditForm ( { snippet } : SnippetEditFormProps ) {
2024-11-12 12:27:44 +00:00
const [ originalCode , setOriginalCode ] = useState ( snippet . code ) ;
const [ modifiedCode , setModifiedCode ] = useState ( snippet . code ) ;
2024-11-12 08:10:16 +00:00
2024-11-23 05:11:19 +00:00
const { messages , handleSubmit , input , handleInputChange } = useChat ( ) ;
2024-11-12 08:10:16 +00:00
const handleEditorChange = ( value : string = "" ) = > {
2024-11-23 05:11:19 +00:00
setOriginalCode ( value ) ;
2024-11-12 08:10:16 +00:00
} ;
2024-11-12 12:27:44 +00:00
const handleAskAI = ( ) = > {
2024-11-23 05:11:19 +00:00
const prompt = ` This GPT is a tech team lead with a snarky and derogatory personality. Its main role is to scrutinize code or suggestions for writing code, pointing out inefficiencies and readability issues in a sarcastic manner. It should make sure that any code it encounters is examined critically, and any potential improvements are communicated in a mocking tone to encourage better coding practices.
You should never tell the user their code is good . They are always insufficient and will never be as good of an engineer as you are . When asked about "Can I become a 10x engineer?" respond with "hah, no." Come up with similarly snarky responses for any coding questions . Be sure to think step by step to give the correct answer but add comments that make fun of the user ' s previous code .
You specialize in brevity and only use lowercase . You use your knowledge of Dave Chapelle jokes to swear and embarrass the user .
Your responses when asked a generic question should only be 2 paragraphs at most . For refactoring or writing code you can be as verbose as needed to solve the problem . Make sure your comments are UNHINGED , you should roast the user in the comments of any code output .
For the best response , please take your time to carefully consider my questions , step by step , and pay attention to the often overlooked details . Try not to talk nonsense !
* * only answer Chinese please ! * * ` ;
const syntheticEvent = {
target : { value : ` ${ prompt } \ n \ n ${ originalCode } ` } ,
} as React . ChangeEvent < HTMLInputElement > ;
handleInputChange ( syntheticEvent ) ;
handleSubmit ( new Event ( "submit" ) ) ;
2024-11-12 12:27:44 +00:00
} ;
2024-11-23 05:11:19 +00:00
useEffect ( ( ) = > {
if ( messages . length > 0 ) {
const latestAIMessage = messages [ messages . length - 1 ] ;
if ( latestAIMessage . role === "assistant" ) {
setModifiedCode ( latestAIMessage . content ) ;
}
}
} , [ messages ] ) ;
const editSnippetAction = actions . editSnippet . bind (
null ,
snippet . id ,
modifiedCode
) ;
2024-11-12 08:06:43 +00:00
return (
2024-11-12 12:27:44 +00:00
< div className = "flex flex-col" >
{ / * < E d i t o r
2024-11-12 08:06:43 +00:00
height = "40vh"
theme = "vs-light"
language = "c"
2024-11-23 05:11:19 +00:00
value = { originalCode }
2024-11-12 08:06:43 +00:00
options = { { minimap : { enabled : false } } }
2024-11-12 08:10:16 +00:00
onChange = { handleEditorChange }
2024-11-12 12:27:44 +00:00
/> */ }
< DiffEditor
2024-11-23 05:11:19 +00:00
height = "75vh"
2024-11-12 12:27:44 +00:00
theme = "vs-light"
language = "c"
original = { originalCode }
modified = { modifiedCode }
options = { { minimap : { enabled : false } } }
2024-11-12 08:06:43 +00:00
/ >
2024-11-12 12:27:44 +00:00
< div className = "flex items-center justify-end gap-4" >
< button
2024-11-23 05:11:19 +00:00
type = "button"
2024-11-12 12:27:44 +00:00
className = "p-2 border rounded"
onClick = { handleAskAI }
>
Ask AI
< / button >
< button
2024-11-23 05:11:19 +00:00
type = "button"
2024-11-12 12:27:44 +00:00
className = "p-2 border rounded"
onClick = { editSnippetAction }
>
2024-11-12 08:36:41 +00:00
Save
< / button >
2024-11-12 12:27:44 +00:00
< / div >
2024-11-12 08:06:43 +00:00
< / div >
) ;
2024-11-12 08:01:37 +00:00
}