import { ActionIcon, Anchor, Button, Card, Center, Flex, Group, Loader, Modal, NumberInput, Stack, Table, Text, TextInput, Title, Tooltip, } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import { IconAlertTriangle, IconClick, IconListSearch } from '@tabler/icons-react'; import { useTranslation } from 'next-i18next'; import { useState } from 'react'; import { InfoCard } from '~/components/InfoCard/InfoCard'; import { City } from '~/server/api/routers/weather'; import { api } from '~/utils/api'; import { IntegrationOptionsValueType } from '../WidgetsEditModal'; type LocationSelectionProps = { widgetId: string; propName: string; value: any; handleChange: (key: string, value: IntegrationOptionsValueType) => void; info?: boolean; infoLink?: string; }; export const LocationSelection = ({ widgetId, propName: key, value, handleChange, info, infoLink, }: LocationSelectionProps) => { const { t } = useTranslation('widgets/location'); const [query, setQuery] = useState(value.name ?? ''); const [opened, { open, close }] = useDisclosure(false); const selectionEnabled = query.length > 1; const emptyLocation = t('form.empty'); const onCitySelected = (city: City) => { close(); handleChange(key, { name: city.name, latitude: city.latitude, longitude: city.longitude, }); setQuery(city.name); }; return ( <> {t(`modules/${widgetId}:descriptor.settings.${key}.label`)} {info && ( )} { setQuery(ev.currentTarget.value); handleChange(key, { name: ev.currentTarget.value, longitude: '', latitude: '', }); }} /> { if (typeof inputValue !== 'number') return; handleChange(key, { ...value, name: emptyLocation, latitude: inputValue, }); setQuery(emptyLocation); }} precision={5} label={t('form.field.latitude')} hideControls /> { if (typeof inputValue !== 'number') return; handleChange(key, { ...value, name: emptyLocation, longitude: inputValue, }); setQuery(emptyLocation); }} precision={5} label={t('form.field.longitude')} hideControls /> ); }; type CitySelectModalProps = { opened: boolean; closeModal: () => void; query: string; onCitySelected: (location: City) => void; }; const CitySelectModal = ({ opened, closeModal, query, onCitySelected }: CitySelectModalProps) => { const { t } = useTranslation('widgets/location'); const { isLoading, data, isError } = api.weather.findCity.useQuery( { query }, { retry: false, enabled: opened, refetchOnWindowFocus: false, refetchOnMount: false, } ); if (isError === true) return ( {t('modal.title')} - {query} } size="xl" opened={opened} onClose={closeModal} zIndex={250} >
{t('modal.table.nothingFound.title')} {t('modal.table.nothingFound.description')}
); const formatter = Intl.NumberFormat('en', { notation: 'compact' }); return ( {t('modal.title')} - {query} } size="xl" opened={opened} onClose={closeModal} zIndex={250} > {isLoading && ( )} {data?.results.map((city) => ( ))}
{t('modal.table.header.city')} {t('modal.table.header.country')} {t('modal.table.header.coordinates')} {t('modal.table.header.population')}
{city.name} {city.country} {city.latitude}, {city.longitude} {city.population ? ( {formatter.format(city.population)} ) : ( {t('modal.table.population.fallback')} )} { onCitySelected(city); }} >
); };