Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/omnichannel-contact-center-date-relative-time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Changes the date columns in the Omnichannel Contact Center to show both the formatted date and a live-updating relative time: the **Last Chat** column in the contacts list, and the **Started at** and **Last Message** columns in the chats list
62 changes: 62 additions & 0 deletions apps/meteor/client/hooks/useReactiveTimeFromNow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { act, renderHook } from '@testing-library/react';

import { useReactiveTimeFromNow } from './useReactiveTimeFromNow';

beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should return undefined when date is undefined', () => {
const { result } = renderHook(() => useReactiveTimeFromNow(undefined));

expect(result.current).toBeUndefined();
});

it('should return undefined for an invalid date instead of throwing', () => {
const { result } = renderHook(() => useReactiveTimeFromNow('not-a-real-date'));

expect(result.current).toBeUndefined();
});

it('should format the initial relative time immediately', () => {
const now = new Date('2026-05-12T12:00:00.000Z');
jest.setSystemTime(now);

const thirtySecondsAgo = new Date(now.getTime() - 30000);

const { result } = renderHook(() => useReactiveTimeFromNow(thirtySecondsAgo));

expect(result.current).toBe('1 minute ago');
});

it('should refresh the displayed text as time passes without changing props', () => {
const now = new Date('2026-05-12T12:00:00.000Z');
jest.setSystemTime(now);

const thirtySecondsAgo = new Date(now.getTime() - 30000);

const { result } = renderHook(() => useReactiveTimeFromNow(thirtySecondsAgo));

expect(result.current).toBe('1 minute ago');

act(() => {
jest.advanceTimersByTime(120000);
});

expect(result.current).toBe('3 minutes ago');
});

it('should omit the suffix when withSuffix is false', () => {
const now = new Date('2026-05-12T12:00:00.000Z');
jest.setSystemTime(now);

const fiveMinutesAgo = new Date(now.getTime() - 300000);

const { result } = renderHook(() => useReactiveTimeFromNow(fiveMinutesAgo, false));

expect(result.current).toBe('5 minutes');
});
37 changes: 37 additions & 0 deletions apps/meteor/client/hooks/useReactiveTimeFromNow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useReducer } from 'react';

import type { DateInput } from '../lib/utils/dateFormat';
import { formatFromNow } from '../lib/utils/dateFormat';

const getRefreshInterval = (elapsedMs: number): number => (Math.abs(elapsedMs) < 3600000 ? 30000 : 300000);

