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';
|
2025-09-25 12:17:14 +05:30
|
|
|
import classNames from 'classnames';
|
2023-10-06 13:44:06 +11:00
|
|
|
import { timeDayMonYear, timeHourMinute, today, yesterday } from '../../utils/time';
|
2025-09-25 12:17:14 +05:30
|
|
|
import * as css from './Time.css';
|
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;
|
2025-10-22 16:20:22 +05:30
|
|
|
inheritPriority?: boolean;
|
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.
|
|
|
|
|
*
|
|
|
|
|
* Displays the time in hour:minute format if the message is from today, yesterday, or if `compact` is true.
|
|
|
|
|
* For older messages, it shows the date and time.
|
|
|
|
|
*
|
|
|
|
|
* @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-10-22 16:20:22 +05:30
|
|
|
({ compact, hour24Clock, dateFormatString, ts, inheritPriority, className, ...props }, ref) => {
|
2025-07-27 15:13:00 +03:00
|
|
|
const formattedTime = timeHourMinute(ts, hour24Clock);
|
|
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
let time = '';
|
|
|
|
|
if (compact) {
|
2025-07-27 15:13:00 +03:00
|
|
|
time = formattedTime;
|
2024-05-31 19:49:46 +05:30
|
|
|
} else if (today(ts)) {
|
2025-07-27 15:13:00 +03:00
|
|
|
time = formattedTime;
|
2024-05-31 19:49:46 +05:30
|
|
|
} else if (yesterday(ts)) {
|
2025-07-27 15:13:00 +03:00
|
|
|
time = `Yesterday ${formattedTime}`;
|
2024-05-31 19:49:46 +05:30
|
|
|
} else {
|
2025-10-22 16:20:22 +05:30
|
|
|
time = `${timeDayMonYear(ts, dateFormatString)}, ${formattedTime}`;
|
2024-05-31 19:49:46 +05:30
|
|
|
}
|
2023-10-06 13:44:06 +11:00
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
return (
|
2025-09-25 12:17:14 +05:30
|
|
|
<Text
|
|
|
|
|
as="time"
|
|
|
|
|
className={classNames(css.Time, className)}
|
|
|
|
|
size="T200"
|
2025-10-22 16:20:22 +05:30
|
|
|
priority={inheritPriority ? undefined : '300'}
|
2025-09-25 12:17:14 +05:30
|
|
|
{...props}
|
|
|
|
|
ref={ref}
|
|
|
|
|
>
|
2024-05-31 19:49:46 +05:30
|
|
|
{time}
|
|
|
|
|
</Text>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|