Skip to content

Commit f20da14

Browse files
committed
ack PR comments
1 parent 7b0aaec commit f20da14

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

apps/sim/app/(landing)/components/footer/components/status-indicator.tsx

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use client'
22

3-
import { useCallback, useEffect, useState } from 'react'
43
import Link from 'next/link'
54
import { StatusDotIcon } from '@/components/icons'
6-
import type { StatusResponse, StatusType } from '@/app/api/status/types'
7-
8-
const POLLING_INTERVAL = 60000
5+
import type { StatusType } from '@/app/api/status/types'
6+
import { useStatus } from '@/hooks/queries/status'
97

108
const STATUS_COLORS: Record<StatusType, string> = {
119
operational: 'text-[#10B981] hover:text-[#059669]',
@@ -17,45 +15,15 @@ const STATUS_COLORS: Record<StatusType, string> = {
1715
}
1816

1917
export default function StatusIndicator() {
20-
const [status, setStatus] = useState<StatusType>('loading')
21-
const [message, setMessage] = useState<string>('Checking Status...')
22-
const [statusUrl, setStatusUrl] = useState<string>('https://status.sim.ai')
23-
24-
const fetchStatus = useCallback(async () => {
25-
try {
26-
const response = await fetch('/api/status')
27-
28-
if (!response.ok) {
29-
throw new Error('Failed to fetch status')
30-
}
31-
32-
const data: StatusResponse = await response.json()
33-
34-
setStatus(data.status)
35-
setMessage(data.message)
36-
setStatusUrl(data.url)
37-
} catch (error) {
38-
console.error('Error fetching status:', error)
39-
setStatus('error')
40-
setMessage('Status Unknown')
41-
}
42-
}, [])
43-
44-
useEffect(() => {
45-
fetchStatus()
46-
47-
const poll = () => {
48-
fetchStatus().finally(() => {
49-
setTimeout(poll, POLLING_INTERVAL)
50-
})
51-
}
52-
53-
const timeoutId = setTimeout(poll, POLLING_INTERVAL)
54-
55-
return () => {
56-
clearTimeout(timeoutId)
57-
}
58-
}, [fetchStatus])
18+
const { data, isLoading, isError } = useStatus()
19+
20+
const status = isLoading ? 'loading' : isError ? 'error' : data?.status || 'error'
21+
const message = isLoading
22+
? 'Checking Status...'
23+
: isError
24+
? 'Status Unknown'
25+
: data?.message || 'Status Unknown'
26+
const statusUrl = data?.url || 'https://status.sim.ai'
5927

6028
return (
6129
<Link

apps/sim/app/api/status/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { NextResponse } from 'next/server'
2-
import type { IncidentIOWidgetResponse, StatusResponse, StatusType } from './types'
2+
import { createLogger } from '@/lib/logs/console/logger'
3+
import type { IncidentIOWidgetResponse, StatusResponse, StatusType } from '@/app/api/status/types'
4+
5+
const logger = createLogger('StatusAPI')
36

47
let cachedResponse: { data: StatusResponse; timestamp: number } | null = null
58
const CACHE_TTL = 2 * 60 * 1000
@@ -75,7 +78,7 @@ export async function GET() {
7578
},
7679
})
7780
} catch (error) {
78-
console.error('Error fetching status from incident.io:', error)
81+
logger.error('Error fetching status from incident.io:', error)
7982

8083
const errorResponse: StatusResponse = {
8184
status: 'error',

apps/sim/hooks/queries/status.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useQuery } from '@tanstack/react-query'
2+
import type { StatusResponse } from '@/app/api/status/types'
3+
4+
/**
5+
* Query key factories for status-related queries
6+
* This ensures consistent cache invalidation across the app
7+
*/
8+
export const statusKeys = {
9+
all: ['status'] as const,
10+
current: () => [...statusKeys.all, 'current'] as const,
11+
}
12+
13+
/**
14+
* Fetch current system status from the API
15+
* The API proxies incident.io and caches for 2 minutes server-side
16+
*/
17+
async function fetchStatus(): Promise<StatusResponse> {
18+
const response = await fetch('/api/status')
19+
20+
if (!response.ok) {
21+
throw new Error('Failed to fetch status')
22+
}
23+
24+
return response.json()
25+
}
26+
27+
/**
28+
* Hook to fetch current system status
29+
* - Polls every 60 seconds to keep status up-to-date
30+
* - Refetches when user returns to tab for immediate updates
31+
* - Caches for 1 minute to reduce unnecessary requests
32+
*/
33+
export function useStatus() {
34+
return useQuery({
35+
queryKey: statusKeys.current(),
36+
queryFn: fetchStatus,
37+
staleTime: 60 * 1000, // 1 minute
38+
refetchInterval: 60 * 1000, // Poll every 60 seconds
39+
refetchOnWindowFocus: true, // Refetch when user returns to tab
40+
retry: 2,
41+
})
42+
}

0 commit comments

Comments
 (0)