import { Input } from "./ui/input"; import { useStore } from "@/store"; import { Grid, List, SearchIcon, XIcon } from "lucide-react"; import { Button } from "./ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "./ui/command"; import { cn } from "@/lib/utils"; import { Check, ChevronsUpDown } from "lucide-react"; import React from "react"; import { Tabs, TabsList, TabsTrigger } from "./ui/tabs"; import SelectedTags from "./SelectedTags"; const Search = () => { const { templates, searchQuery, setSearchQuery, setView, templatesCount } = useStore(); const selectedTags = useStore((state) => state.selectedTags); const addSelectedTag = useStore((state) => state.addSelectedTag); const removeSelectedTag = useStore((state) => state.removeSelectedTag); const [open, setOpen] = React.useState(false); const [tagSearch, setTagSearch] = React.useState(""); const view = useStore((state) => state.view); // Get all unique tags, safely handle empty templates const uniqueTags = React.useMemo(() => { if (!templates || templates.length === 0) return []; return Array.from( new Set(templates.flatMap((template) => template.tags || [])) ).sort(); }, [templates]); // Filter tags based on search const filteredTags = React.useMemo(() => { if (!tagSearch) return uniqueTags; return uniqueTags.filter((tag) => tag.toLowerCase().includes(tagSearch.toLowerCase()) ); }, [uniqueTags, tagSearch]); return (
{/*

Available Templates ({templates?.length || 0})

*/}
Available Templates
{(templatesCount && templatesCount) || 0}
setSearchQuery(e.target.value)} className="w-full p-6" /> {searchQuery.length > 0 ? (
setSearchQuery("")}>
) : ( )}
{ setView(value as "grid" | "rows"); }} > Grid List No tags found. {filteredTags.map((tag) => ( { if (selectedTags.includes(value)) { removeSelectedTag(value); } else { addSelectedTag(value); } }} > {tag} ))}
{selectedTags.length > 0 && }
); }; export default Search;