import { useRef, useState, useTransition } from "react" import Link from "next/link" import { encode, decode } from 'js-base64' // import throttle from "@jcoreio/async-throttle" import debounce from "lodash.debounce" import { GoSearch } from "react-icons/go" import { useStore } from "@/app/state/useStore" import { cn } from "@/lib/utils/cn" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { MediaInfo } from "@/types/general" import { search } from "@/app/api/v1/search" export function LatentSearchInput() { const [_pending, startTransition] = useTransition() const setSearchAutocompleteQuery = useStore(s => s.setSearchAutocompleteQuery) const showAutocompleteBox = useStore(s => s.showAutocompleteBox) const setShowAutocompleteBox = useStore(s => s.setShowAutocompleteBox) const searchAutocompleteResults = useStore(s => s.searchAutocompleteResults) const setSearchAutocompleteResults = useStore(s => s.setSearchAutocompleteResults) const setSearchQuery = useStore(s => s.setSearchQuery) const [searchDraft, setSearchDraft] = useState("") const ref = useRef(null) // called when pressing enter or clicking on search const debouncedSearch = debounce((query: string) => { startTransition(async () => { console.log(`searching the latent space for "${query}"..`) if (query.length < 2) { console.log("search term is too short") } console.log("imaginating medias..") const imaginedMedias = await search({ prompt: query, nbResults: 4 }) console.log(`imagined ${imaginedMedias.length} results:`, imaginedMedias) setSearchAutocompleteResults(imaginedMedias.map(item => ({ title: item.title, tags: item.tags, }))) // TODO: only close the show autocomplete box if we found something // setShowAutocompleteBox(false) }) }, 500) // called when pressing enter or clicking on search const handleSearch = () => { ref.current?.focus() setSearchQuery(searchDraft) setShowAutocompleteBox(true) debouncedSearch(searchDraft) } return (
{ handleSearch() }} onChange={(e) => { setSearchDraft(e.target.value) setShowAutocompleteBox(true) // handleSearch() }} onKeyDown={({ key }) => { if (key === 'Enter') { handleSearch() } }} value={searchDraft} />
{searchAutocompleteResults.length === 0 ?
Nothing to show, type something and press enter
: null} {searchAutocompleteResults.map(item => (
{item.title}
))}
) }