|
|
@ -6,6 +6,7 @@ import { apiFetch, incrementMetric, setUnraidCsrfToken } from "src/api"
|
|
|
|
import { ExitNode, noExitNode, runAsExitNode } from "src/hooks/exit-nodes"
|
|
|
|
import { ExitNode, noExitNode, runAsExitNode } from "src/hooks/exit-nodes"
|
|
|
|
import { VersionInfo } from "src/hooks/self-update"
|
|
|
|
import { VersionInfo } from "src/hooks/self-update"
|
|
|
|
import { assertNever } from "src/utils/util"
|
|
|
|
import { assertNever } from "src/utils/util"
|
|
|
|
|
|
|
|
import useSWR from "swr"
|
|
|
|
|
|
|
|
|
|
|
|
export type NodeData = {
|
|
|
|
export type NodeData = {
|
|
|
|
Profile: UserProfile
|
|
|
|
Profile: UserProfile
|
|
|
@ -120,19 +121,12 @@ type RoutesPOSTData = {
|
|
|
|
|
|
|
|
|
|
|
|
// useNodeData returns basic data about the current node.
|
|
|
|
// useNodeData returns basic data about the current node.
|
|
|
|
export default function useNodeData() {
|
|
|
|
export default function useNodeData() {
|
|
|
|
const [data, setData] = useState<NodeData>()
|
|
|
|
const { data, mutate } = useSWR<NodeData>("/data")
|
|
|
|
const [isPosting, setIsPosting] = useState<boolean>(false)
|
|
|
|
const [isPosting, setIsPosting] = useState<boolean>(false)
|
|
|
|
|
|
|
|
|
|
|
|
const refreshData = useCallback(
|
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
() => setUnraidCsrfToken(data?.IsUnraid ? data.UnraidToken : undefined),
|
|
|
|
apiFetch("/data", "GET")
|
|
|
|
[data]
|
|
|
|
.then((r) => r.json())
|
|
|
|
|
|
|
|
.then((d: NodeData) => {
|
|
|
|
|
|
|
|
setData(d)
|
|
|
|
|
|
|
|
setUnraidCsrfToken(d.IsUnraid ? d.UnraidToken : undefined)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((error) => console.error(error)),
|
|
|
|
|
|
|
|
[setData]
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const prefsPATCH = useCallback(
|
|
|
|
const prefsPATCH = useCallback(
|
|
|
@ -147,12 +141,12 @@ export default function useNodeData() {
|
|
|
|
// then make the prefs PATCH. If the request fails,
|
|
|
|
// then make the prefs PATCH. If the request fails,
|
|
|
|
// data will be updated to it's previous value in
|
|
|
|
// data will be updated to it's previous value in
|
|
|
|
// onComplete below.
|
|
|
|
// onComplete below.
|
|
|
|
setData(optimisticUpdates)
|
|
|
|
mutate(optimisticUpdates, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const onComplete = () => {
|
|
|
|
const onComplete = () => {
|
|
|
|
setIsPosting(false)
|
|
|
|
setIsPosting(false)
|
|
|
|
refreshData() // refresh data after PATCH finishes
|
|
|
|
mutate() // refresh data after PATCH finishes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return apiFetch("/local/v0/prefs", "PATCH", d)
|
|
|
|
return apiFetch("/local/v0/prefs", "PATCH", d)
|
|
|
@ -163,7 +157,7 @@ export default function useNodeData() {
|
|
|
|
throw err
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[setIsPosting, refreshData, setData, data]
|
|
|
|
[data, mutate]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const routesPOST = useCallback(
|
|
|
|
const routesPOST = useCallback(
|
|
|
@ -171,7 +165,7 @@ export default function useNodeData() {
|
|
|
|
setIsPosting(true)
|
|
|
|
setIsPosting(true)
|
|
|
|
const onComplete = () => {
|
|
|
|
const onComplete = () => {
|
|
|
|
setIsPosting(false)
|
|
|
|
setIsPosting(false)
|
|
|
|
refreshData() // refresh data after POST finishes
|
|
|
|
mutate() // refresh data after POST finishes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const updateMetrics = () => {
|
|
|
|
const updateMetrics = () => {
|
|
|
|
// only update metrics if values have changed
|
|
|
|
// only update metrics if values have changed
|
|
|
@ -195,26 +189,7 @@ export default function useNodeData() {
|
|
|
|
throw err
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[setIsPosting, refreshData, data?.AdvertisingExitNode]
|
|
|
|
[mutate, data?.AdvertisingExitNode]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
|
|
|
() => {
|
|
|
|
|
|
|
|
// Initial data load.
|
|
|
|
|
|
|
|
refreshData()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Refresh on browser tab focus.
|
|
|
|
|
|
|
|
const onVisibilityChange = () => {
|
|
|
|
|
|
|
|
document.visibilityState === "visible" && refreshData()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("visibilitychange", onVisibilityChange)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
|
|
// Cleanup browser tab listener.
|
|
|
|
|
|
|
|
window.removeEventListener("visibilitychange", onVisibilityChange)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
// Run once.
|
|
|
|
|
|
|
|
[refreshData]
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const nodeUpdaters: NodeUpdaters = useMemo(
|
|
|
|
const nodeUpdaters: NodeUpdaters = useMemo(
|
|
|
@ -245,5 +220,5 @@ export default function useNodeData() {
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return { data, refreshData, nodeUpdaters, isPosting }
|
|
|
|
return { data, refreshData: mutate, nodeUpdaters, isPosting }
|
|
|
|
}
|
|
|
|
}
|
|
|
|