diff --git a/web/src/components/icon-name-combobox.tsx b/web/src/components/icon-name-combobox.tsx index 43aaee07..c02379b2 100644 --- a/web/src/components/icon-name-combobox.tsx +++ b/web/src/components/icon-name-combobox.tsx @@ -12,7 +12,7 @@ import { ComboboxList, ComboboxTrigger, } from "@/components/ui/shadcn-io/combobox" -import { pb } from "@/lib/pb" +import { useExistingIconNames } from "@/hooks/use-submissions" interface IconNameComboboxProps { value: string @@ -21,45 +21,25 @@ interface IconNameComboboxProps { } export function IconNameCombobox({ value, onValueChange, onIsExisting }: IconNameComboboxProps) { - const [existingIcons, setExistingIcons] = useState>([]) - const [loading, setLoading] = useState(true) - - useEffect(() => { - async function fetchExistingIcons() { - try { - const records = await pb.collection("community_gallery").getFullList({ - fields: "name", - sort: "name", - }) - - const uniqueNames = Array.from(new Set(records.map((r) => r.name))) - const formattedIcons = uniqueNames.map((name) => ({ - label: name, - value: name, - })) - - setExistingIcons(formattedIcons) - } catch (error) { - console.error("Failed to fetch existing icons:", error) - } finally { - setLoading(false) - } - } - - fetchExistingIcons() - }, []) + const { data: existingIcons = [], isLoading: loading } = useExistingIconNames() + const [previewValue, setPreviewValue] = useState("") + // Check if current value is existing useEffect(() => { const isExisting = existingIcons.some((icon) => icon.value === value) onIsExisting(isExisting) }, [value, existingIcons, onIsExisting]) - const handleCreateNew = (inputValue: string) => { - const sanitizedValue = inputValue + const sanitizeIconName = (inputValue: string): string => { + return inputValue .toLowerCase() .trim() .replace(/\s+/g, "-") .replace(/[^a-z0-9-]/g, "") + } + + const handleCreateNew = (inputValue: string) => { + const sanitizedValue = sanitizeIconName(inputValue) onValueChange(sanitizedValue) } @@ -75,14 +55,37 @@ export function IconNameCombobox({ value, onValueChange, onIsExisting }: IconNam {value} ) : ( - Select or create icon ID... + + {loading ? "Loading icons..." : "Select or create icon ID..."} + )} - + + + {loading ? ( + "Loading..." + ) : ( + + {(inputValue) => { + const sanitized = sanitizeIconName(inputValue) + return ( +
+ Create new icon: + + {sanitized} + +
+ ) + }} +
+ )} +
- No existing icon found. - {existingIcons.length > 0 && ( + {!loading && existingIcons.length > 0 && ( {existingIcons.map((icon) => ( @@ -91,20 +94,6 @@ export function IconNameCombobox({ value, onValueChange, onIsExisting }: IconNam ))} )} - - {(inputValue) => ( -
- Create new icon: - - {inputValue - .toLowerCase() - .trim() - .replace(/\s+/g, "-") - .replace(/[^a-z0-9-]/g, "")} - -
- )} -