mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-30 17:12:58 +08:00
refactor: move formatTimeAgo function to dateUtils.ts
This commit is contained in:
@@ -20,31 +20,7 @@ import { useTaskMaster } from '../contexts/TaskMasterContext';
|
|||||||
import { useTasksSettings } from '../contexts/TasksSettingsContext';
|
import { useTasksSettings } from '../contexts/TasksSettingsContext';
|
||||||
import { IS_PLATFORM } from '../constants/config';
|
import { IS_PLATFORM } from '../constants/config';
|
||||||
|
|
||||||
// Move formatTimeAgo outside component to avoid recreation on every render
|
import { formatTimeAgo } from '../utils/dateUtils';
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
function Sidebar({
|
function Sidebar({
|
||||||
projects,
|
projects,
|
||||||
|
|||||||
24
src/utils/dateUtils.ts
Normal file
24
src/utils/dateUtils.ts
Normal file
@@ -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();
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user