"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { ActionIcon, Alert, Anchor, Button, Collapse, Fieldset, Group, Loader, NumberInput, Paper, Stack, Text, TextInput, Tooltip, } from "@mantine/core"; import { IconClick, IconListSearch } from "@tabler/icons-react"; import type { RouterOutputs } from "@homarr/api"; import { clientApi } from "@homarr/api/client"; import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client"; import { formatLocalizedCompactNumber } from "../common/locale"; import type { OptionLocation } from "../options"; import type { CommonWidgetInputProps } from "./common"; import { useWidgetInputTranslation } from "./common"; import { useFormContext } from "./form"; export const WidgetLocationInput = ({ property, kind, options }: CommonWidgetInputProps<"location">) => { const t = useWidgetInputTranslation(kind, property); const tLocation = useI18n("widget.common.location"); const form = useFormContext(); const [searchQuery, setSearchQuery] = useState(null); const fieldPath = `options.${property}`; const inputProps = form.getInputProps(fieldPath); const value = isOptionLocation(inputProps.value) ? inputProps.value : options.defaultValue; const nameInputProps = form.getInputProps(`options.${property}.name`); const latitudeInputProps = form.getInputProps(`options.${property}.latitude`); const longitudeInputProps = form.getInputProps(`options.${property}.longitude`); const selectionEnabled = value.name.length > 1; const initializedFieldPathRef = useRef(null); useEffect(() => { if (initializedFieldPathRef.current === fieldPath) return; initializedFieldPathRef.current = fieldPath; if (isOptionLocation(inputProps.value)) return; form.setFieldValue(fieldPath, options.defaultValue); }, [fieldPath, form, inputProps.value, options.defaultValue]); const handleChange = inputProps.onChange as LocationOnChange; const unknownLocation = tLocation("unknownLocation"); const onLocationSelect = useCallback( (location: OptionLocation) => { handleChange(location); form.clearFieldError(`options.${property}.latitude`); form.clearFieldError(`options.${property}.longitude`); setSearchQuery(null); }, [form, handleChange, property], ); const onSearch = useCallback(() => { if (!selectionEnabled) return; setSearchQuery(value.name); }, [selectionEnabled, value.name]); form.watch(`options.${property}.latitude`, ({ value: nextValue }) => { if (typeof nextValue !== "number") return; form.setFieldValue(`options.${property}.name`, unknownLocation); }); form.watch(`options.${property}.longitude`, ({ value: nextValue }) => { if (typeof nextValue !== "number") return; form.setFieldValue(`options.${property}.name`, unknownLocation); }); return (
{searchQuery && ( setSearchQuery(null)} /> )}
); }; const isOptionLocation = (value: unknown): value is OptionLocation => typeof value === "object" && value !== null && "name" in value && typeof value.name === "string" && "latitude" in value && typeof value.latitude === "number" && "longitude" in value && typeof value.longitude === "number"; type LocationOnChange = ( location: Pick & { latitude: OptionLocation["latitude"] | ""; longitude: OptionLocation["longitude"] | ""; }, ) => void; interface LocationSearchResultsProps { query: string; onLocationSelect: (location: OptionLocation) => void; onClose: () => void; } const LocationSearchResults = ({ query, onLocationSelect, onClose }: LocationSearchResultsProps) => { const tLocation = useI18n("widget.common.location"); const tCommon = useI18n("common"); const { data, isPending, error, refetch } = clientApi.location.searchCity.useQuery({ query }); if (error) { return ( ); } return ( {isPending && ( )} {!isPending && data?.results.length === 0 && ( {tCommon("noResults")} )} {data?.results.map((city) => ( ))} ); }; interface LocationSelectResultProps { city: RouterOutputs["location"]["searchCity"]["results"][number]; onLocationSelect: (location: OptionLocation) => void; } const LocationSelectResult = ({ city, onLocationSelect }: LocationSelectResultProps) => { const t = useI18n("widget.common.location.table"); const locale = useCurrentIntlLocale(); let populationLabel = t("population.fallback"); if (city.population) { populationLabel = `${t("header.population")}: ${formatLocalizedCompactNumber(city.population, locale)}`; } const onSelect = useCallback(() => { onLocationSelect({ name: city.name, latitude: city.latitude, longitude: city.longitude, }); }, [city, onLocationSelect]); return ( {city.name} {city.country} {city.latitude}, {city.longitude} {populationLabel} ); };