import { Card, Stack, Text, Title, Group, TextInput, Button, NumberInput, Modal, Table, Tooltip, ActionIcon, Loader, } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import { IconListSearch, IconClick } from '@tabler/icons-react'; import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { IntegrationOptionsValueType } from '../WidgetsEditModal'; import { City } from '~/server/api/routers/weather'; import { api } from '~/utils/api'; type LocationSelectionProps = { widgetId: string; propName: string; value: any; handleChange: (key: string, value: IntegrationOptionsValueType) => void; }; export const LocationSelection = ({ widgetId, propName: key, value, handleChange, }: LocationSelectionProps) => { const { t } = useTranslation('widgets/location'); const [query, setQuery] = useState(value.name ?? ''); const [opened, { open, close }] = useDisclosure(false); const selectionEnabled = query.length > 1; const EMPTY_LOCATION = 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`)} { setQuery(ev.currentTarget.value); handleChange(key, { name: ev.currentTarget.value, longitude: '', latitude: '', }); }} /> { if (typeof v !== 'number') return; handleChange(key, { ...value, name: EMPTY_LOCATION, latitude: v, }); setQuery(EMPTY_LOCATION); }} precision={5} label={t('form.field.latitude')} hideControls /> { if (typeof v !== 'number') return; handleChange(key, { ...value, name: EMPTY_LOCATION, longitude: v, }); setQuery(EMPTY_LOCATION); }} 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 } = api.weather.findCity.useQuery( { query }, { enabled: opened, refetchOnWindowFocus: false, refetchOnMount: false, } ); 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 ? ( {t('modal.population.count', { count: city.population })} ) : ( {t('modal.population.fallback')} )} { onCitySelected(city); }} >
); };