diff --git a/app/routes/machines/components/machine-row.tsx b/app/routes/machines/components/machine-row.tsx index 40c23a5..4d2da34 100644 --- a/app/routes/machines/components/machine-row.tsx +++ b/app/routes/machines/components/machine-row.tsx @@ -13,52 +13,10 @@ import type { User } from '~/types'; import cn from '~/utils/cn'; import * as hinfo from '~/utils/host-info'; import { PopulatedNode } from '~/utils/node-info'; +import { formatTimeDelta } from '~/utils/time'; import toast from '~/utils/toast'; import MenuOptions from './menu'; -/** - * Formats the time delta since last seen into a human-readable string. - * - Under 1 hour: "X minutes ago" - * - Under 1 day: "X hours, Y minutes ago" - * - Under 1 month: "X days, Y hours ago" - * - Over 1 month: "X months, Y days ago" - */ -function formatTimeDelta(lastSeen: Date): string { - const now = new Date(); - const diffMs = now.getTime() - lastSeen.getTime(); - - const minutes = Math.floor(diffMs / (1000 * 60)); - const hours = Math.floor(diffMs / (1000 * 60 * 60)); - const days = Math.floor(diffMs / (1000 * 60 * 60 * 24)); - const months = Math.floor(days / 30); - - if (minutes < 60) { - return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`; - } - - if (hours < 24) { - const remainingMinutes = minutes % 60; - if (remainingMinutes === 0) { - return `${hours} hour${hours !== 1 ? 's' : ''} ago`; - } - return `${hours} hour${hours !== 1 ? 's' : ''}, ${remainingMinutes} minute${remainingMinutes !== 1 ? 's' : ''} ago`; - } - - if (days < 30) { - const remainingHours = hours % 24; - if (remainingHours === 0) { - return `${days} day${days !== 1 ? 's' : ''} ago`; - } - return `${days} day${days !== 1 ? 's' : ''}, ${remainingHours} hour${remainingHours !== 1 ? 's' : ''} ago`; - } - - const remainingDays = days % 30; - if (remainingDays === 0) { - return `${months} month${months !== 1 ? 's' : ''} ago`; - } - return `${months} month${months !== 1 ? 's' : ''}, ${remainingDays} day${remainingDays !== 1 ? 's' : ''} ago`; -} - interface Props { node: PopulatedNode; users: User[]; diff --git a/app/utils/time.ts b/app/utils/time.ts new file mode 100644 index 0000000..90d8363 --- /dev/null +++ b/app/utils/time.ts @@ -0,0 +1,42 @@ +/** + * Formats the time delta since a given date into a human-readable string. + * - Under 1 hour: "X minutes ago" + * - Under 1 day: "X hours, Y minutes ago" + * - Under 1 month: "X days, Y hours ago" + * - Over 1 month: "X months, Y days ago" + */ +export function formatTimeDelta(date: Date): string { + const now = new Date(); + const diffMs = now.getTime() - date.getTime(); + + const minutes = Math.floor(diffMs / (1000 * 60)); + const hours = Math.floor(diffMs / (1000 * 60 * 60)); + const days = Math.floor(diffMs / (1000 * 60 * 60 * 24)); + const months = Math.floor(days / 30); + + if (minutes < 60) { + return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`; + } + + if (hours < 24) { + const remainingMinutes = minutes % 60; + if (remainingMinutes === 0) { + return `${hours} hour${hours !== 1 ? 's' : ''} ago`; + } + return `${hours} hour${hours !== 1 ? 's' : ''}, ${remainingMinutes} minute${remainingMinutes !== 1 ? 's' : ''} ago`; + } + + if (days < 30) { + const remainingHours = hours % 24; + if (remainingHours === 0) { + return `${days} day${days !== 1 ? 's' : ''} ago`; + } + return `${days} day${days !== 1 ? 's' : ''}, ${remainingHours} hour${remainingHours !== 1 ? 's' : ''} ago`; + } + + const remainingDays = days % 30; + if (remainingDays === 0) { + return `${months} month${months !== 1 ? 's' : ''} ago`; + } + return `${months} month${months !== 1 ? 's' : ''}, ${remainingDays} day${remainingDays !== 1 ? 's' : ''} ago`; +}