import { User, UserStatus } from "@nirvana/common/models/user"; import { Divider, Select, Tag } from "antd"; import { ChangeEvent, useEffect, useMemo, useRef, useState } from "react"; import { FaPlus, FaRegTimesCircle, FaSearch } from "react-icons/fa"; import searchClient, { usersIndex } from "../../services/algoliaSearchService"; import SimpleLoadingDot from "../Loading/SimpleLoadingDot"; import SimpleUserDetailsRow from "../UserDetails/SimpleUserDetailsRow"; import { debounce } from "lodash"; import toast from "react-hot-toast"; import { SearchResponse } from "@algolia/client-search"; const searchDebounceMsTime = 3000; export default function CreateConversation() { const input = useRef(); const [selectedUsers, setSelectedUsers] = useState([] as User[]); const [isLoading, setIsLoading] = useState(false); const [userSearchInput, setUserSearchInput] = useState(""); // recommendations list: algolio results + cached "relevant users" from all cached conversations currently // options for the select // have a list of 10 max? const [recommendedUsers, setRecommendedUsers] = useState( [] as User[] ); // on loading this page always focus the select field useEffect(() => { input.current?.focus(); }, []); // as people are selected, add a list of users // if it's an email of a non-nirvana user, then hit invite button to email them with a voice clip along with it // if it's an existing user, then show a nice chip of that user's profile picture and their name const handleChangeUserSearchInput = async (event) => { const newInput = event?.target?.value; console.log("searching for : " + newInput); setUserSearchInput(newInput); // no search if empty input if (!newInput) { setIsLoading(false); console.warn("no valid input to search...no point searching"); return false; } try { // do the search on the search value const searchedUsers: SearchResponse = await usersIndex.search(newInput); console.log(searchedUsers.hits); if (searchedUsers.hits) { const userHits = searchedUsers.hits.map((returnedUser) => { const recUser = returnedUser as unknown as User; recUser.id = returnedUser.objectID; return recUser; }); setRecommendedUsers(userHits); } } catch (error) { console.log(error); toast.error("Problem in searching"); } // stop showing loading to say that we got results setIsLoading(false); }; const debounceHandler = useMemo( () => debounce(handleChangeUserSearchInput, searchDebounceMsTime), [] ); // Stop the invocation of the debounced function // after unmounting useEffect(() => { return () => { debounceHandler.cancel(); }; }, []); const selectUser = (user) => { setSelectedUsers((prevUsers) => [user, ...prevUsers]); }; return (
{/* { setIsLoading(true); debounceHandler(e); }} /> {isLoading && } {/* show search results nicely like tailwind website */} {recommendedUsers && recommendedUsers.length > 0 ? ( <> showing {recommendedUsers.length} results {recommendedUsers.map((recUser) => { return ( selectUser(recUser)} className="p-2 rounded-full hover:cursor-pointer text-teal-500 hover:bg-slate-200 ml-auto" > } /> ); })} ) : ( <> )} {selectedUsers?.length ? ( <> Selected {/* list of selected people who are nirvana users */} {selectedUsers.map((selectUser) => ( } /> ))} Name Name this conversation: engineering, hangout, lunch time, or leave it as is. ) : ( <> )} {/* show conversations already with these people */}
); }