mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 12:08:01 +00:00
# Pull Request Template ## Description This PR will replace the usage of `timeMixin` with `timeHelper` Fixes https://linear.app/chatwoot/issue/CW-3451/move-time-mixin-to-a-helper ## Type of change - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? Please refer to this issue description. https://linear.app/chatwoot/issue/CW-3451/move-time-mixin-to-a-helper ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
94 lines
3.2 KiB
JavaScript
94 lines
3.2 KiB
JavaScript
import {
|
|
format,
|
|
isSameYear,
|
|
fromUnixTime,
|
|
formatDistanceToNow,
|
|
} from 'date-fns';
|
|
|
|
/**
|
|
* Formats a Unix timestamp into a human-readable time format.
|
|
* @param {number} time - Unix timestamp.
|
|
* @param {string} [dateFormat='h:mm a'] - Desired format of the time.
|
|
* @returns {string} Formatted time string.
|
|
*/
|
|
export const messageStamp = (time, dateFormat = 'h:mm a') => {
|
|
const unixTime = fromUnixTime(time);
|
|
return format(unixTime, dateFormat);
|
|
};
|
|
|
|
/**
|
|
* Provides a formatted timestamp, adjusting the format based on the current year.
|
|
* @param {number} time - Unix timestamp.
|
|
* @param {string} [dateFormat='MMM d, yyyy'] - Desired date format.
|
|
* @returns {string} Formatted date string.
|
|
*/
|
|
export const messageTimestamp = (time, dateFormat = 'MMM d, yyyy') => {
|
|
const messageTime = fromUnixTime(time);
|
|
const now = new Date();
|
|
const messageDate = format(messageTime, dateFormat);
|
|
if (!isSameYear(messageTime, now)) {
|
|
return format(messageTime, 'LLL d y, h:mm a');
|
|
}
|
|
return messageDate;
|
|
};
|
|
|
|
/**
|
|
* Converts a Unix timestamp to a relative time string (e.g., 3 hours ago).
|
|
* @param {number} time - Unix timestamp.
|
|
* @returns {string} Relative time string.
|
|
*/
|
|
export const dynamicTime = time => {
|
|
const unixTime = fromUnixTime(time);
|
|
return formatDistanceToNow(unixTime, { addSuffix: true });
|
|
};
|
|
|
|
/**
|
|
* Formats a Unix timestamp into a specified date format.
|
|
* @param {number} time - Unix timestamp.
|
|
* @param {string} [dateFormat='MMM d, yyyy'] - Desired date format.
|
|
* @returns {string} Formatted date string.
|
|
*/
|
|
export const dateFormat = (time, df = 'MMM d, yyyy') => {
|
|
const unixTime = fromUnixTime(time);
|
|
return format(unixTime, df);
|
|
};
|
|
|
|
/**
|
|
* Converts a detailed time description into a shorter format, optionally appending 'ago'.
|
|
* @param {string} time - Detailed time description (e.g., 'a minute ago').
|
|
* @param {boolean} [withAgo=false] - Whether to append 'ago' to the result.
|
|
* @returns {string} Shortened time description.
|
|
*/
|
|
export const shortTimestamp = (time, withAgo = false) => {
|
|
// This function takes a time string and converts it to a short time string
|
|
// with the following format: 1m, 1h, 1d, 1mo, 1y
|
|
// The function also takes an optional boolean parameter withAgo
|
|
// which will add the word "ago" to the end of the time string
|
|
const suffix = withAgo ? ' ago' : '';
|
|
const timeMappings = {
|
|
'less than a minute ago': 'now',
|
|
'a minute ago': `1m${suffix}`,
|
|
'an hour ago': `1h${suffix}`,
|
|
'a day ago': `1d${suffix}`,
|
|
'a month ago': `1mo${suffix}`,
|
|
'a year ago': `1y${suffix}`,
|
|
};
|
|
// Check if the time string is one of the specific cases
|
|
if (timeMappings[time]) {
|
|
return timeMappings[time];
|
|
}
|
|
const convertToShortTime = time
|
|
.replace(/about|over|almost|/g, '')
|
|
.replace(' minute ago', `m${suffix}`)
|
|
.replace(' minutes ago', `m${suffix}`)
|
|
.replace(' hour ago', `h${suffix}`)
|
|
.replace(' hours ago', `h${suffix}`)
|
|
.replace(' day ago', `d${suffix}`)
|
|
.replace(' days ago', `d${suffix}`)
|
|
.replace(' month ago', `mo${suffix}`)
|
|
.replace(' months ago', `mo${suffix}`)
|
|
.replace(' year ago', `y${suffix}`)
|
|
.replace(' years ago', `y${suffix}`);
|
|
return convertToShortTime;
|
|
};
|