Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Pre-fill users timezone

Blue Mouse1bb931203ceb
diff --git a/web/app/pages/Onboarding/Onboarding.tsx b/web/app/pages/Onboarding/Onboarding.tsx
index dada8538e..17474b89a 100644
--- a/web/app/pages/Onboarding/Onboarding.tsx
+++ b/web/app/pages/Onboarding/Onboarding.tsx
@@ -49,6 +49,29 @@ const MAX_PROJECT_NAME_LENGTH = 50
const ANIMATION_DURATION = 0.4
const EXIT_DURATION = 0.15
const EASE_OUT_QUART = [0.25, 1, 0.5, 1] as const
+const GENERIC_TIMEZONES = new Set([DEFAULT_TIMEZONE, 'Etc/UTC', 'GMT', 'UTC'])
+
+const isSpecificTimezone = (timezone?: string | null): timezone is string =>
+ !!timezone && !GENERIC_TIMEZONES.has(timezone)
+
+const getBrowserTimezone = () => {
+ if (typeof window === 'undefined') {
+ return null
+ }
+
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
+
+ if (!timezone || !timezone.includes('/')) {
+ return null
+ }
+
+ try {
+ new Intl.DateTimeFormat('en-US', { timeZone: timezone }).format(new Date())
+ return timezone
+ } catch {
+ return null
+ }
+}
interface Project {
id: string
@@ -455,11 +478,14 @@ const Onboarding = () => {
const [direction, setDirection] = useState(0)
const [projectName, setProjectName] = useState('')
const [projectWebsiteUrl, setProjectWebsiteUrl] = useState('')
- const [projectTimezone, setProjectTimezone] = useState(
- () => user?.timezone || DEFAULT_TIMEZONE,
- )
+ const [projectTimezone, setProjectTimezone] = useState(() => {
+ const userTimezone = user?.timezone
+
+ return isSpecificTimezone(userTimezone) ? userTimezone : DEFAULT_TIMEZONE
+ })
const [project, setProject] = useState<Project | null>(loaderProject)
const [selectedLanguage, setSelectedLanguage] = useState(i18n.language)
+ const hasAppliedBrowserTimezoneRef = useRef(false)
const [newProjectErrors, setNewProjectErrors] = useState<{
name?: string
@@ -476,8 +502,22 @@ const Onboarding = () => {
const progressStepIndex = stepsForProgress.indexOf(currentStep)
useEffect(() => {
- if (user?.timezone) {
- setProjectTimezone(user.timezone)
+ const userTimezone = user?.timezone
+
+ if (isSpecificTimezone(userTimezone)) {
+ setProjectTimezone(userTimezone)
+ return
+ }
+
+ if (hasAppliedBrowserTimezoneRef.current) {
+ return
+ }
+
+ const browserTimezone = getBrowserTimezone()
+
+ if (browserTimezone) {
+ setProjectTimezone(browserTimezone)
+ hasAppliedBrowserTimezoneRef.current = true
}
}, [user?.timezone])
diff --git a/web/app/ui/TimezonePicker.tsx b/web/app/ui/TimezonePicker.tsx
index 2755b60df..935ba77fc 100644
--- a/web/app/ui/TimezonePicker.tsx
+++ b/web/app/ui/TimezonePicker.tsx
@@ -98,6 +98,15 @@ const formatOffset = (offsetMinutes: number): string => {
: `${sign}${hours}`
}
+const isValidTimezone = (timeZone: string, date: Date): boolean => {
+ try {
+ new Intl.DateTimeFormat('en-US', { timeZone }).format(date)
+ return true
+ } catch {
+ return false
+ }
+}
+
const getTimezoneCityName = (timeZone: string): string => {
if (timeZone === 'GMT') {
return 'UTC'
@@ -156,6 +165,34 @@ const buildOptions = (date = new Date()) =>
[],
).sort((a, b) => a.offset - b.offset)
+const buildCustomOption = (
+ timeZone: string,
+ date: Date,
+): TimezoneOption | null => {
+ if (!isValidTimezone(timeZone, date)) {
+ return null
+ }
+
+ const offsetMinutes = getTimezoneOffsetMinutes(timeZone, date)
+ const hr = formatOffset(offsetMinutes)
+ const offsetLabel = `GMT${_includes(hr, '-') ? hr : `+${hr}`}`
+ const cityName = getTimezoneCityName(timeZone)
+ const genericName = getTimezoneName(timeZone, 'longGeneric', date)
+
+ return {
+ value: timeZone,
+ label: `(${offsetLabel}) ${cityName}`,
+ displayName: cityName,
+ cityName,
+ offsetLabel,
+ offset: offsetMinutes / 60,
+ abbrev: getTimezoneName(timeZone, 'short', date),
+ altName: getTimezoneName(timeZone, 'long', date),
+ genericName,
+ regionName: getTimezoneRegionName(genericName),
+ }
+}
+
const formatInTimezone = (
date: Date,
timeZone: string | null | undefined,
@@ -226,10 +263,14 @@ const TimezoneSelect = ({ value, onChange }: TimezoneSelectProps) => {
now.getMonth(),
now.getDate(),
).getTime()
- const options = useMemo(
- () => buildOptions(new Date(optionsCacheKey + 12 * 60 * 60 * 1000)),
+ const optionsReferenceDate = useMemo(
+ () => new Date(optionsCacheKey + 12 * 60 * 60 * 1000),
[optionsCacheKey],
)
+ const options = useMemo(
+ () => buildOptions(optionsReferenceDate),
+ [optionsReferenceDate],
+ )
const keyExtractor = (option: TimezoneOption) => option.value
const parseTimezone = (
@@ -254,11 +295,17 @@ const TimezoneSelect = ({ value, onChange }: TimezoneSelectProps) => {
}
if (typeof zone === 'string') {
- return _find(options, (tz) => tz.value === zone) ?? null
+ return (
+ _find(options, (tz) => tz.value === zone) ??
+ buildCustomOption(zone, optionsReferenceDate)
+ )
}
if (zone.value && !zone.label) {
- return _find(options, (tz) => tz.value === zone.value) ?? null
+ return (
+ _find(options, (tz) => tz.value === zone.value) ??
+ buildCustomOption(zone.value, optionsReferenceDate)
+ )
}
return null