2024-05-31 19:49:46 +05:30
|
|
|
import React, { ComponentProps } from 'react';
|
2023-10-06 13:44:06 +11:00
|
|
|
import { Text, as } from 'folds';
|
2026-09-20 00:28:47 -04:00
|
|
|
import { formatTimestamp } from '../../utils/formatTimestamp';
|
2023-10-06 13:44:06 +11:00
|
|
|
|
|
|
|
|
export type TimeProps = {
|
|
|
|
|
compact?: boolean;
|
|
|
|
|
ts: number;
|
2025-07-27 15:13:00 +03:00
|
|
|
hour24Clock: boolean;
|
|
|
|
|
dateFormatString: string;
|
2023-10-06 13:44:06 +11:00
|
|
|
};
|
|
|
|
|
|
2025-07-27 15:13:00 +03:00
|
|
|
/**
|
|
|
|
|
* Renders a formatted timestamp, supporting compact and full display modes.
|
|
|
|
|
*
|
2026-09-20 00:28:47 -04:00
|
|
|
* `compact` always shows the clock time; otherwise the shared `formatTimestamp`
|
|
|
|
|
* rules apply (today → time, yesterday/this week → day word + time, else date + time).
|
2025-07-27 15:13:00 +03:00
|
|
|
*
|
|
|
|
|
* @param {number} ts - The timestamp to display.
|
|
|
|
|
* @param {boolean} [compact=false] - If true, always show only the time.
|
|
|
|
|
* @param {boolean} hour24Clock - Whether to use 24-hour time format.
|
|
|
|
|
* @param {string} dateFormatString - Format string for the date part.
|
|
|
|
|
* @returns {React.ReactElement} A <Text as="time"> element with the formatted date/time.
|
|
|
|
|
*/
|
2024-05-31 19:49:46 +05:30
|
|
|
export const Time = as<'span', TimeProps & ComponentProps<typeof Text>>(
|
2025-07-27 15:13:00 +03:00
|
|
|
({ compact, hour24Clock, dateFormatString, ts, ...props }, ref) => {
|
2026-09-20 00:28:47 -04:00
|
|
|
const time = formatTimestamp(ts, { hour24Clock, dateFormatString }, compact ? 'time' : 'auto');
|
2023-10-06 13:44:06 +11:00
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
return (
|
|
|
|
|
<Text as="time" style={{ flexShrink: 0 }} size="T200" priority="300" {...props} ref={ref}>
|
|
|
|
|
{time}
|
|
|
|
|
</Text>
|
|
|
|
|
);
|
2026-05-21 23:30:50 -04:00
|
|
|
},
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|