-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add last-changed date to more-info (#16509)
- Loading branch information
Showing
6 changed files
with
164 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isSameDay, isSameYear } from "date-fns"; | ||
import { FrontendLocaleData } from "../../data/translation"; | ||
import { | ||
formatShortDateTime, | ||
formatShortDateTimeWithYear, | ||
} from "./format_date_time"; | ||
import { formatTime } from "./format_time"; | ||
|
||
export const absoluteTime = ( | ||
from: Date, | ||
locale: FrontendLocaleData, | ||
to?: Date | ||
): string => { | ||
const _to = to ?? new Date(); | ||
|
||
if (isSameDay(from, _to)) { | ||
return formatTime(from, locale); | ||
} | ||
if (isSameYear(from, _to)) { | ||
return formatShortDateTime(from, locale); | ||
} | ||
return formatShortDateTimeWithYear(from, locale); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { addDays, differenceInMilliseconds, startOfDay } from "date-fns"; | ||
import { PropertyValues, ReactiveElement } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import { absoluteTime } from "../common/datetime/absolute_time"; | ||
import type { HomeAssistant } from "../types"; | ||
|
||
const SAFE_MARGIN = 5 * 1000; | ||
|
||
@customElement("ha-absolute-time") | ||
class HaAbsoluteTime extends ReactiveElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public datetime?: string | Date; | ||
|
||
private _timeout?: number; | ||
|
||
public disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
this._clearTimeout(); | ||
} | ||
|
||
public connectedCallback(): void { | ||
super.connectedCallback(); | ||
if (this.datetime) { | ||
this._updateNextDay(); | ||
} | ||
} | ||
|
||
protected createRenderRoot() { | ||
return this; | ||
} | ||
|
||
protected firstUpdated(changedProps: PropertyValues) { | ||
super.firstUpdated(changedProps); | ||
this._updateAbsolute(); | ||
} | ||
|
||
protected update(changedProps: PropertyValues) { | ||
super.update(changedProps); | ||
this._updateAbsolute(); | ||
} | ||
|
||
private _clearTimeout(): void { | ||
if (this._timeout) { | ||
window.clearTimeout(this._timeout); | ||
this._timeout = undefined; | ||
} | ||
} | ||
|
||
private _updateNextDay(): void { | ||
this._clearTimeout(); | ||
|
||
const now = new Date(); | ||
const nextDay = addDays(startOfDay(now), 1); | ||
const ms = differenceInMilliseconds(nextDay, now) + SAFE_MARGIN; | ||
|
||
this._timeout = window.setTimeout(() => { | ||
this._updateNextDay(); | ||
this._updateAbsolute(); | ||
}, ms); | ||
} | ||
|
||
private _updateAbsolute(): void { | ||
if (!this.datetime) { | ||
this.innerHTML = this.hass.localize("ui.components.absolute_time.never"); | ||
} else { | ||
this.innerHTML = absoluteTime(new Date(this.datetime), this.hass.locale); | ||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-absolute-time": HaAbsoluteTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters