From 5299854fd6abe21da955ee097ad6a89658c6bc9c Mon Sep 17 00:00:00 2001 From: mamadoudicko Date: Thu, 3 Aug 2023 17:07:30 +0200 Subject: [PATCH] ui: improve personnality picker --- .../SettingsTab/components/PublicPrompts.tsx | 104 ++++++++++++++---- 1 file changed, 80 insertions(+), 24 deletions(-) diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx index 2f93e15266e5..be1e5d106e1d 100644 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx +++ b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx @@ -1,5 +1,5 @@ -import * as Accordion from "@radix-ui/react-accordion"; -import { ChangeEvent, useEffect, useState } from "react"; +/* eslint-disable max-lines */ +import { ChangeEvent, useEffect, useRef, useState } from "react"; import { usePromptApi } from "@/lib/api/prompt/usePromptApi"; import { Prompt } from "@/lib/types/Prompt"; @@ -12,12 +12,14 @@ export const PublicPrompts = ({ onSelect, }: PublicPromptsProps): JSX.Element => { const [publicPrompts, setPublicPrompts] = useState([]); - const { getPublicPrompts } = usePromptApi(); - const fetchPublicPrompts = async () => { - setPublicPrompts(await getPublicPrompts()); - }; + useEffect(() => { + const fetchPublicPrompts = async () => { + setPublicPrompts(await getPublicPrompts()); + }; + void fetchPublicPrompts(); + }, []); const handleChange = (event: ChangeEvent) => { const selectedPrompt = publicPrompts.find( @@ -31,27 +33,81 @@ export const PublicPrompts = ({ } }; + return ( + + ); +}; + +type CustomSelectProps = { + options: Prompt[]; + onChange: (event: ChangeEvent) => void; + onSelect: ({ title, content }: { title: string; content: string }) => void; +}; + +const CustomSelect = ({ options, onChange, onSelect }: CustomSelectProps) => { + const [isOpen, setIsOpen] = useState(false); + const [selectedOption, setSelectedOption] = useState(null); + const selectRef = useRef(null); + + const toggleDropdown = () => { + setIsOpen((prevIsOpen) => !prevIsOpen); + }; + + const handleOptionClick = (option: Prompt) => { + setSelectedOption(option); + setIsOpen(false); + onChange({ + target: { value: option.id }, + } as ChangeEvent); + onSelect({ + title: option.title, + content: option.content, + }); + }; + + const handleClickOutside = (event: MouseEvent) => { + if ( + selectRef.current && + !selectRef.current.contains(event.target as Node) + ) { + setIsOpen(false); + } + }; + useEffect(() => { - void fetchPublicPrompts(); + document.addEventListener("click", handleClickOutside, true); + + return () => { + document.removeEventListener("click", handleClickOutside, true); + }; }, []); return ( - - - Pick in public prompts - - - - - +
+ + {isOpen && ( +
+ {options.map((option) => ( +
handleOptionClick(option)} + > + {option.title} +
+ ))} +
+ )} +
); };