Spaces:
Paused

EMAILOUT / frontend /src /lib /timezones.js
Seth
Claude Sonnet 5
Turn campaign timezone field into a searchable city/country picker
0d1ff1d
Raw History Blame Contribute Delete
7.33 kB
// Curated IANA timezone options with human-friendly "City, Country" labels so users can search by
// country/city name (e.g. "India") instead of needing to already know the IANA zone id (e.g.
// "Asia/Kolkata"). Merged with the full canonical IANA list as a fallback for anything not curated
// below, so search still finds obscure zones — just with a less friendly label.
export const CURATED_TIMEZONES = [
{ value: 'America/New_York', label: 'New York, United States (Eastern)' },
{ value: 'America/Chicago', label: 'Chicago, United States (Central)' },
{ value: 'America/Denver', label: 'Denver, United States (Mountain)' },
{ value: 'America/Los_Angeles', label: 'Los Angeles, United States (Pacific)' },
{ value: 'America/Anchorage', label: 'Anchorage, United States (Alaska)' },
{ value: 'Pacific/Honolulu', label: 'Honolulu, United States (Hawaii)' },
{ value: 'America/Toronto', label: 'Toronto, Canada' },
{ value: 'America/Vancouver', label: 'Vancouver, Canada' },
{ value: 'America/Mexico_City', label: 'Mexico City, Mexico' },
{ value: 'America/Sao_Paulo', label: 'São Paulo, Brazil' },
{ value: 'America/Bogota', label: 'Bogotá, Colombia' },
{ value: 'America/Argentina/Buenos_Aires', label: 'Buenos Aires, Argentina' },
{ value: 'Europe/London', label: 'London, United Kingdom' },
{ value: 'Europe/Dublin', label: 'Dublin, Ireland' },
{ value: 'Europe/Lisbon', label: 'Lisbon, Portugal' },
{ value: 'Europe/Madrid', label: 'Madrid, Spain' },
{ value: 'Europe/Paris', label: 'Paris, France' },
{ value: 'Europe/Brussels', label: 'Brussels, Belgium' },
{ value: 'Europe/Amsterdam', label: 'Amsterdam, Netherlands' },
{ value: 'Europe/Berlin', label: 'Berlin, Germany' },
{ value: 'Europe/Zurich', label: 'Zurich, Switzerland' },
{ value: 'Europe/Rome', label: 'Rome, Italy' },
{ value: 'Europe/Vienna', label: 'Vienna, Austria' },
{ value: 'Europe/Warsaw', label: 'Warsaw, Poland' },
{ value: 'Europe/Stockholm', label: 'Stockholm, Sweden' },
{ value: 'Europe/Oslo', label: 'Oslo, Norway' },
{ value: 'Europe/Copenhagen', label: 'Copenhagen, Denmark' },
{ value: 'Europe/Helsinki', label: 'Helsinki, Finland' },
{ value: 'Europe/Athens', label: 'Athens, Greece' },
{ value: 'Europe/Bucharest', label: 'Bucharest, Romania' },
{ value: 'Europe/Istanbul', label: 'Istanbul, Turkey' },
{ value: 'Europe/Moscow', label: 'Moscow, Russia' },
{ value: 'Africa/Cairo', label: 'Cairo, Egypt' },
{ value: 'Africa/Lagos', label: 'Lagos, Nigeria' },
{ value: 'Africa/Nairobi', label: 'Nairobi, Kenya' },
{ value: 'Africa/Johannesburg', label: 'Johannesburg, South Africa' },
{ value: 'Africa/Casablanca', label: 'Casablanca, Morocco' },
{ value: 'Asia/Jerusalem', label: 'Jerusalem, Israel' },
{ value: 'Asia/Dubai', label: 'Dubai, United Arab Emirates' },
{ value: 'Asia/Riyadh', label: 'Riyadh, Saudi Arabia' },
{ value: 'Asia/Qatar', label: 'Doha, Qatar' },
{ value: 'Asia/Kuwait', label: 'Kuwait City, Kuwait' },
{ value: 'Asia/Karachi', label: 'Karachi, Pakistan' },
{ value: 'Asia/Kolkata', label: 'Mumbai / New Delhi, India' },
{ value: 'Asia/Colombo', label: 'Colombo, Sri Lanka' },
{ value: 'Asia/Dhaka', label: 'Dhaka, Bangladesh' },
{ value: 'Asia/Kathmandu', label: 'Kathmandu, Nepal' },
{ value: 'Asia/Almaty', label: 'Almaty, Kazakhstan' },
{ value: 'Asia/Bangkok', label: 'Bangkok, Thailand' },
{ value: 'Asia/Jakarta', label: 'Jakarta, Indonesia' },
{ value: 'Asia/Ho_Chi_Minh', label: 'Ho Chi Minh City, Vietnam' },
{ value: 'Asia/Singapore', label: 'Singapore' },
{ value: 'Asia/Kuala_Lumpur', label: 'Kuala Lumpur, Malaysia' },
{ value: 'Asia/Manila', label: 'Manila, Philippines' },
{ value: 'Asia/Hong_Kong', label: 'Hong Kong' },
{ value: 'Asia/Taipei', label: 'Taipei, Taiwan' },
{ value: 'Asia/Shanghai', label: 'Beijing / Shanghai, China' },
{ value: 'Asia/Seoul', label: 'Seoul, South Korea' },
{ value: 'Asia/Tokyo', label: 'Tokyo, Japan' },
{ value: 'Australia/Perth', label: 'Perth, Australia' },
{ value: 'Australia/Adelaide', label: 'Adelaide, Australia' },
{ value: 'Australia/Sydney', label: 'Sydney, Australia' },
{ value: 'Australia/Brisbane', label: 'Brisbane, Australia' },
{ value: 'Australia/Melbourne', label: 'Melbourne, Australia' },
{ value: 'Pacific/Auckland', label: 'Auckland, New Zealand' },
{ value: 'UTC', label: 'UTC (Coordinated Universal Time)' },
];
function prettifyZoneId(zoneId) {
const parts = zoneId.split('/');
return parts[parts.length - 1].replace(/_/g, ' ') + (parts.length > 1 ? ` (${parts[0]})` : '');
}
let _allTimezoneOptions = null;
/** Curated list first (nicer labels), then any remaining canonical IANA zones as a fallback so
* search still finds less common zones — just prettified from the zone id instead of hand-labeled. */
export function getAllTimezoneOptions() {
if (_allTimezoneOptions) return _allTimezoneOptions;
const curatedValues = new Set(CURATED_TIMEZONES.map((t) => t.value));
let canonical = [];
try {
canonical = typeof Intl.supportedValuesOf === 'function' ? Intl.supportedValuesOf('timeZone') : [];
} catch {
canonical = [];
}
const fallback = canonical
.filter((z) => !curatedValues.has(z))
.map((z) => ({ value: z, label: prettifyZoneId(z) }));
_allTimezoneOptions = [...CURATED_TIMEZONES, ...fallback];
return _allTimezoneOptions;
}
export function findTimezoneLabel(value) {
if (!value) return '';
const match = getAllTimezoneOptions().find((t) => t.value === value);
return match ? match.label : value;
}
function segments(text) {
return text
.toLowerCase()
.split(/[,/]/)
.map((s) => s.trim())
.filter(Boolean);
}
/**
* Curated "City, Country" labels are ranked well above raw IANA fallback zone ids so that typing
* a country name (e.g. "India") surfaces the intended business timezone (Kolkata) instead of
* incidental substring hits like "Indianapolis" or the "Indian/*" ocean-region zones — both of
* which literally start with "india" as a string but aren't what the user meant.
*/
export function searchTimezones(query, limit = 8) {
const q = (query || '').trim().toLowerCase();
if (!q) return [];
const curatedValues = new Set(CURATED_TIMEZONES.map((t) => t.value));
const scored = [];
for (const opt of CURATED_TIMEZONES) {
const labelSegments = segments(opt.label);
const citySegment = opt.value.toLowerCase().split('/').pop().replace(/_/g, ' ');
let score = null;
if (labelSegments.some((seg) => seg.startsWith(q)) || citySegment.startsWith(q)) score = 0;
else if (opt.label.toLowerCase().includes(q) || opt.value.toLowerCase().includes(q)) score = 1;
if (score !== null) scored.push({ ...opt, score });
}
if (scored.length < limit) {
for (const opt of getAllTimezoneOptions()) {
if (curatedValues.has(opt.value)) continue;
const label = opt.label.toLowerCase();
const value = opt.value.toLowerCase();
let score = null;
if (label.startsWith(q) || value.startsWith(q)) score = 2;
else if (label.includes(q) || value.includes(q)) score = 3;
if (score !== null) scored.push({ ...opt, score });
if (scored.length >= limit * 4) break;
}
}
scored.sort((a, b) => a.score - b.score);
return scored.slice(0, limit);
}