|
| 1 | +import { Activity, Clock, RefreshCw } from "lucide-react"; |
| 2 | +import type { ForecastStatus } from "../hooks/use-auto-predictions"; |
| 3 | + |
| 4 | +interface ForecastStatusProps { |
| 5 | + status: ForecastStatus | null; |
| 6 | + lastFetchTime: Date | null; |
| 7 | + onRefresh: () => void; |
| 8 | + loading: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Compact unified forecast status display with inline refresh |
| 13 | + */ |
| 14 | +export function ForecastStatusDisplay({ |
| 15 | + status, |
| 16 | + lastFetchTime, |
| 17 | + onRefresh, |
| 18 | + loading, |
| 19 | +}: ForecastStatusProps) { |
| 20 | + const formatTime = (timestamp: string | null) => { |
| 21 | + if (!timestamp) return null; |
| 22 | + const date = new Date(timestamp); |
| 23 | + return date.toLocaleTimeString("en-US", { |
| 24 | + hour: "2-digit", |
| 25 | + minute: "2-digit", |
| 26 | + hour12: false, |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + const formatRelativeTime = (date: Date | null) => { |
| 31 | + if (!date) return null; |
| 32 | + const now = new Date(); |
| 33 | + const diff = Math.floor((now.getTime() - date.getTime()) / 1000); |
| 34 | + |
| 35 | + if (diff < 60) return `${diff}s`; |
| 36 | + if (diff < 3600) return `${Math.floor(diff / 60)}m`; |
| 37 | + return `${Math.floor(diff / 3600)}h`; |
| 38 | + }; |
| 39 | + |
| 40 | + const isRunning = status?.scheduler?.is_running || false; |
| 41 | + const nextRun = status?.scheduler?.next_scheduled_run; |
| 42 | + // Use scheduler timestamp first, fallback to last forecast from BigQuery |
| 43 | + const lastRun = |
| 44 | + status?.scheduler?.last_run_timestamp || status?.last_forecast?.run_timestamp; |
| 45 | + const relativeTime = formatRelativeTime(lastFetchTime); |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="absolute top-4 left-4"> |
| 49 | + <div className="bg-slate-900/95 backdrop-blur-sm rounded-lg border border-slate-700/50 shadow-lg"> |
| 50 | + <div className="flex items-center gap-3 px-3 py-2"> |
| 51 | + {/* Status Icon & Text */} |
| 52 | + <div className="flex items-center gap-2"> |
| 53 | + <Activity |
| 54 | + className={`w-3.5 h-3.5 ${isRunning ? "text-green-400 animate-pulse" : "text-blue-400"}`} |
| 55 | + /> |
| 56 | + <div className="text-xs text-white font-medium"> |
| 57 | + {isRunning ? "Running" : "Auto"} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + |
| 61 | + {/* Divider */} |
| 62 | + <div className="h-4 w-px bg-slate-700/50" /> |
| 63 | + |
| 64 | + {/* Time Info */} |
| 65 | + <div className="flex items-center gap-1.5 text-[10px] text-slate-400"> |
| 66 | + {lastRun ? ( |
| 67 | + <> |
| 68 | + <Clock className="w-3 h-3" /> |
| 69 | + {relativeTime && <span>{relativeTime}</span>} |
| 70 | + {nextRun && !isRunning && ( |
| 71 | + <> |
| 72 | + <span className="text-slate-600">•</span> |
| 73 | + <span>Next {formatTime(nextRun)}</span> |
| 74 | + </> |
| 75 | + )} |
| 76 | + </> |
| 77 | + ) : ( |
| 78 | + <span>Waiting for first run</span> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + |
| 82 | + {/* Divider */} |
| 83 | + <div className="h-4 w-px bg-slate-700/50" /> |
| 84 | + |
| 85 | + {/* Refresh Button */} |
| 86 | + <button |
| 87 | + onClick={onRefresh} |
| 88 | + disabled={loading} |
| 89 | + className="hover:bg-slate-800/80 disabled:opacity-50 disabled:cursor-not-allowed rounded p-1 transition-all group" |
| 90 | + title="Refresh predictions" |
| 91 | + > |
| 92 | + <RefreshCw |
| 93 | + className={`w-3.5 h-3.5 text-blue-400 ${loading ? "animate-spin" : "group-hover:rotate-180 transition-transform duration-500"}`} |
| 94 | + /> |
| 95 | + </button> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ); |
| 100 | +} |
0 commit comments