From a259c39c05bfece0b655f9b05761babd95f24499 Mon Sep 17 00:00:00 2001 From: Haileyesus Date: Fri, 6 Feb 2026 11:03:08 +0300 Subject: [PATCH] refactor: move formatTimeAgo function to dateUtils.ts --- src/components/Sidebar.jsx | 26 +------------------------- src/utils/dateUtils.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 src/utils/dateUtils.ts diff --git a/src/components/Sidebar.jsx b/src/components/Sidebar.jsx index a18e2ec..170543c 100644 --- a/src/components/Sidebar.jsx +++ b/src/components/Sidebar.jsx @@ -20,31 +20,7 @@ import { useTaskMaster } from '../contexts/TaskMasterContext'; import { useTasksSettings } from '../contexts/TasksSettingsContext'; import { IS_PLATFORM } from '../constants/config'; -// Move formatTimeAgo outside component to avoid recreation on every render -const formatTimeAgo = (dateString, currentTime, t) => { - const date = new Date(dateString); - const now = currentTime; - - // Check if date is valid - if (isNaN(date.getTime())) { - return t ? t('status.unknown') : 'Unknown'; - } - - const diffInMs = now - date; - const diffInSeconds = Math.floor(diffInMs / 1000); - const diffInMinutes = Math.floor(diffInMs / (1000 * 60)); - const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60)); - const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24)); - - if (diffInSeconds < 60) return t ? t('time.justNow') : 'Just now'; - if (diffInMinutes === 1) return t ? t('time.oneMinuteAgo') : '1 min ago'; - if (diffInMinutes < 60) return t ? t('time.minutesAgo', { count: diffInMinutes }) : `${diffInMinutes} mins ago`; - if (diffInHours === 1) return t ? t('time.oneHourAgo') : '1 hour ago'; - if (diffInHours < 24) return t ? t('time.hoursAgo', { count: diffInHours }) : `${diffInHours} hours ago`; - if (diffInDays === 1) return t ? t('time.oneDayAgo') : '1 day ago'; - if (diffInDays < 7) return t ? t('time.daysAgo', { count: diffInDays }) : `${diffInDays} days ago`; - return date.toLocaleDateString(); -}; +import { formatTimeAgo } from '../utils/dateUtils'; function Sidebar({ projects, diff --git a/src/utils/dateUtils.ts b/src/utils/dateUtils.ts new file mode 100644 index 0000000..94df227 --- /dev/null +++ b/src/utils/dateUtils.ts @@ -0,0 +1,24 @@ +export const formatTimeAgo = (dateString: string, currentTime: Date, t:any) => { + const date = new Date(dateString); + const now = currentTime; + + // Check if date is valid + if (isNaN(date.getTime())) { + return t ? t('status.unknown') : 'Unknown'; + } + + const diffInMs = now.getTime() - date.getTime(); + const diffInSeconds = Math.floor(diffInMs / 1000); + const diffInMinutes = Math.floor(diffInMs / (1000 * 60)); + const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60)); + const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24)); + + if (diffInSeconds < 60) return t ? t('time.justNow') : 'Just now'; + if (diffInMinutes === 1) return t ? t('time.oneMinuteAgo') : '1 min ago'; + if (diffInMinutes < 60) return t ? t('time.minutesAgo', { count: diffInMinutes }) : `${diffInMinutes} mins ago`; + if (diffInHours === 1) return t ? t('time.oneHourAgo') : '1 hour ago'; + if (diffInHours < 24) return t ? t('time.hoursAgo', { count: diffInHours }) : `${diffInHours} hours ago`; + if (diffInDays === 1) return t ? t('time.oneDayAgo') : '1 day ago'; + if (diffInDays < 7) return t ? t('time.daysAgo', { count: diffInDays }) : `${diffInDays} days ago`; + return date.toLocaleDateString(); +}; \ No newline at end of file