|
| 1 | +import { ChatbotUIContext } from "@/context/context" |
| 2 | +import { Tables } from "@/supabase/types" |
| 3 | +import { IconRobotFace } from "@tabler/icons-react" |
| 4 | +import Image from "next/image" |
| 5 | +import { FC, useContext, useEffect, useRef } from "react" |
| 6 | +import { usePromptAndCommand } from "./chat-hooks/use-prompt-and-command" |
| 7 | + |
| 8 | +interface AssistantPickerProps {} |
| 9 | + |
| 10 | +export const AssistantPicker: FC<AssistantPickerProps> = ({}) => { |
| 11 | + const { |
| 12 | + assistants, |
| 13 | + assistantImages, |
| 14 | + focusAssistant, |
| 15 | + atCommand, |
| 16 | + isAssistantPickerOpen, |
| 17 | + setIsAssistantPickerOpen |
| 18 | + } = useContext(ChatbotUIContext) |
| 19 | + |
| 20 | + const { handleSelectAssistant } = usePromptAndCommand() |
| 21 | + |
| 22 | + const itemsRef = useRef<(HTMLDivElement | null)[]>([]) |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (focusAssistant && itemsRef.current[0]) { |
| 26 | + itemsRef.current[0].focus() |
| 27 | + } |
| 28 | + }, [focusAssistant]) |
| 29 | + |
| 30 | + const filteredAssistants = assistants.filter(assistant => |
| 31 | + assistant.name.toLowerCase().includes(atCommand.toLowerCase()) |
| 32 | + ) |
| 33 | + |
| 34 | + const handleOpenChange = (isOpen: boolean) => { |
| 35 | + setIsAssistantPickerOpen(isOpen) |
| 36 | + } |
| 37 | + |
| 38 | + const callSelectAssistant = (assistant: Tables<"assistants">) => { |
| 39 | + handleSelectAssistant(assistant) |
| 40 | + handleOpenChange(false) |
| 41 | + } |
| 42 | + |
| 43 | + const getKeyDownHandler = |
| 44 | + (index: number) => (e: React.KeyboardEvent<HTMLDivElement>) => { |
| 45 | + if (e.key === "Backspace") { |
| 46 | + e.preventDefault() |
| 47 | + handleOpenChange(false) |
| 48 | + } else if (e.key === "Enter") { |
| 49 | + e.preventDefault() |
| 50 | + callSelectAssistant(filteredAssistants[index]) |
| 51 | + } else if ( |
| 52 | + (e.key === "Tab" || e.key === "ArrowDown") && |
| 53 | + !e.shiftKey && |
| 54 | + index === filteredAssistants.length - 1 |
| 55 | + ) { |
| 56 | + e.preventDefault() |
| 57 | + itemsRef.current[0]?.focus() |
| 58 | + } else if (e.key === "ArrowUp" && !e.shiftKey && index === 0) { |
| 59 | + // go to last element if arrow up is pressed on first element |
| 60 | + e.preventDefault() |
| 61 | + itemsRef.current[itemsRef.current.length - 1]?.focus() |
| 62 | + } else if (e.key === "ArrowUp") { |
| 63 | + e.preventDefault() |
| 64 | + const prevIndex = |
| 65 | + index - 1 >= 0 ? index - 1 : itemsRef.current.length - 1 |
| 66 | + itemsRef.current[prevIndex]?.focus() |
| 67 | + } else if (e.key === "ArrowDown") { |
| 68 | + e.preventDefault() |
| 69 | + const nextIndex = index + 1 < itemsRef.current.length ? index + 1 : 0 |
| 70 | + itemsRef.current[nextIndex]?.focus() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + {isAssistantPickerOpen && ( |
| 77 | + <div className="bg-background flex flex-col space-y-1 rounded-xl border-2 p-2 text-sm"> |
| 78 | + {filteredAssistants.length === 0 ? ( |
| 79 | + <div className="text-md flex h-14 cursor-pointer items-center justify-center italic hover:opacity-50"> |
| 80 | + No matching assistants. |
| 81 | + </div> |
| 82 | + ) : ( |
| 83 | + <> |
| 84 | + {filteredAssistants.map((item, index) => ( |
| 85 | + <div |
| 86 | + key={item.id} |
| 87 | + ref={ref => { |
| 88 | + itemsRef.current[index] = ref |
| 89 | + }} |
| 90 | + tabIndex={0} |
| 91 | + className="hover:bg-accent focus:bg-accent flex cursor-pointer items-center rounded p-2 focus:outline-none" |
| 92 | + onClick={() => |
| 93 | + callSelectAssistant(item as Tables<"assistants">) |
| 94 | + } |
| 95 | + onKeyDown={getKeyDownHandler(index)} |
| 96 | + > |
| 97 | + {item.image_path ? ( |
| 98 | + <Image |
| 99 | + src={ |
| 100 | + assistantImages.find( |
| 101 | + image => image.path === item.image_path |
| 102 | + )?.url || "" |
| 103 | + } |
| 104 | + alt={item.name} |
| 105 | + width={32} |
| 106 | + height={32} |
| 107 | + className="rounded" |
| 108 | + /> |
| 109 | + ) : ( |
| 110 | + <IconRobotFace size={32} /> |
| 111 | + )} |
| 112 | + |
| 113 | + <div className="ml-3 flex flex-col"> |
| 114 | + <div className="font-bold">{item.name}</div> |
| 115 | + |
| 116 | + <div className="truncate text-sm opacity-80"> |
| 117 | + {item.description || "No description."} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + ))} |
| 122 | + </> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + )} |
| 126 | + </> |
| 127 | + ) |
| 128 | +} |
0 commit comments