|
1 | | -import { ConfigProvider, App as AntApp, ThemeConfig } from 'antd' |
2 | | -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 1 | +import { ConfigProvider, App as AntApp, ThemeConfig, Alert, Space } from 'antd' |
| 2 | +import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query' |
3 | 3 | import { RouterProvider } from '@tanstack/react-router' |
4 | 4 | import { router } from './router' |
5 | 5 | import { AuthProvider } from './contexts/AuthContext' |
6 | 6 | import { initializeAnalytics } from './utils/analytics-config' |
| 7 | +import { useState, useEffect } from 'react' |
7 | 8 |
|
8 | 9 | const queryClient = new QueryClient({ |
9 | 10 | defaultOptions: { |
@@ -50,13 +51,113 @@ const theme: ThemeConfig = { |
50 | 51 | // Initialize analytics service |
51 | 52 | initializeAnalytics() |
52 | 53 |
|
| 54 | +interface CronStatusResponse { |
| 55 | + success: boolean |
| 56 | + last_run: string | null |
| 57 | + last_run_unix: number | null |
| 58 | + time_since_last_run: string | null |
| 59 | + time_since_last_run_seconds: number | null |
| 60 | + message?: string |
| 61 | +} |
| 62 | + |
| 63 | +function CronStatusBanner() { |
| 64 | + const [apiEndpoint, setApiEndpoint] = useState<string>('') |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + // Get API endpoint from window object |
| 68 | + setApiEndpoint((window as any).API_ENDPOINT || '') |
| 69 | + }, []) |
| 70 | + |
| 71 | + const { data: cronStatus, isError } = useQuery<CronStatusResponse>({ |
| 72 | + queryKey: ['cronStatus'], |
| 73 | + queryFn: async () => { |
| 74 | + const response = await fetch(`${apiEndpoint}/api/cron.status`) |
| 75 | + if (!response.ok) { |
| 76 | + throw new Error('Failed to fetch cron status') |
| 77 | + } |
| 78 | + return response.json() |
| 79 | + }, |
| 80 | + refetchInterval: 3600000, // Refetch every hour |
| 81 | + enabled: !!apiEndpoint // Only run query if we have an API endpoint |
| 82 | + }) |
| 83 | + |
| 84 | + // Don't show banner if we can't fetch status or if there's an error |
| 85 | + if (!cronStatus || isError) { |
| 86 | + return null |
| 87 | + } |
| 88 | + |
| 89 | + // Check if last run was more than 90 seconds ago |
| 90 | + const needsCronSetup = |
| 91 | + !cronStatus.last_run || |
| 92 | + (cronStatus.time_since_last_run_seconds && cronStatus.time_since_last_run_seconds > 90) |
| 93 | + |
| 94 | + if (!needsCronSetup) { |
| 95 | + return null |
| 96 | + } |
| 97 | + |
| 98 | + const cronCommand = `# Run every minute |
| 99 | +* * * * * curl ${apiEndpoint}/api/cron > /dev/null 2>&1` |
| 100 | + |
| 101 | + return ( |
| 102 | + <div |
| 103 | + style={{ |
| 104 | + position: 'fixed', |
| 105 | + bottom: 16, |
| 106 | + right: 16, |
| 107 | + width: '500px', |
| 108 | + zIndex: 1000 |
| 109 | + }} |
| 110 | + > |
| 111 | + <Alert |
| 112 | + message="Cron Job Setup Required" |
| 113 | + description={ |
| 114 | + <Space direction="vertical" style={{ width: '100%' }}> |
| 115 | + <div> |
| 116 | + {cronStatus.last_run |
| 117 | + ? `Last cron run was ${Math.floor((cronStatus.time_since_last_run_seconds || 0) / 60)} minutes ago. ` |
| 118 | + : 'No cron run detected. '} |
| 119 | + Add this cron job to your server to enable automatic task processing:{' '} |
| 120 | + <a |
| 121 | + href="https://docs.notifuse.com/installation#a-cron-scheduler" |
| 122 | + target="_blank" |
| 123 | + rel="noopener noreferrer" |
| 124 | + style={{ color: '#7763F1' }} |
| 125 | + > |
| 126 | + Learn more |
| 127 | + </a> |
| 128 | + </div> |
| 129 | + <code |
| 130 | + style={{ |
| 131 | + display: 'block', |
| 132 | + padding: '8px', |
| 133 | + backgroundColor: '#f5f5f5', |
| 134 | + border: '1px solid #d9d9d9', |
| 135 | + borderRadius: '4px', |
| 136 | + fontFamily: 'monospace', |
| 137 | + fontSize: '12px', |
| 138 | + whiteSpace: 'pre-wrap' |
| 139 | + }} |
| 140 | + > |
| 141 | + {cronCommand} |
| 142 | + </code> |
| 143 | + </Space> |
| 144 | + } |
| 145 | + type="warning" |
| 146 | + showIcon |
| 147 | + closable |
| 148 | + /> |
| 149 | + </div> |
| 150 | + ) |
| 151 | +} |
| 152 | + |
53 | 153 | export function App() { |
54 | 154 | return ( |
55 | 155 | <QueryClientProvider client={queryClient}> |
56 | 156 | <AuthProvider> |
57 | 157 | <ConfigProvider theme={theme}> |
58 | 158 | <AntApp> |
59 | 159 | <RouterProvider router={router} /> |
| 160 | + <CronStatusBanner /> |
60 | 161 | </AntApp> |
61 | 162 | </ConfigProvider> |
62 | 163 | </AuthProvider> |
|
0 commit comments