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 (