"use client" import { useState } from "react" import { useAccount, useCoState } from "@/lib/providers/jazz-provider" import { LaIcon } from "@/components/custom/la-icon" import AiSearch from "../../custom/ai-search" import Link from "next/link" import { Topic, PersonalLink, PersonalPage } from "@/lib/schema" import { PublicGlobalGroup } from "@/lib/schema/master/public-group" import { JAZZ_GLOBAL_GROUP_ID } from "@/lib/constants" interface SearchTitleProps { title: string count: number } interface SearchItemProps { icon: string href: string title: string subtitle?: string topic?: Topic } const SearchTitle: React.FC = ({ title, count }) => (

{title}

{count}
) const SearchItem: React.FC = ({ icon, href, title, subtitle, topic }) => (
e.stopPropagation()} className="hover:text-primary text-sm font-medium hover:opacity-70" > {title} {subtitle && ( e.stopPropagation()} className="text-muted-foreground ml-2 truncate text-xs hover:underline" > {subtitle} )} {topic && ( {topic.latestGlobalGuide?.sections?.reduce((total, section) => total + (section?.links?.length || 0), 0) || 0}{" "} links )}
) export const SearchWrapper = () => { const [searchText, setSearchText] = useState("") const [showAiSearch, setShowAiSearch] = useState(false) const [searchResults, setSearchResults] = useState<{ topics: Topic[] links: PersonalLink[] pages: PersonalPage[] }>({ topics: [], links: [], pages: [] }) const { me } = useAccount({ root: { personalLinks: [], personalPages: [] } }) const globalGroup = useCoState(PublicGlobalGroup, JAZZ_GLOBAL_GROUP_ID, { root: { topics: [] } }) const handleSearch = (e: React.ChangeEvent) => { const value = e.target.value.toLowerCase() setSearchText(value) if (!value) { setSearchResults({ topics: [], links: [], pages: [] }) return } setSearchResults({ topics: globalGroup?.root.topics?.filter( (topic: Topic | null): topic is Topic => topic !== null && topic.prettyName.toLowerCase().startsWith(value) ) || [], links: me?.root.personalLinks?.filter( (link: PersonalLink | null): link is PersonalLink => link !== null && link.title.toLowerCase().startsWith(value) ) || [], pages: me?.root.personalPages?.filter( (page): page is PersonalPage => page !== null && page.title !== undefined && page.title.toLowerCase().startsWith(value) ) || [] }) } const clearSearch = () => { setSearchText("") setSearchResults({ topics: [], links: [], pages: [] }) setShowAiSearch(false) } return (
{searchText && ( )}
{Object.values(searchResults).some(arr => arr.length > 0) ? (
{searchResults.links.length > 0 && ( <> {searchResults.links.map(link => ( ))} )} {searchResults.pages.length > 0 && ( <> {searchResults.pages.map(page => ( ))} )} {searchResults.topics.length > 0 && ( <> {searchResults.topics.map(topic => ( ))} )}
) : (
{searchText && !showAiSearch && (
setShowAiSearch(true)} > ✨ Didn't find what you were looking for? Ask AI
)} {showAiSearch && }
)}
) }