|
| 1 | +import { Message, MessageTypeEnum } from "@/lib/types/conversation.type"; |
| 2 | +import { Check, Pencil, X } from "lucide-react"; |
| 3 | +import React, { useEffect } from "react"; |
| 4 | +import { vapi } from "../Assistant"; |
| 5 | + |
| 6 | +function CharacterDetails() { |
| 7 | + const [characterDetails, setCharacterDetails] = React.useState< |
| 8 | + Record<string, string> |
| 9 | + >({ |
| 10 | + name: "John Doe", |
| 11 | + "personality traits": |
| 12 | + "Mysterious, Intelligent, Smart, Ruthless, Cunning, Manipulative.", |
| 13 | + }); |
| 14 | + |
| 15 | + const [editKey, setEditKey] = React.useState<string | null>(null); |
| 16 | + const [editValue, setEditValue] = React.useState<string>(""); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const onMessageUpdate = (message: Message) => { |
| 20 | + if (message.type !== MessageTypeEnum.FUNCTION_CALL) return; |
| 21 | + if (message.functionCall.name === "finalizeDetail") { |
| 22 | + const params = message.functionCall.parameters; |
| 23 | + setCharacterDetails((details) => ({ |
| 24 | + ...details, |
| 25 | + [params.key.toLowerCase()]: [params.value], |
| 26 | + })); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + vapi.on("message", onMessageUpdate); |
| 31 | + return () => { |
| 32 | + vapi.off("message", onMessageUpdate); |
| 33 | + }; |
| 34 | + }, []); |
| 35 | + |
| 36 | + const handleEdit = (key: string) => { |
| 37 | + setEditKey(key); |
| 38 | + setEditValue(characterDetails[key]); |
| 39 | + }; |
| 40 | + |
| 41 | + const handleEditChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 42 | + setEditValue(event.target.value); |
| 43 | + }; |
| 44 | + |
| 45 | + const handleCancel = () => { |
| 46 | + setEditKey(null); |
| 47 | + setEditValue(""); |
| 48 | + }; |
| 49 | + |
| 50 | + const handleSave = () => { |
| 51 | + if (editKey) { |
| 52 | + vapi.send({ |
| 53 | + type: MessageTypeEnum.ADD_MESSAGE, |
| 54 | + message: { |
| 55 | + role: "system", |
| 56 | + content: `The user has updated the final value for ${editKey} to ${editValue}.`, |
| 57 | + }, |
| 58 | + }); |
| 59 | + } |
| 60 | + setEditKey(null); |
| 61 | + setEditValue(""); |
| 62 | + }; |
| 63 | + return ( |
| 64 | + <> |
| 65 | + {Object.keys(characterDetails).map((key: string) => ( |
| 66 | + <div className="flex flex-row gap-2 justify-between w-full" key={key}> |
| 67 | + <h1 className="font-bold capitalize">{key}</h1> |
| 68 | + <div className="flex"> |
| 69 | + {editKey === key ? ( |
| 70 | + <textarea |
| 71 | + value={editValue} |
| 72 | + className="border-0 h-auto focus-visible:ring-0 focus-visible:outline-none" |
| 73 | + onChange={handleEditChange} |
| 74 | + /> |
| 75 | + ) : ( |
| 76 | + <div className="transition text-right "> |
| 77 | + {characterDetails[key]} |
| 78 | + </div> |
| 79 | + )} |
| 80 | + {editKey === key ? ( |
| 81 | + <div className="right-0 top-0 flex"> |
| 82 | + <button onClick={handleCancel} className="py-1"> |
| 83 | + <X size={16} className="font-bold text-red-500" /> |
| 84 | + </button> |
| 85 | + <button className="py-1"> |
| 86 | + <Check |
| 87 | + size={16} |
| 88 | + onClick={handleSave} |
| 89 | + className="font-bold text-green-500" |
| 90 | + /> |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + ) : ( |
| 94 | + <button className="p-1" onClick={() => handleEdit(key)}> |
| 95 | + <Pencil size={16} className="font-bold text-blue-500" /> |
| 96 | + </button> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ))} |
| 101 | + </> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +export { CharacterDetails }; |
0 commit comments