"use client"; import { useCallback, useState } from "react"; import Link from "next/link"; import { Center, Chip, Divider, Flex, Group, Text } from "@mantine/core"; import { Spotlight as MantineSpotlight, SpotlightAction } from "@mantine/spotlight"; import { IconSearch } from "@tabler/icons-react"; import { useAtomValue } from "jotai"; import type { TranslationFunction } from "@homarr/translation"; import { useI18n } from "@homarr/translation/client"; import { GroupChip } from "./chip-group"; import classes from "./component.module.css"; import { actionsAtomRead, groupsAtomRead } from "./data-store"; import { setSelectedAction, spotlightStore } from "./spotlight-store"; import type { SpotlightActionData } from "./type"; import { useWebSearchEngines } from "./web-search-engines"; export const Spotlight = () => { useWebSearchEngines(); const [query, setQuery] = useState(""); const [group, setGroup] = useState("all"); const groups = useAtomValue(groupsAtomRead); const actions = useAtomValue(actionsAtomRead); const t = useI18n(); const preparedActions = actions.map((action) => prepareAction(action, t)); const items = preparedActions .filter( (item) => (item.ignoreSearchAndOnlyShowInGroup ? item.group === group : item.title.toLowerCase().includes(query.toLowerCase().trim())) && (group === "all" || item.group === group), ) .map((item) => { const renderRoot = item.type === "link" ? (props: Record) => ( ) : undefined; return ( {item.icon && (
{typeof item.icon !== "string" && } {typeof item.icon === "string" && {item.title}}
)} {item.title} {item.description && ( {item.description} )}
); }); const onGroupChange = useCallback( (group: string) => { setSelectedAction(-1, spotlightStore); setGroup(group); }, [setGroup, setSelectedAction], ); return ( } /> {groups.map((group) => ( ))} {items.length > 0 ? items : {t("common.search.nothingFound")}} ); }; const prepareHref = (href: string, query: string) => { return href.replace("%s", query); }; const translateIfNecessary = (value: string | ((t: TranslationFunction) => string), t: TranslationFunction) => { if (typeof value === "function") { return value(t); } return value; }; const prepareAction = (action: SpotlightActionData, t: TranslationFunction) => ({ ...action, title: translateIfNecessary(action.title, t), description: translateIfNecessary(action.description, t), });