|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import Component from '@glimmer/component'; |
| 7 | +import { typeOf } from '@ember/utils'; |
| 8 | +import { inject as service } from '@ember/service'; |
| 9 | +import { action } from '@ember/object'; |
| 10 | +import type { DisplayType } from '../../../services/hds-time-types.ts'; |
| 11 | + |
| 12 | +import type TimeService from '../../../services/hds-time'; |
| 13 | + |
| 14 | +export interface HdsTimeSignature { |
| 15 | + Args: { |
| 16 | + date?: Date | string; |
| 17 | + startDate?: Date | string; |
| 18 | + endDate?: Date | string; |
| 19 | + display?: |
| 20 | + | 'utc' |
| 21 | + | 'relative' |
| 22 | + | 'friendly-only' |
| 23 | + | 'friendly-local' |
| 24 | + | 'friendly-relative'; |
| 25 | + isOpen?: boolean; |
| 26 | + hasTooltip?: boolean; |
| 27 | + isoUtcString?: string; |
| 28 | + }; |
| 29 | + Element: HTMLElement; |
| 30 | +} |
| 31 | + |
| 32 | +const dateIsValid = (date?: Date | string): date is Date => |
| 33 | + date instanceof Date && !isNaN(+date); |
| 34 | + |
| 35 | +export default class HdsTime extends Component<HdsTimeSignature> { |
| 36 | + @service declare readonly hdsTime: TimeService; |
| 37 | + |
| 38 | + get date(): Date | undefined { |
| 39 | + const { date } = this.args; |
| 40 | + |
| 41 | + // Sometimes an ISO date string might be passed in instead of a JS Date. |
| 42 | + if (date) { |
| 43 | + if (typeOf(date) === 'string') { |
| 44 | + return new Date(date); |
| 45 | + } else if (date instanceof Date) { |
| 46 | + return date; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + get startDate(): Date | undefined { |
| 52 | + const { startDate } = this.args; |
| 53 | + |
| 54 | + if (startDate) { |
| 55 | + if (typeOf(startDate) === 'string') { |
| 56 | + return new Date(startDate); |
| 57 | + } else if (startDate instanceof Date) { |
| 58 | + return startDate; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + get endDate(): Date | undefined { |
| 64 | + const { endDate } = this.args; |
| 65 | + |
| 66 | + if (endDate) { |
| 67 | + if (typeOf(endDate) === 'string') { |
| 68 | + return new Date(endDate); |
| 69 | + } else if (endDate instanceof Date) { |
| 70 | + return endDate; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + get isValidDate(): boolean { |
| 76 | + return dateIsValid(this.date); |
| 77 | + } |
| 78 | + |
| 79 | + get isValidDateRange(): boolean { |
| 80 | + if (dateIsValid(this.startDate) && dateIsValid(this.endDate)) { |
| 81 | + return this.startDate <= this.endDate; |
| 82 | + } |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + get hasTooltip(): boolean { |
| 87 | + return this.args.hasTooltip ?? true; |
| 88 | + } |
| 89 | + |
| 90 | + get isoUtcString(): string | undefined { |
| 91 | + const date = this.date; |
| 92 | + |
| 93 | + if (dateIsValid(date)) { |
| 94 | + return this.hdsTime.toIsoUtcString(date); |
| 95 | + } |
| 96 | + |
| 97 | + return undefined; |
| 98 | + } |
| 99 | + |
| 100 | + get rangeIsoUtcString(): string { |
| 101 | + const startDate = this.startDate; |
| 102 | + const endDate = this.endDate; |
| 103 | + |
| 104 | + if (dateIsValid(startDate) && dateIsValid(endDate)) { |
| 105 | + return `${this.hdsTime.toIsoUtcString(startDate)}–${this.hdsTime.toIsoUtcString(endDate)}`; |
| 106 | + } |
| 107 | + return ''; |
| 108 | + } |
| 109 | + |
| 110 | + get display(): DisplayType { |
| 111 | + const date = this.date; |
| 112 | + const { display } = this.args; |
| 113 | + |
| 114 | + if (dateIsValid(date)) { |
| 115 | + const nextDiff = this.hdsTime.timeDifference(this.hdsTime.now, date); |
| 116 | + return this.hdsTime.format(nextDiff, display); |
| 117 | + } |
| 118 | + return { |
| 119 | + options: undefined, |
| 120 | + difference: { absValueInMs: 0, valueInMs: 0 }, |
| 121 | + relative: { value: 0, unit: '' }, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + get isOpen(): boolean { |
| 126 | + return this.args.isOpen ?? false; |
| 127 | + } |
| 128 | + |
| 129 | + @action |
| 130 | + didInsertNode(): void { |
| 131 | + const date = this.date; |
| 132 | + |
| 133 | + if (dateIsValid(date)) { |
| 134 | + this.hdsTime.register(date); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @action |
| 139 | + willDestroyNode(): void { |
| 140 | + const date = this.date; |
| 141 | + |
| 142 | + if (dateIsValid(date)) { |
| 143 | + this.hdsTime.unregister(date); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments