|
| 1 | +import React, { useSyncExternalStore, useCallback } from 'react'; |
| 2 | +import { useTranslation } from '../i18n'; |
| 3 | + |
| 4 | +interface RefreshControlProps { |
| 5 | + isPolling: boolean; |
| 6 | + isPaused: boolean; |
| 7 | + pauseReason: 'hidden' | 'offline' | 'manual' | null; |
| 8 | + onPause: () => void; |
| 9 | + onResume: () => void; |
| 10 | + onRefresh: () => void; |
| 11 | + isRefetching?: boolean; |
| 12 | + lastUpdated?: Date | null; |
| 13 | +} |
| 14 | + |
| 15 | +function subscribeToTime(callback: () => void): () => void { |
| 16 | + const interval = setInterval(callback, 30000); |
| 17 | + return () => clearInterval(interval); |
| 18 | +} |
| 19 | + |
| 20 | +function useRelativeTime(date: Date | null | undefined, t: (key: string) => string): string { |
| 21 | + const getSnapshot = useCallback(() => { |
| 22 | + if (!date) return ''; |
| 23 | + const seconds = Math.floor((Date.now() - date.getTime()) / 1000); |
| 24 | + if (seconds < 60) return t('refresh.justNow'); |
| 25 | + const minutes = Math.floor(seconds / 60); |
| 26 | + if (minutes === 1) return t('refresh.oneMinuteAgo'); |
| 27 | + return `${minutes} ${t('refresh.minutesAgo')}`; |
| 28 | + }, [date, t]); |
| 29 | + |
| 30 | + return useSyncExternalStore(subscribeToTime, getSnapshot, getSnapshot); |
| 31 | +} |
| 32 | + |
| 33 | +const RefreshControl: React.FC<RefreshControlProps> = ({ |
| 34 | + isPolling, |
| 35 | + isPaused, |
| 36 | + pauseReason, |
| 37 | + onPause, |
| 38 | + onResume, |
| 39 | + onRefresh, |
| 40 | + isRefetching = false, |
| 41 | + lastUpdated, |
| 42 | +}) => { |
| 43 | + const { t } = useTranslation(); |
| 44 | + const lastUpdatedText = useRelativeTime(lastUpdated, t); |
| 45 | + |
| 46 | + const getPauseReasonText = (): string => { |
| 47 | + switch (pauseReason) { |
| 48 | + case 'hidden': |
| 49 | + return t('refresh.pausedHidden'); |
| 50 | + case 'offline': |
| 51 | + return t('refresh.pausedOffline'); |
| 52 | + case 'manual': |
| 53 | + return t('refresh.pausedManual'); |
| 54 | + default: |
| 55 | + return ''; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div |
| 61 | + style={{ |
| 62 | + display: 'flex', |
| 63 | + alignItems: 'center', |
| 64 | + gap: '12px', |
| 65 | + padding: '8px 12px', |
| 66 | + background: 'var(--bg-card)', |
| 67 | + borderRadius: '8px', |
| 68 | + fontSize: 'var(--text-sm)', |
| 69 | + }} |
| 70 | + > |
| 71 | + <div style={{ display: 'flex', alignItems: 'center', gap: '6px' }}> |
| 72 | + <span |
| 73 | + style={{ |
| 74 | + width: '8px', |
| 75 | + height: '8px', |
| 76 | + borderRadius: '50%', |
| 77 | + background: isPolling |
| 78 | + ? 'var(--accent-green, #22c55e)' |
| 79 | + : isPaused |
| 80 | + ? 'var(--accent-yellow, #eab308)' |
| 81 | + : 'var(--text-secondary)', |
| 82 | + }} |
| 83 | + /> |
| 84 | + <span style={{ color: 'var(--text-secondary)' }}> |
| 85 | + {isPolling |
| 86 | + ? t('refresh.live') |
| 87 | + : isPaused |
| 88 | + ? getPauseReasonText() |
| 89 | + : t('refresh.stopped')} |
| 90 | + </span> |
| 91 | + </div> |
| 92 | + |
| 93 | + {lastUpdatedText && ( |
| 94 | + <span style={{ color: 'var(--text-secondary)', opacity: 0.7 }}> |
| 95 | + {lastUpdatedText} |
| 96 | + </span> |
| 97 | + )} |
| 98 | + |
| 99 | + <div style={{ display: 'flex', gap: '8px', marginLeft: 'auto' }}> |
| 100 | + {isPolling ? ( |
| 101 | + <button |
| 102 | + onClick={onPause} |
| 103 | + style={{ |
| 104 | + padding: '4px 12px', |
| 105 | + background: 'transparent', |
| 106 | + border: '1px solid var(--border-glass)', |
| 107 | + borderRadius: '6px', |
| 108 | + color: 'var(--text-primary)', |
| 109 | + cursor: 'pointer', |
| 110 | + fontSize: 'var(--text-sm)', |
| 111 | + }} |
| 112 | + > |
| 113 | + {t('refresh.pause')} |
| 114 | + </button> |
| 115 | + ) : isPaused && pauseReason === 'manual' ? ( |
| 116 | + <button |
| 117 | + onClick={onResume} |
| 118 | + style={{ |
| 119 | + padding: '4px 12px', |
| 120 | + background: 'var(--accent-cyan)', |
| 121 | + border: 'none', |
| 122 | + borderRadius: '6px', |
| 123 | + color: '#000', |
| 124 | + cursor: 'pointer', |
| 125 | + fontSize: 'var(--text-sm)', |
| 126 | + fontWeight: 500, |
| 127 | + }} |
| 128 | + > |
| 129 | + {t('refresh.resume')} |
| 130 | + </button> |
| 131 | + ) : null} |
| 132 | + |
| 133 | + <button |
| 134 | + onClick={onRefresh} |
| 135 | + disabled={isRefetching} |
| 136 | + style={{ |
| 137 | + padding: '4px 12px', |
| 138 | + background: 'transparent', |
| 139 | + border: '1px solid var(--border-glass)', |
| 140 | + borderRadius: '6px', |
| 141 | + color: 'var(--text-primary)', |
| 142 | + cursor: isRefetching ? 'not-allowed' : 'pointer', |
| 143 | + fontSize: 'var(--text-sm)', |
| 144 | + opacity: isRefetching ? 0.6 : 1, |
| 145 | + }} |
| 146 | + > |
| 147 | + {isRefetching ? t('refresh.refreshing') : t('refresh.refreshNow')} |
| 148 | + </button> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +export default RefreshControl; |
0 commit comments