"use client"; import type { FocusEventHandler } from "react"; import { startTransition, useEffect, useState } from "react"; import { ActionIcon, Box, Button, Card, Combobox, Flex, Group, Image, Indicator, InputBase, Paper, Skeleton, Stack, Text, UnstyledButton, useCombobox, } from "@mantine/core"; import { useDebouncedValue, useUncontrolled } from "@mantine/hooks"; import { IconMoodEmpty, IconSearchOff, IconUpload } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { useSession } from "@homarr/auth/client"; import { supportedLanguages } from "@homarr/translation"; import { useScopedI18n } from "@homarr/translation/client"; import { UploadMedia } from "../upload-media/upload-media"; import classes from "./icon-picker.module.css"; interface IconPickerProps { value?: string; onChange: (iconUrl: string) => void; error?: string | null; onFocus?: FocusEventHandler; onBlur?: FocusEventHandler; label?: string; placeholder?: string; withAsterisk?: boolean; } export const IconPicker = ({ value: propsValue, onChange, error, onFocus, onBlur, withAsterisk = true, label, placeholder, }: IconPickerProps) => { const [value, setValue] = useUncontrolled({ value: propsValue, onChange }); const [query, setQuery] = useState(""); useEffect(() => { setQuery(""); }, [value]); const { data: session } = useSession(); const tCommon = useScopedI18n("common"); const [debouncedQuery] = useDebouncedValue(query, 100); const searchText = !isDirectIconUrl(debouncedQuery) && debouncedQuery !== (value ?? "") ? debouncedQuery : ""; const { data, isLoading, isError, refetch } = clientApi.icon.findIcons.useQuery( { searchText }, { placeholderData: (prev) => prev }, ); const combobox = useCombobox({ onDropdownClose: () => combobox.resetSelectedOption(), }); const totalOptions = data?.icons.reduce((acc, group) => acc + group.icons.length, 0) ?? 0; const groups = data?.icons.map((group) => { const options = group.icons.map((item) => ( { startTransition(() => { setValue(item.url); setQuery(""); combobox.closeDropdown(); }); }} > )); return ( {group.slug} {options} ); }); const inputValue = query || value || ""; const leftSection = shouldShowPreview(value) && ; const placeholderText = placeholder || (data && tCommon("iconPicker.header", { countIcons: String(data.countIcons) })) || tCommon("iconPicker.headerLoading"); const renderDropdown = () => { if (isError) { return ( {tCommon("iconPicker.error")} ); } if (isLoading) { return Array(15) .fill(0) .map((_, index) => ( )); } if (totalOptions > 0) { return {groups}; } return ( {tCommon("iconPicker.noResults", { search: debouncedQuery })} ); }; return ( } leftSection={leftSection} value={inputValue} onChange={(event) => { const nextValue = event.currentTarget.value; const trimmedValue = nextValue.trim(); setQuery(nextValue); if (isDirectIconUrl(trimmedValue)) { setValue(trimmedValue); combobox.closeDropdown(); } else { combobox.openDropdown(); } }} onClick={() => combobox.openDropdown()} onFocus={(event) => { onFocus?.(event); combobox.openDropdown(); }} onBlur={(event) => { onBlur?.(event); combobox.closeDropdown(); setQuery(""); }} rightSectionPointerEvents="none" withAsterisk={withAsterisk} error={error} label={label ?? tCommon("iconPicker.label")} placeholder={placeholderText} /> {session?.user.permissions.includes("media-upload") && ( { const first = medias.at(0); if (!first) return; startTransition(() => { setValue(first.url); }); }} > {({ onClick, loading }) => ( )} )} {renderDropdown()} ); }; // This regex is used to prevent loading a preview like en or /en which would trigger a language change // See https://github.com/homarr-labs/homarr/issues/3070 const localizationPathRegex = new RegExp(`^/?(${supportedLanguages.join("|")})(/.*)?$`, "i"); const shouldShowPreview = (value: string | null | undefined): value is string => { if (!value) return false; return !localizationPathRegex.test(value); }; const isDirectIconUrl = (value: string) => { try { const url = new URL(value.trim()); return (url.protocol === "http:" || url.protocol === "https:") && Boolean(url.hostname); } catch { return false; } };