Skip to content
Open
Changes from all 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
16 changes: 14 additions & 2 deletions src/frontend/apps/impress/src/hook/useDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const formatDefault: DateTimeFormatOptions = {
};

export const useDate = () => {
const { i18n } = useTranslation();
const { i18n ,t } = useTranslation();

const formatDate = (
date: string,
Expand All @@ -22,7 +22,19 @@ export const useDate = () => {
};

const relativeDate = (date: string): string => {
return DateTime.fromISO(date).setLocale(i18n.language).toRelative() || '';
const dateTime = DateTime.fromISO(date).setLocale(i18n.language);
const relative = dateTime.toRelative();
if (relative) {
const diffInSeconds = Math.abs(dateTime.diffNow('seconds').seconds);
if (diffInSeconds < 1) {
return t('just now');
}
if (relative.includes('0 seconds') || relative.includes('0 second') ||
relative.match(/0\s*(second|seconde|segundo|秒)/i)) {
return t('just now');
}
}
return relative;
Comment on lines +25 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

  • Regex is unnecessary, we have input data so we should use it to calculate the difference
  • Locale is only required for displaying and so it would be more readable to set it when displaying
  • Always best to name variables explicitly and concisely
  • Lets use a timeframe of 5 seconds to display just now for everything below it

Suggestion

  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');

Agree / Disagree?

};

const calculateDaysLeft = (date: string, daysLimit: number): number =>
Expand Down
Loading