/** * Device status utilities for determining online/offline state */ const ONLINE_THRESHOLD_MS = 5 * 60 * 1000; // 5 minutes /** * Check if a device is considered "online" based on lastSyncAt timestamp * A device is online if it has synced within the last 5 minutes */ export function isDeviceOnline(lastSyncAt: Date | number | null): boolean { if (!lastSyncAt) return false; const timestamp = typeof lastSyncAt === "number" ? lastSyncAt : lastSyncAt.getTime(); return Date.now() - timestamp < ONLINE_THRESHOLD_MS; } /** * Get a human-readable status string for device last seen time */ export function getDeviceStatusText(lastSyncAt: Date | number | null): string { if (!lastSyncAt) return "Never synced"; const timestamp = typeof lastSyncAt === "number" ? lastSyncAt : lastSyncAt.getTime(); const diffMs = Date.now() - timestamp; const diffMins = Math.floor(diffMs / 60000); if (diffMins < 1) return "Just now"; if (diffMins < 5) return `${diffMins}m ago`; if (diffMins < 60) return `${diffMins}m ago`; const diffHours = Math.floor(diffMins / 60); if (diffHours < 24) return `${diffHours}h ago`; const diffDays = Math.floor(diffHours / 24); return `${diffDays}d ago`; }