Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: consolidate 5 duplicate timeAgo implementations into shared utility

Adam Eivy64971ce5b2d1
diff --git a/client/src/components/NotificationDropdown.jsx b/client/src/components/NotificationDropdown.jsx
index 8e0b5a320e..7a4930b8e5 100644
--- a/client/src/components/NotificationDropdown.jsx
+++ b/client/src/components/NotificationDropdown.jsx
@@ -1,6 +1,7 @@
import { useState, useRef, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Bell, X, CheckCheck, Trash2, Brain, ListTodo, AlertTriangle, Code, HelpCircle } from 'lucide-react';
+import { timeAgo } from '../utils/formatters';
const NOTIFICATION_TYPE_CONFIG = {
memory_approval: {
@@ -37,17 +38,6 @@ const PRIORITY_COLORS = {
critical: 'border-red-500/50'
};
-function formatTimeAgo(timestamp) {
- const now = new Date();
- const date = new Date(timestamp);
- const seconds = Math.floor((now - date) / 1000);
-
- if (seconds < 60) return 'just now';
- if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
- if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
- return `${Math.floor(seconds / 86400)}d ago`;
-}
-
export default function NotificationDropdown({
notifications,
unreadCount,
@@ -192,7 +182,7 @@ export default function NotificationDropdown({
)}
<div className="flex items-center gap-2 mt-1">
<span className="text-[10px] text-gray-600">
- {formatTimeAgo(notification.timestamp)}
+ {timeAgo(notification.timestamp)}
</span>
{!notification.read && (
<button
diff --git a/client/src/components/cos/tabs/JobsTab.jsx b/client/src/components/cos/tabs/JobsTab.jsx
index 09dac79f66..70d055233c 100644
--- a/client/src/components/cos/tabs/JobsTab.jsx
+++ b/client/src/components/cos/tabs/JobsTab.jsx
@@ -2,6 +2,7 @@ import { useState, useEffect, useCallback } from 'react';
import { Plus, RefreshCw, Play, Trash2, ChevronDown, ChevronUp, Clock, ToggleLeft, ToggleRight, Edit3, Save, X, Terminal } from 'lucide-react';
import toast from 'react-hot-toast';
import * as api from '../../../services/api';
+import { timeAgo } from '../../../utils/formatters';
const INTERVAL_OPTIONS = [
{ value: 'hourly', label: 'Every Hour' },
@@ -38,17 +39,6 @@ const JOB_TYPE_OPTIONS = [
{ value: 'shell', label: 'Shell Command' }
];
-function formatTimeAgo(dateStr) {
- if (!dateStr) return 'Never';
- const diff = Date.now() - new Date(dateStr).getTime();
- const hours = Math.floor(diff / 3600000);
- const days = Math.floor(hours / 24);
- if (days > 0) return `${days}d ago`;
- if (hours > 0) return `${hours}h ago`;
- const mins = Math.floor(diff / 60000);
- return mins > 0 ? `${mins}m ago` : 'Just now';
-}
-
function formatNextDue(lastRun, intervalMs, scheduledTime) {
if (!lastRun) return scheduledTime ? `at ${scheduledTime}` : 'Immediately';
let nextDue = new Date(lastRun).getTime() + intervalMs;
@@ -162,7 +152,7 @@ function JobCard({ job, onToggle, onTrigger, onDelete, onUpdate }) {
{INTERVAL_OPTIONS.find(i => i.value === job.interval)?.label || job.interval}
{job.scheduledTime && ` at ${job.scheduledTime}`}
</span>
- <span>Last: {formatTimeAgo(job.lastRun)}</span>
+ <span>Last: {timeAgo(job.lastRun, 'Never')}</span>
{job.enabled && (
<span className={isDue ? 'text-port-warning' : 'text-gray-500'}>
Next: {formatNextDue(job.lastRun, job.intervalMs, job.scheduledTime)}
diff --git a/client/src/components/feature-agents/constants.js b/client/src/components/feature-agents/constants.js
index 01264397c4..dda459abf6 100644
--- a/client/src/components/feature-agents/constants.js
+++ b/client/src/components/feature-agents/constants.js
@@ -1,13 +1,5 @@
import { LayoutDashboard, Settings, Play, GitBranch, Terminal } from 'lucide-react';
-
-export function timeAgo(dateStr) {
- if (!dateStr) return 'never';
- const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
- if (seconds < 60) return 'just now';
- if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
- if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
- return `${Math.floor(seconds / 86400)}d ago`;
-}
+export { timeAgo } from '../../utils/formatters';
export const TABS = [
{ id: 'overview', label: 'Overview', icon: LayoutDashboard },
diff --git a/client/src/pages/GitHub.jsx b/client/src/pages/GitHub.jsx
index 8804110472..2e8afb9bab 100644
--- a/client/src/pages/GitHub.jsx
+++ b/client/src/pages/GitHub.jsx
@@ -10,20 +10,10 @@ import {
archiveGitHubRepo,
unarchiveGitHubRepo
} from '../services/api';
+import { timeAgo } from '../utils/formatters';
const FILTERS = ['all', 'npm', 'secrets', 'archived'];
-function timeAgo(dateStr) {
- if (!dateStr) return 'never';
- const diff = Date.now() - new Date(dateStr).getTime();
- const days = Math.floor(diff / 86400000);
- if (days === 0) return 'today';
- if (days === 1) return 'yesterday';
- if (days < 30) return `${days}d ago`;
- if (days < 365) return `${Math.floor(days / 30)}mo ago`;
- return `${Math.floor(days / 365)}y ago`;
-}
-
export default function GitHub() {
const [repos, setRepos] = useState({});
const [secrets, setSecrets] = useState({});
diff --git a/client/src/pages/Instances.jsx b/client/src/pages/Instances.jsx
index 404036252d..3e09bfd370 100644
--- a/client/src/pages/Instances.jsx
+++ b/client/src/pages/Instances.jsx
@@ -15,6 +15,7 @@ import {
} from '../services/api';
import PeerAppsList from '../components/instances/PeerAppsList';
import PeerAgentsSection from '../components/instances/PeerAgentsSection';
+import { timeAgo } from '../utils/formatters';
const STATUS_COLORS = {
online: 'text-port-success',
@@ -28,18 +29,6 @@ const STATUS_ICONS = {
unknown: CircleDot
};
-function timeAgo(iso) {
- if (!iso) return 'Never';
- const diff = Date.now() - new Date(iso).getTime();
- const secs = Math.floor(diff / 1000);
- if (secs < 60) return `${secs}s ago`;
- const mins = Math.floor(secs / 60);
- if (mins < 60) return `${mins}m ago`;
- const hrs = Math.floor(mins / 60);
- if (hrs < 24) return `${hrs}h ago`;
- return `${Math.floor(hrs / 24)}d ago`;
-}
-
function timeUntil(iso) {
if (!iso) return null;
const diff = new Date(iso).getTime() - Date.now();
diff --git a/client/src/utils/formatters.js b/client/src/utils/formatters.js
index 86a24d885d..6b1a39fa9f 100644
--- a/client/src/utils/formatters.js
+++ b/client/src/utils/formatters.js
@@ -33,6 +33,28 @@ export function formatRuntime(ms) {
return `${(ms / 60000).toFixed(1)}m`;
}
+/**
+ * Format a timestamp as relative time (e.g., "just now", "5m ago", "2d ago")
+ * Handles null/missing values with configurable fallback.
+ * @param {string|Date|null} dateStr - ISO timestamp, Date object, or null
+ * @param {string} fallback - Text to show for null/missing dates (default: 'never')
+ * @returns {string} Relative time string
+ */
+export function timeAgo(dateStr, fallback = 'never') {
+ if (!dateStr) return fallback;
+ const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
+ if (seconds < 0) return 'just now';
+ if (seconds < 60) return seconds < 10 ? 'just now' : `${seconds}s ago`;
+ const mins = Math.floor(seconds / 60);
+ if (mins < 60) return `${mins}m ago`;
+ const hrs = Math.floor(mins / 60);
+ if (hrs < 24) return `${hrs}h ago`;
+ const days = Math.floor(hrs / 24);
+ if (days < 30) return `${days}d ago`;
+ if (days < 365) return `${Math.floor(days / 30)}mo ago`;
+ return `${Math.floor(days / 365)}y ago`;
+}
+
/**
* Format bytes as a human-readable string
* @param {number} bytes - Size in bytes