export const useReactiveTimeFromNow = (date: DateInput | undefined, withSuffix = true): string | undefined => {
const parsed = date !== undefined ? new Date(date).getTime() : NaN;
const time = Number.isNaN(parsed) ? undefined : parsed;

const [, rerender] = useReducer((x) => x + 1, 0);

useEffect(() => {
if (time === undefined) {
return undefined;
}

let timeoutId: ReturnType<typeof setTimeout>;

const schedule = () => {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
timeoutId = setTimeout(
() => {
rerender();
schedule();
},
getRefreshInterval(Date.now() - time),
);
};

schedule();

return () => clearTimeout(timeoutId);
}, [time]);

return time !== undefined ? formatFromNow(time, withSuffix) : undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useTranslation } from 'react-i18next';
import RemoveChatButton from './RemoveChatButton';
import { OmnichannelRoomIcon } from '../../../../../components/RoomIcon/OmnichannelRoomIcon';
import { useFormatDate } from '../../../../../hooks/useFormatDate';
import { useTimeFromNow } from '../../../../../hooks/useTimeFromNow';
import { useReactiveTimeFromNow } from '../../../../../hooks/useReactiveTimeFromNow';
import OmnichannelVerificationTag from '../../../components/OmnichannelVerificationTag';
import RoomActivityIcon from '../../../components/RoomActivityIcon';
import { useOmnichannelPriorities } from '../../../hooks/useOmnichannelPriorities';
Expand All @@ -20,7 +20,8 @@ const ChatsTableRow = (room: IOmnichannelRoomWithDepartment) => {
const { t } = useTranslation();
const { _id, fname, tags, servedBy, ts, department, open, priorityWeight, lm, onHold, source, verified } = room;
const { enabled: isPriorityEnabled } = useOmnichannelPriorities();
const getTimeFromNow = useTimeFromNow(true);
const relativeStartedTime = useReactiveTimeFromNow(ts, true);
const relativeLastMessageTime = useReactiveTimeFromNow(lm, true);
const formatDate = useFormatDate();
const { getSourceLabel } = useOmnichannelSource();

Expand Down Expand Up @@ -49,9 +50,11 @@ const ChatsTableRow = (room: IOmnichannelRoomWithDepartment) => {

return (
<GenericTableRow key={_id} tabIndex={0} onClick={() => onRowClick(_id)} action>
<GenericTableCell withTruncatedText>
<GenericTableCell withTruncatedText verticalAlign='top'>
<Box display='flex' flexDirection='column'>
<Box withTruncatedText>{fname}</Box>
<Box withTruncatedText fontScale='p2m'>
{fname}
</Box>
{tags && (
<Box color='hint' display='flex' flex-direction='row'>
{tags.map((tag: string) => (
Expand All @@ -66,34 +69,54 @@ const ChatsTableRow = (room: IOmnichannelRoomWithDepartment) => {
</Box>
</GenericTableCell>
{isPriorityEnabled && (
<GenericTableCell>
<GenericTableCell verticalAlign='top'>
<PriorityIcon level={priorityWeight} />
</GenericTableCell>
)}
<GenericTableCell withTruncatedText>
<GenericTableCell withTruncatedText verticalAlign='top'>
<Box display='flex' alignItems='center'>
<OmnichannelRoomIcon size='x20' source={source} />
<Box mis={8}>{getSourceLabel(source)}</Box>
</Box>
</GenericTableCell>
<GenericTableCell withTruncatedText>{servedBy?.username}</GenericTableCell>
<GenericTableCell>
<GenericTableCell withTruncatedText verticalAlign='top'>
{servedBy?.username}
</GenericTableCell>
<GenericTableCell verticalAlign='top'>
<Box display='flex'>
<OmnichannelVerificationTag verified={verified} />
</Box>
</GenericTableCell>
<GenericTableCell withTruncatedText>{department?.name}</GenericTableCell>
<GenericTableCell withTruncatedText title={getTimeFromNow(ts)}>
{formatDate(ts)}
<GenericTableCell withTruncatedText verticalAlign='top'>
{department?.name}
</GenericTableCell>
<GenericTableCell withTruncatedText verticalAlign='top'>
<Box display='flex' flexDirection='column'>
<Box fontScale='p2m' withTruncatedText title={formatDate(ts)}>
{formatDate(ts)}
</Box>
<Box color='hint' withTruncatedText title={relativeStartedTime}>
{relativeStartedTime}
</Box>
</Box>
</GenericTableCell>
<GenericTableCell withTruncatedText title={lm ? getTimeFromNow(lm) : undefined}>
{lm ? formatDate(lm) : undefined}
<GenericTableCell withTruncatedText verticalAlign='top'>
{lm && (
<Box display='flex' flexDirection='column'>
<Box fontScale='p2m' withTruncatedText title={formatDate(lm)}>
{formatDate(lm)}
</Box>
<Box color='hint' withTruncatedText title={relativeLastMessageTime}>
{relativeLastMessageTime}
</Box>
</Box>
)}
</GenericTableCell>
<GenericTableCell withTruncatedText>
<GenericTableCell withTruncatedText verticalAlign='top'>
<RoomActivityIcon room={room} />
{getStatusText(open, onHold)}
</GenericTableCell>
{canRemoveClosedChats && <GenericTableCell>{!open && <RemoveChatButton _id={_id} />}</GenericTableCell>}
{canRemoveClosedChats && <GenericTableCell verticalAlign='top'>{!open && <RemoveChatButton _id={_id} />}</GenericTableCell>}
</GenericTableRow>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { GenericTableCell, GenericTableRow } from '@rocket.chat/ui-client';

import ContactItemMenu from './ContactItemMenu';
import { OmnichannelRoomIcon } from '../../../../components/RoomIcon/OmnichannelRoomIcon';
import { useTimeFromNow } from '../../../../hooks/useTimeFromNow';
import { useFormatDate } from '../../../../hooks/useFormatDate';
import { useReactiveTimeFromNow } from '../../../../hooks/useReactiveTimeFromNow';
import { useOmnichannelSource } from '../../hooks/useOmnichannelSource';
import { useOmnichannelDirectoryRouter } from '../hooks/useOmnichannelDirectoryRouter';

const ContactTableRow = ({ _id, name, contactManager, lastChat, channels }: ILivechatContactWithManagerData) => {
const { getSourceLabel } = useOmnichannelSource();
const getTimeFromNow = useTimeFromNow(true);
const relativeLastChatTime = useReactiveTimeFromNow(lastChat?.ts, true);
const formatDate = useFormatDate();
const omnichannelDirectoryRouter = useOmnichannelDirectoryRouter();

const latestChannel = channels?.sort((a, b) => {
Expand All @@ -32,8 +34,12 @@ const ContactTableRow = ({ _id, name, contactManager, lastChat, channels }: ILiv

return (
<GenericTableRow action key={_id} tabIndex={0} height='40px' rcx-show-call-button-on-hover onClick={() => onRowClick(_id)}>
<GenericTableCell withTruncatedText>{name}</GenericTableCell>
<GenericTableCell withTruncatedText>
<GenericTableCell withTruncatedText verticalAlign='top'>
<Box withTruncatedText fontScale='p2m'>
{name}
</Box>
</GenericTableCell>
<GenericTableCell withTruncatedText verticalAlign='top'>
{latestChannel?.details && (
<Box withTruncatedText display='flex' alignItems='center'>
<OmnichannelRoomIcon size='x20' source={latestChannel?.details} />
Expand All @@ -43,9 +49,22 @@ const ContactTableRow = ({ _id, name, contactManager, lastChat, channels }: ILiv
</Box>
)}
</GenericTableCell>
<GenericTableCell withTruncatedText>{contactManager?.username}</GenericTableCell>
<GenericTableCell withTruncatedText>{lastChat && getTimeFromNow(lastChat.ts)}</GenericTableCell>
<GenericTableCell>
<GenericTableCell withTruncatedText verticalAlign='top'>
{contactManager?.username}
</GenericTableCell>
<GenericTableCell withTruncatedText verticalAlign='top'>
{lastChat && (
<Box display='flex' flexDirection='column'>
<Box fontScale='p2m' withTruncatedText title={formatDate(lastChat.ts)}>
{formatDate(lastChat.ts)}
</Box>
<Box color='hint' withTruncatedText title={relativeLastChatTime}>
{relativeLastChatTime}
</Box>
</Box>
)}
</GenericTableCell>
<GenericTableCell verticalAlign='top'>
<ContactItemMenu _id={_id} name={name} channels={channels} />
</GenericTableCell>
</GenericTableRow>
Expand Down
Loading