🐛(frontend) fix doc timestamp display
Implemented the logic to show 'Just now' instead of '0 seconds ago' when the difference is under one second. Signed-off-by: buildwithricky <nwakezepatrick@gmail.com>
This commit is contained in:
committed by
Anthony LC
parent
9903bd73e2
commit
7cf42e6404
@@ -45,7 +45,7 @@ test.describe('Document search', () => {
|
|||||||
const listSearch = page.getByRole('listbox').getByRole('group');
|
const listSearch = page.getByRole('listbox').getByRole('group');
|
||||||
const rowdoc = listSearch.getByRole('option').first();
|
const rowdoc = listSearch.getByRole('option').first();
|
||||||
await expect(rowdoc.getByText('keyboard_return')).toBeVisible();
|
await expect(rowdoc.getByText('keyboard_return')).toBeVisible();
|
||||||
await expect(rowdoc.getByText(/seconds? ago/)).toBeVisible();
|
await expect(rowdoc.getByText(/just now/)).toBeVisible();
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
listSearch.getByRole('option').getByText(doc1Title),
|
listSearch.getByRole('option').getByText(doc1Title),
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { DateTime } from 'luxon';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { css } from 'styled-components';
|
import { css } from 'styled-components';
|
||||||
|
|
||||||
import { Box, Text } from '@/components';
|
import { Box, Text } from '@/components';
|
||||||
import { useCunninghamTheme } from '@/cunningham';
|
import { useCunninghamTheme } from '@/cunningham';
|
||||||
|
import { useDate } from '@/hooks/useDate';
|
||||||
import { useResponsiveStore } from '@/stores';
|
import { useResponsiveStore } from '@/stores';
|
||||||
|
|
||||||
import ChildDocument from '../assets/child-document.svg';
|
import ChildDocument from '../assets/child-document.svg';
|
||||||
@@ -38,6 +38,7 @@ export const SimpleDocItem = ({
|
|||||||
const { isDesktop } = useResponsiveStore();
|
const { isDesktop } = useResponsiveStore();
|
||||||
const { untitledDocument } = useTrans();
|
const { untitledDocument } = useTrans();
|
||||||
const { isChild } = useDocUtils(doc);
|
const { isChild } = useDocUtils(doc);
|
||||||
|
const { relativeDate } = useDate();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
@@ -100,7 +101,7 @@ export const SimpleDocItem = ({
|
|||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
>
|
>
|
||||||
<Text $size="xs" $variation="tertiary">
|
<Text $size="xs" $variation="tertiary">
|
||||||
{DateTime.fromISO(doc.updated_at).toRelative()}
|
{relativeDate(doc.updated_at)}
|
||||||
</Text>
|
</Text>
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ describe('DocsGridItemDate', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
[
|
[
|
||||||
|
{
|
||||||
|
updated_at: DateTime.now().minus({ seconds: 1 }).toISO(),
|
||||||
|
rendered: 'just now',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
updated_at: DateTime.now().minus({ minutes: 1 }).toISO(),
|
updated_at: DateTime.now().minus({ minutes: 1 }).toISO(),
|
||||||
rendered: '1 minute ago',
|
rendered: '1 minute ago',
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ const formatDefault: DateTimeFormatOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useDate = () => {
|
export const useDate = () => {
|
||||||
const { i18n } = useTranslation();
|
const { i18n, t } = useTranslation();
|
||||||
|
|
||||||
const formatDate = (
|
const formatDate = (
|
||||||
date: string,
|
date: string,
|
||||||
@@ -22,7 +22,19 @@ export const useDate = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const relativeDate = (date: string): string => {
|
const relativeDate = (date: string): string => {
|
||||||
return DateTime.fromISO(date).setLocale(i18n.language).toRelative() || '';
|
const dateToCompare = DateTime.fromISO(date);
|
||||||
|
|
||||||
|
if (!dateToCompare.isValid) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const dateNow = DateTime.now();
|
||||||
|
|
||||||
|
const differenceInSeconds = dateNow.diff(dateToCompare).as('seconds');
|
||||||
|
|
||||||
|
return Math.abs(differenceInSeconds) >= 5
|
||||||
|
? dateToCompare.toRelative({ base: dateNow, locale: i18n.language })
|
||||||
|
: t('just now');
|
||||||
};
|
};
|
||||||
|
|
||||||
const calculateDaysLeft = (date: string, daysLimit: number): number =>
|
const calculateDaysLeft = (date: string, daysLimit: number): number =>
|
||||||
|
|||||||
Reference in New Issue
Block a user