import { useState, useEffect, useRef } from "react"; import { Plus, MagnifyingGlass } from "@phosphor-icons/react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import paths from "@/utils/paths"; import Preloader from "@/components/Preloader"; import debounce from "lodash.debounce"; import Workspace from "@/models/workspace"; import { Tooltip } from "react-tooltip"; const DEFAULT_SEARCH_RESULTS = { workspaces: [], threads: [], }; const SEARCH_RESULT_SELECTED = "search-result-selected"; export default function SearchBox({ user, showNewWsModal }) { const { t } = useTranslation(); const searchRef = useRef(null); const [searchTerm, setSearchTerm] = useState(""); const [loading, setLoading] = useState(false); const [searchResults, setSearchResults] = useState(DEFAULT_SEARCH_RESULTS); const handleSearch = debounce(handleSearchDebounced, 500); async function handleSearchDebounced(e) { try { const searchValue = e.target.value; setSearchTerm(searchValue); setLoading(true); const searchResults = await Workspace.searchWorkspaceOrThread(searchValue); setSearchResults(searchResults); } catch (error) { console.error(error); setSearchResults(DEFAULT_SEARCH_RESULTS); } finally { setLoading(false); } } function handleReset() { searchRef.current.value = ""; setSearchTerm(""); setLoading(false); setSearchResults(DEFAULT_SEARCH_RESULTS); } useEffect(() => { window.addEventListener(SEARCH_RESULT_SELECTED, handleReset); return () => window.removeEventListener(SEARCH_RESULT_SELECTED, handleReset); }, []); return (
e.target.select()} className="border-none w-full h-full rounded-lg bg-theme-sidebar-item-default pl-4 pr-1 placeholder:text-theme-settings-input-placeholder placeholder:pl-4 outline-none text-white search-input peer text-sm" />
); } function SearchResultWrapper({ children }) { return (
{children}
); } function SearchResults({ searchResults, searchTerm, loading }) { if (!searchTerm || searchTerm.length < 3) return null; if (loading) return (

Searching for "{searchTerm}"

); if ( searchResults.workspaces.length === 0 && searchResults.threads.length === 0 ) { return (

No results found for
"{searchTerm}"

); } return ( ({ id: workspace.slug, to: paths.workspace.chat(workspace.slug), name: workspace.name, }))} /> ({ id: thread.slug, to: paths.workspace.thread(thread.workspace.slug, thread.slug), name: thread.name, hint: thread.workspace.name, }))} /> ); } function SearchResultCategory({ items, name }) { if (!items?.length) return null; return (

{name}

{items.map((item) => ( ))}
); } function SearchResultItem({ to, name, hint }) { return ( window.dispatchEvent(new Event(SEARCH_RESULT_SELECTED))} className="hover:bg-[#FFF]/10 light:hover:bg-[#000]/10 transition-all duration-300 rounded-sm px-[8px] py-[2px]" >

{name} {hint && ( | {hint} )}

); } function ShortWidthNewWorkspaceButton({ user, showNewWsModal }) { const { t } = useTranslation(); if (!!user && user?.role === "default") return null; return ( <> ); }