-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3479b
commit 6b52a8d
Showing
7 changed files
with
86 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use client" | ||
|
||
import { ServerApi } from "@/app/types/nezha-api" | ||
import getEnv from "@/lib/env-entry" | ||
import { nezhaFetcher } from "@/lib/utils" | ||
import { ReactNode, createContext, useContext, useEffect, useState } from "react" | ||
import useSWR from "swr" | ||
|
||
interface ServerDataWithTimestamp { | ||
timestamp: number | ||
data: ServerApi | ||
} | ||
|
||
interface ServerDataContextType { | ||
data: ServerApi | undefined | ||
error: Error | undefined | ||
isLoading: boolean | ||
history: ServerDataWithTimestamp[] | ||
} | ||
|
||
const ServerDataContext = createContext<ServerDataContextType | undefined>(undefined) | ||
|
||
const MAX_HISTORY_LENGTH = 30 | ||
|
||
export function ServerDataProvider({ children }: { children: ReactNode }) { | ||
const [history, setHistory] = useState<ServerDataWithTimestamp[]>([]) | ||
|
||
const { data, error, isLoading } = useSWR<ServerApi>("/api/server", nezhaFetcher, { | ||
refreshInterval: Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 2000, | ||
dedupingInterval: 1000, | ||
}) | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setHistory((prev) => { | ||
const newHistory = [ | ||
{ | ||
timestamp: Date.now(), | ||
data: data, | ||
}, | ||
...prev, | ||
].slice(0, MAX_HISTORY_LENGTH) | ||
|
||
return newHistory | ||
}) | ||
} | ||
}, [data]) | ||
|
||
return ( | ||
<ServerDataContext.Provider value={{ data, error, isLoading, history }}> | ||
{children} | ||
</ServerDataContext.Provider> | ||
) | ||
} | ||
|
||
export function useServerData() { | ||
const context = useContext(ServerDataContext) | ||
if (context === undefined) { | ||
throw new Error("useServerData must be used within a ServerDataProvider") | ||
} | ||
return context | ||
} |