| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx } from "react/jsx-runtime"; |
| 3 |
import { useEffect, useState } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Timestamp/timestamp.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { Tooltip } from '../Tooltip'; |
| 7 |
import { isValidDate } from '../../helpers/datetimeUtils'; |
| 8 |
export var TimestampFormat; |
| 9 |
(function (TimestampFormat) { |
| 10 |
TimestampFormat["full"] = "full"; |
| 11 |
TimestampFormat["long"] = "long"; |
| 12 |
TimestampFormat["medium"] = "medium"; |
| 13 |
TimestampFormat["short"] = "short"; |
| 14 |
})(TimestampFormat || (TimestampFormat = {})); |
| 15 |
export var TimestampTooltipVariant; |
| 16 |
(function (TimestampTooltipVariant) { |
| 17 |
TimestampTooltipVariant["default"] = "default"; |
| 18 |
TimestampTooltipVariant["custom"] = "custom"; |
| 19 |
})(TimestampTooltipVariant || (TimestampTooltipVariant = {})); |
| 20 |
export const Timestamp = (_a) => { |
| 21 |
var { children, className, customFormat, date: dateProp, dateFormat, displaySuffix = '', is12Hour, locale, shouldDisplayUTC, timeFormat, tooltip } = _a, props = __rest(_a, ["children", "className", "customFormat", "date", "dateFormat", "displaySuffix", "is12Hour", "locale", "shouldDisplayUTC", "timeFormat", "tooltip"]); |
| 22 |
const [date, setDate] = useState(() => { |
| 23 |
const initDate = new Date(dateProp); |
| 24 |
if (isValidDate(initDate)) { |
| 25 |
return initDate; |
| 26 |
} |
| 27 |
return new Date(); |
| 28 |
}); |
| 29 |
useEffect(() => { |
| 30 |
const dateFromProp = new Date(dateProp); |
| 31 |
if (isValidDate(dateFromProp) && dateFromProp.toString() !== new Date(date).toString()) { |
| 32 |
setDate(dateFromProp); |
| 33 |
} |
| 34 |
else if (!dateProp) { |
| 35 |
setDate(new Date()); |
| 36 |
} |
| 37 |
}, [dateProp]); |
| 38 |
const hasTimeFormat = timeFormat && !customFormat; |
| 39 |
const formatOptions = Object.assign(Object.assign(Object.assign({}, (dateFormat && !customFormat && { dateStyle: dateFormat })), (customFormat && Object.assign({}, customFormat))), (is12Hour !== undefined && { hour12: is12Hour })); |
| 40 |
const dateAsLocaleString = new Date(date).toLocaleString(locale, Object.assign(Object.assign({}, formatOptions), (hasTimeFormat && { timeStyle: timeFormat }))); |
| 41 |
const utcTimeFormat = timeFormat !== 'short' ? 'medium' : 'short'; |
| 42 |
const convertToUTCString = (date) => new Date(date).toUTCString().slice(0, -3); |
| 43 |
const utcDateString = new Date(convertToUTCString(date)).toLocaleString(locale, Object.assign(Object.assign({}, formatOptions), (hasTimeFormat && { timeStyle: utcTimeFormat }))); |
| 44 |
const defaultUTCSuffix = timeFormat === 'full' ? 'Coordinated Universal Time' : 'UTC'; |
| 45 |
const createUTCContent = (customSuffix) => `${utcDateString} ${customSuffix ? customSuffix : defaultUTCSuffix}`; |
| 46 |
const defaultDisplay = shouldDisplayUTC |
| 47 |
? createUTCContent(displaySuffix) |
| 48 |
: `${dateAsLocaleString}${displaySuffix ? ' ' + displaySuffix : ''}`; |
| 49 |
const { dateTime } = props, propsWithoutDateTime = __rest(props, ["dateTime"]); |
| 50 |
const timestamp = (_jsx("span", Object.assign({ className: css(styles.timestamp, tooltip && styles.modifiers.helpText, className) }, (tooltip && { tabIndex: 0 }), propsWithoutDateTime, { children: _jsx("time", { className: `${styles.timestamp}__text`, dateTime: dateTime || new Date(date).toISOString(), children: !children ? defaultDisplay : children }) }))); |
| 51 |
return tooltip ? (_jsx(Tooltip, Object.assign({ content: tooltip.variant === TimestampTooltipVariant.default ? createUTCContent(tooltip.suffix) : tooltip.content }, tooltip.tooltipProps, { children: timestamp }))) : (timestamp); |
| 52 |
}; |
| 53 |
Timestamp.displayName = 'Timestamp'; |
| 54 |
//# sourceMappingURL=Timestamp.js.map |