import { v4 } from 'uuid'; import classNames from 'classnames'; import { useCallback, useState } from 'react'; import { CTAButton, Stack, Text, DropdownSelect, TextArea, useBoolean } from 'ohmy-ui'; import styles from './SearchView.module.css'; interface Message { id: string; user: 'user' | 'system'; text: string; } interface SelectOption { value: string; label: string; } export default function SearchView() { const [messages, setMessages] = useState([]); const [inputValue, setInputValue] = useState(""); const handleInputChange = useCallback((event: React.ChangeEvent) => { setInputValue(event.target.value); }, []); const searchOptions = [{ value: 'SIMILARITY', label: 'Look for similar graph nodes', }, { value: 'SUMMARY', label: 'Get a summary related to query', }, { value: 'ADJACENT', label: 'Look for graph node\'s neighbors', }, { value: 'CATEGORIES', label: 'Search by categories (Comma separated categories)', }]; const [searchType, setSearchType] = useState(searchOptions[0]); const handleSearchSubmit = useCallback((event: React.FormEvent) => { event.preventDefault(); setMessages((currentMessages) => [ ...currentMessages, { id: v4(), user: 'user', text: inputValue, }, ]); fetch('http://localhost:8000/search', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query_params: { query: inputValue, searchType: searchType.value, }, }), }) .then((response) => response.json()) .then((systemMessage) => { setMessages((currentMessages) => [ ...currentMessages, { id: v4(), user: 'system', text: systemMessage, }, ]); setInputValue(''); }) }, [inputValue, searchType]); const { value: isInputExpanded, setTrue: expandInput, setFalse: contractInput, } = useBoolean(false); return ( value={searchType} options={searchOptions} onChange={setSearchType} />
{messages.map((message) => ( {message.text} ))}