Skip to content

Commit

Permalink
Fallback to VITE_DEFAULT_HOUR_FORMAT if the browser doesn't provide…
Browse files Browse the repository at this point in the history
… a time format. (Fixes #867)
  • Loading branch information
MelissaAutumn committed Feb 12, 2025
1 parent 1ee54d5 commit 9decff9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ VITE_POSTHOG_HOST=https://us.i.posthog.com
VITE_POSTHOG_UI_HOST=https://us.posthog.com

VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md

# If the browser's hour setting is empty or missing fallback to this value
VITE_DEFAULT_HOUR_FORMAT=12
3 changes: 3 additions & 0 deletions frontend/.env.prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ VITE_POSTHOG_HOST=https://data.appointment.day
VITE_POSTHOG_UI_HOST=https://us.posthog.com

VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md

# If the browser's hour setting is empty or missing fallback to this value
VITE_DEFAULT_HOUR_FORMAT=12
3 changes: 3 additions & 0 deletions frontend/.env.stage.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ VITE_POSTHOG_HOST=https://data.appointment.day
VITE_POSTHOG_UI_HOST=https://us.posthog.com

VITE_REPORT_BUG_URL=https://github.com/thunderbird/appointment/issues/new?assignees=&labels=bug&projects=&template=bug_report.md

# If the browser's hour setting is empty or missing fallback to this value
VITE_DEFAULT_HOUR_FORMAT=12
10 changes: 9 additions & 1 deletion frontend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ export const download = (data: BlobPart, filename: string, contenttype: string =
// This functions works independent from Pinia stores so that
// it can be called even if stores are not initialized yet.
export const timeFormat = (): string => {
const fallbackFormat = import.meta.env?.VITE_DEFAULT_HOUR_FORMAT ?? 12;
const user = JSON.parse(localStorage?.getItem('tba/user') ?? '{}') as User;
const detected = Intl.DateTimeFormat().resolvedOptions().hour12 ? 12 : 24;
const use12HourTime = Intl.DateTimeFormat().resolvedOptions()?.hour12;

// `.hour12` is an optional value and can be undefined. So default to our env value, and if not undefined use it.
let detected = fallbackFormat;
if (typeof use12HourTime !== 'undefined') {
detected = use12HourTime ? 12 : 24;
}

const format = Number(user.settings?.timeFormat ?? detected);
return format === 24 ? 'HH:mm' : 'hh:mm A';
};
Expand Down

0 comments on commit 9decff9

Please sign in to comment.