uprava FE pro multisite
This commit is contained in:
@@ -1,26 +1,52 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
import { useSiteSelection } from '../context/SiteSelectionContext'
|
||||
import { getJson } from '../api/postgrest'
|
||||
import type { SiteStatusRow } from '../types/ems'
|
||||
|
||||
const POLL_MS = 30_000
|
||||
|
||||
export function useSiteStatus() {
|
||||
const { selectedSiteId, ready: selectionReady, error: selectionError } = useSiteSelection()
|
||||
const [row, setRow] = useState<SiteStatusRow | null>(null)
|
||||
const [ready, setReady] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [statusReady, setStatusReady] = useState(false)
|
||||
const [fetchError, setFetchError] = useState<string | null>(null)
|
||||
const selectedSiteIdRef = useRef(selectedSiteId)
|
||||
selectedSiteIdRef.current = selectedSiteId
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const rows = await getJson<SiteStatusRow[]>('/vw_site_status')
|
||||
setRow(Array.isArray(rows) && rows.length > 0 ? rows[0]! : null)
|
||||
setError(null)
|
||||
} catch {
|
||||
setRow(null)
|
||||
setError('Stav lokality se nepodařilo načíst')
|
||||
} finally {
|
||||
setReady(true)
|
||||
if (!selectionReady) {
|
||||
return
|
||||
}
|
||||
}, [])
|
||||
if (selectedSiteId == null) {
|
||||
setRow(null)
|
||||
setFetchError(null)
|
||||
setStatusReady(true)
|
||||
return
|
||||
}
|
||||
const sid = selectedSiteId
|
||||
try {
|
||||
const rows = await getJson<SiteStatusRow[]>('/vw_site_status', {
|
||||
site_id: `eq.${sid}`,
|
||||
})
|
||||
if (selectedSiteIdRef.current !== sid) return
|
||||
setRow(Array.isArray(rows) && rows.length > 0 ? rows[0]! : null)
|
||||
setFetchError(null)
|
||||
} catch {
|
||||
if (selectedSiteIdRef.current !== sid) return
|
||||
setRow(null)
|
||||
setFetchError('Stav lokality se nepodařilo načíst')
|
||||
} finally {
|
||||
if (selectedSiteIdRef.current === sid) {
|
||||
setStatusReady(true)
|
||||
}
|
||||
}
|
||||
}, [selectionReady, selectedSiteId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectionReady) return
|
||||
setStatusReady(false)
|
||||
}, [selectionReady, selectedSiteId])
|
||||
|
||||
useEffect(() => {
|
||||
void load()
|
||||
@@ -28,6 +54,9 @@ export function useSiteStatus() {
|
||||
return () => window.clearInterval(id)
|
||||
}, [load])
|
||||
|
||||
const ready = selectionReady && statusReady
|
||||
const error = selectionError ?? fetchError
|
||||
|
||||
const hasTelemetry =
|
||||
row != null &&
|
||||
(row.pv_power_w != null ||
|
||||
|
||||
Reference in New Issue
Block a user