import { useState } from "react" import { useArcadiaClient } from "@crema/arcadia-core-client" import { useToast } from "@crema/notification-ui" import { TenantSection, Field } from "~/components/tenant-detail/section" import { NativeSelect, NativeSelectOption } from "~/components/ui/native-select" import { updateLocalization, TENANT_LOCALES, TENANT_CURRENCIES, COMMON_TIMEZONES, } from "~/lib/arcadia/tenants" import type { TenantTabProps } from "~/routes/tenants.$id" export function LocalizationTab({ tenant, reload }: TenantTabProps) { const arcadia = useArcadiaClient() const toast = useToast() const current = tenant.localization const [locale, setLocale] = useState(current?.locale ?? "") const [timezone, setTimezone] = useState(current?.timezone ?? "") const [currency, setCurrency] = useState(current?.currency ?? "") const [saving, setSaving] = useState(false) const [error, setError] = useState(null) // The tenant's real timezone might be an IANA name outside our curated list — // prepend it so the select shows the current value instead of silently // snapping to the first option. const timezoneOptions = current?.timezone && !COMMON_TIMEZONES.includes(current.timezone) ? [current.timezone, ...COMMON_TIMEZONES] : COMMON_TIMEZONES const dirty = locale !== (current?.locale ?? "") || timezone !== (current?.timezone ?? "") || currency !== (current?.currency ?? "") const save = async () => { setSaving(true) setError(null) try { await updateLocalization(arcadia, tenant.id, { locale, timezone, currency }) await reload() toast.success("Localization updated") } catch (err) { setError(err) } finally { setSaving(false) } } return ( setLocale(e.target.value)} data-action="tenant-detail-localization-locale" > {TENANT_LOCALES.map((l) => ( {l} ))} setCurrency(e.target.value)} data-action="tenant-detail-localization-currency" > {TENANT_CURRENCIES.map((c) => ( {c} ))} setTimezone(e.target.value)} data-action="tenant-detail-localization-timezone" > {timezoneOptions.map((tz) => ( {tz} ))} ) }