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