|
| 1 | +import { useQueryClient } from "@tanstack/react-query"; |
1 | 2 | import { Link, useMatchRoute } from "@tanstack/react-router"; |
2 | 3 | import { AnimatePresence, motion } from "framer-motion"; |
3 | 4 | import { |
@@ -39,13 +40,50 @@ const WORKSPACE_SECTIONS = [ |
39 | 40 | { label: "Webhooks", icon: Webhook, section: "webhooks" }, |
40 | 41 | ] as const; |
41 | 42 |
|
| 43 | +function formatLastUpdated(value: number | null, now = Date.now()): string { |
| 44 | + if (!value) return "Not updated yet"; |
| 45 | + const elapsed = Math.max(0, now - value); |
| 46 | + if (elapsed < 10_000) return "Updated just now"; |
| 47 | + if (elapsed < 60_000) return `Updated ${Math.floor(elapsed / 1000)}s ago`; |
| 48 | + if (elapsed < 3_600_000) return `Updated ${Math.floor(elapsed / 60_000)}m ago`; |
| 49 | + return `Updated ${Math.floor(elapsed / 3_600_000)}h ago`; |
| 50 | +} |
| 51 | + |
| 52 | +function useLastDataUpdate(): string { |
| 53 | + const queryClient = useQueryClient(); |
| 54 | + const [updatedAt, setUpdatedAt] = useState<number | null>(null); |
| 55 | + const [now, setNow] = useState(() => Date.now()); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + function refresh() { |
| 59 | + setNow(Date.now()); |
| 60 | + const latest = queryClient |
| 61 | + .getQueryCache() |
| 62 | + .getAll() |
| 63 | + .reduce((max, query) => Math.max(max, query.state.dataUpdatedAt || 0), 0); |
| 64 | + setUpdatedAt(latest || null); |
| 65 | + } |
| 66 | + |
| 67 | + refresh(); |
| 68 | + const unsubscribe = queryClient.getQueryCache().subscribe(refresh); |
| 69 | + const interval = window.setInterval(refresh, 30_000); |
| 70 | + return () => { |
| 71 | + unsubscribe(); |
| 72 | + window.clearInterval(interval); |
| 73 | + }; |
| 74 | + }, [queryClient]); |
| 75 | + |
| 76 | + return formatLastUpdated(updatedAt, now); |
| 77 | +} |
| 78 | + |
42 | 79 | export function Sidebar() { |
43 | 80 | const matchRoute = useMatchRoute(); |
44 | 81 | const { instances, active, activate } = useInstances(); |
45 | 82 | const { theme, toggle } = useTheme(); |
46 | 83 | const { demo, toggle: toggleDemo, mask } = useDemo(); |
47 | 84 | const { showMetadata, toggle: toggleMeta } = useMetadata(); |
48 | 85 | const { data: health } = useHealthStatus(); |
| 86 | + const lastUpdated = useLastDataUpdate(); |
49 | 87 | const [switcherOpen, setSwitcherOpen] = useState(false); |
50 | 88 | const switcherRef = useRef<HTMLDivElement | null>(null); |
51 | 89 |
|
@@ -121,6 +159,9 @@ export function Sidebar() { |
121 | 159 | <p className="text-xs font-mono truncate" style={{ color: "var(--text-4)" }}> |
122 | 160 | {mask(active.baseUrl.replace(/^https?:\/\//, ""))} |
123 | 161 | </p> |
| 162 | + <p className="text-[10px] font-mono truncate" style={{ color: "var(--text-4)" }}> |
| 163 | + {lastUpdated} |
| 164 | + </p> |
124 | 165 | </div> |
125 | 166 | {instances.length > 1 && ( |
126 | 167 | <ChevronsUpDown |
|
0 commit comments