fix(web): the custom snooze calendar starts the week where the locale does (#12745)
This commit is contained in:
1 parent
80d9c181da
commit
e1cbb70527
3 files changed
+57
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
import { Button } from "./ui/button";
|
||||
import { CalendarIcon } from "lucide-react";
|
||||
import { Calendar } from "./ui/calendar";
|
||||
import { weekStartsOn } from "../timestampFormat";
|
||||
import { Popover, PopoverTrigger, PopoverPopup } from "./ui/popover";
|
||||
import { Input } from "./ui/input";
|
||||
import { Label } from "./ui/label";
|
||||
@@ -149,6 +150,7 @@ function CustomSnoozeDialog() {
|
||||
required
|
||||
selected={date}
|
||||
defaultMonth={date}
|
||||
{...(weekStartsOn === undefined ? {} : { weekStartsOn })}
|
||||
disabled={{ before: new Date(new Date().setHours(0, 0, 0, 0)) }}
|
||||
onSelect={(selected) => {
|
||||
setDate(selected);
|
||||
|
||||
@@ -52,6 +52,32 @@ describe("formatShortTimestamp", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveWeekStartsOn", () => {
|
||||
it.each([
|
||||
["en-US", 0],
|
||||
["en-GB", 1],
|
||||
["pl-PL", 1],
|
||||
["ar-EG", 6],
|
||||
])("starts the %s week on weekday %i", async (locale, weekday) => {
|
||||
const { resolveWeekStartsOn } = await import("./timestampFormat");
|
||||
expect(resolveWeekStartsOn(locale)).toBe(weekday);
|
||||
});
|
||||
|
||||
it("leaves the default to the caller for a malformed locale", async () => {
|
||||
const { resolveWeekStartsOn } = await import("./timestampFormat");
|
||||
expect(resolveWeekStartsOn("not a locale")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("follows the locale the desktop host reports", async () => {
|
||||
vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => "en-GB" } });
|
||||
vi.resetModules();
|
||||
const { weekStartsOn } = await import("./timestampFormat");
|
||||
expect(weekStartsOn).toBe(1);
|
||||
vi.unstubAllGlobals();
|
||||
vi.resetModules();
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatChatTimestampTooltip", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
|
||||
@@ -53,6 +53,35 @@ function readHostSystemLocale(): string | null {
|
||||
|
||||
const timestampLocale = resolveTimestampLocale(readHostSystemLocale());
|
||||
|
||||
const WEEKDAY_INDEXES = [0, 1, 2, 3, 4, 5, 6] as const;
|
||||
type WeekdayIndex = (typeof WEEKDAY_INDEXES)[number];
|
||||
|
||||
type LocaleWithWeekInfo = Intl.Locale & {
|
||||
readonly weekInfo?: { readonly firstDay: number };
|
||||
getWeekInfo?: () => { readonly firstDay: number };
|
||||
};
|
||||
|
||||
/**
|
||||
* First weekday of a locale as a `Date#getDay` index (0 is Sunday), or
|
||||
* `undefined` when the runtime has no week data, so callers keep their own
|
||||
* default. Without a locale it reads the runtime's.
|
||||
*/
|
||||
export function resolveWeekStartsOn(locale: string | undefined): WeekdayIndex | undefined {
|
||||
try {
|
||||
const resolved: LocaleWithWeekInfo = new Intl.Locale(
|
||||
locale ?? Intl.DateTimeFormat().resolvedOptions().locale,
|
||||
);
|
||||
// Week info counts Monday as 1 and Sunday as 7.
|
||||
const firstDay = resolved.getWeekInfo?.().firstDay ?? resolved.weekInfo?.firstDay;
|
||||
return firstDay === undefined ? undefined : WEEKDAY_INDEXES[firstDay % 7];
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/** Week start for calendars, from the same locale timestamps are shown in. */
|
||||
export const weekStartsOn = resolveWeekStartsOn(timestampLocale);
|
||||
|
||||
const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>();
|
||||
|
||||
function getTimestampFormatter(
|
||||
|
||||
Reference in new issue
Block a user