From fa49fa03da350f7e98b7261528cf2061f5dcd9dd Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 27 Oct 2022 14:14:52 +0200 Subject: [PATCH] Use first weekday is history. energy, logbook panels --- src/common/datetime/first_weekday.ts | 15 ++++++++++++--- src/panels/history/ha-panel-history.ts | 6 ++++-- src/panels/logbook/ha-panel-logbook.ts | 6 ++++-- .../components/hui-energy-period-selector.ts | 13 ++++++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/common/datetime/first_weekday.ts b/src/common/datetime/first_weekday.ts index 48b7b75261..f1db51620c 100644 --- a/src/common/datetime/first_weekday.ts +++ b/src/common/datetime/first_weekday.ts @@ -11,16 +11,25 @@ export const weekdays = [ "saturday", ] as const; -export const firstWeekdayIndex = (locale: FrontendLocaleData): number => { +export const firstWeekdayIndex = ( + locale: FrontendLocaleData +): 0 | 1 | 2 | 3 | 4 | 5 | 6 => { if (locale.first_weekday === FirstWeekday.language) { // @ts-ignore if ("weekInfo" in Intl.Locale.prototype) { // @ts-ignore return new Intl.Locale(locale.language).weekInfo.firstDay % 7; } - return getWeekStartByLocale(locale.language) % 7; + return (getWeekStartByLocale(locale.language) % 7) as + | 0 + | 1 + | 2 + | 3 + | 4 + | 5 + | 6; } - return weekdays.indexOf(locale.first_weekday); + return weekdays.indexOf(locale.first_weekday) as 0 | 1 | 2 | 3 | 4 | 5 | 6; }; export const firstWeekday = (locale: FrontendLocaleData) => { diff --git a/src/panels/history/ha-panel-history.ts b/src/panels/history/ha-panel-history.ts index 8be17099f5..95573534bd 100644 --- a/src/panels/history/ha-panel-history.ts +++ b/src/panels/history/ha-panel-history.ts @@ -16,6 +16,7 @@ import { } from "home-assistant-js-websocket/dist/types"; import { css, html, LitElement, PropertyValues } from "lit"; import { property, state } from "lit/decorators"; +import { firstWeekdayIndex } from "../../common/datetime/first_weekday"; import { LocalStorage } from "../../common/decorators/local-storage"; import { ensureArray } from "../../common/ensure-array"; import { navigate } from "../../common/navigate"; @@ -179,8 +180,9 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { } const today = new Date(); - const weekStart = startOfWeek(today); - const weekEnd = endOfWeek(today); + const weekStartsOn = firstWeekdayIndex(this.hass.locale); + const weekStart = startOfWeek(today, { weekStartsOn }); + const weekEnd = endOfWeek(today, { weekStartsOn }); this._ranges = { [this.hass.localize("ui.components.date-range-picker.ranges.today")]: [ diff --git a/src/panels/logbook/ha-panel-logbook.ts b/src/panels/logbook/ha-panel-logbook.ts index 84f330bb94..8f53ce7f56 100644 --- a/src/panels/logbook/ha-panel-logbook.ts +++ b/src/panels/logbook/ha-panel-logbook.ts @@ -12,6 +12,7 @@ import { } from "date-fns/esm"; import { css, html, LitElement, PropertyValues } from "lit"; import { customElement, property, state } from "lit/decorators"; +import { firstWeekdayIndex } from "../../common/datetime/first_weekday"; import { navigate } from "../../common/navigate"; import { createSearchParam, @@ -108,8 +109,9 @@ export class HaPanelLogbook extends LitElement { } const today = new Date(); - const weekStart = startOfWeek(today); - const weekEnd = endOfWeek(today); + const weekStartsOn = firstWeekdayIndex(this.hass.locale); + const weekStart = startOfWeek(today, { weekStartsOn }); + const weekEnd = endOfWeek(today, { weekStartsOn }); this._ranges = { [this.hass.localize("ui.components.date-range-picker.ranges.today")]: [ diff --git a/src/panels/lovelace/components/hui-energy-period-selector.ts b/src/panels/lovelace/components/hui-energy-period-selector.ts index 4a943b9ab8..ea79a96e35 100644 --- a/src/panels/lovelace/components/hui-energy-period-selector.ts +++ b/src/panels/lovelace/components/hui-energy-period-selector.ts @@ -36,6 +36,7 @@ import { EnergyData, getEnergyDataCollection } from "../../../data/energy"; import { SubscribeMixin } from "../../../mixins/subscribe-mixin"; import { HomeAssistant, ToggleButton } from "../../../types"; import { computeRTLDirection } from "../../../common/util/compute_rtl"; +import { firstWeekdayIndex } from "../../../common/datetime/first_weekday"; @customElement("hui-energy-period-selector") export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) { @@ -183,7 +184,9 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) { this._period === "day" ? startOfDay(start) : this._period === "week" - ? startOfWeek(start, { weekStartsOn: 1 }) + ? startOfWeek(start, { + weekStartsOn: firstWeekdayIndex(this.hass.locale), + }) : this._period === "month" ? startOfMonth(start) : startOfYear(start) @@ -195,7 +198,9 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) { this._period === "day" ? startOfToday() : this._period === "week" - ? startOfWeek(new Date(), { weekStartsOn: 1 }) + ? startOfWeek(new Date(), { + weekStartsOn: firstWeekdayIndex(this.hass.locale), + }) : this._period === "month" ? startOfMonth(new Date()) : startOfYear(new Date()) @@ -231,7 +236,9 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) { this._period === "day" ? endOfDay(startDate) : this._period === "week" - ? endOfWeek(startDate, { weekStartsOn: 1 }) + ? endOfWeek(startDate, { + weekStartsOn: firstWeekdayIndex(this.hass.locale), + }) : this._period === "month" ? endOfMonth(startDate) : endOfYear(startDate);