乌龟服官服
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import {
|
||||
WOW_COOKIE,
|
||||
WOW_VERSIONS,
|
||||
type WowVersion,
|
||||
} from "@/lib/wow-versions";
|
||||
|
||||
interface WowVersionContextValue {
|
||||
wowVersion: WowVersion;
|
||||
setWowVersion: (next: WowVersion) => void;
|
||||
versions: readonly WowVersion[];
|
||||
}
|
||||
|
||||
const WowVersionContext = createContext<WowVersionContextValue | null>(null);
|
||||
|
||||
export function WowVersionProvider({
|
||||
initial,
|
||||
children,
|
||||
}: {
|
||||
initial: WowVersion;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const [wowVersion, setLocal] = useState<WowVersion>(initial);
|
||||
|
||||
const setWowVersion = useCallback((next: WowVersion) => {
|
||||
setLocal(next);
|
||||
if (typeof document !== "undefined") {
|
||||
document.cookie = `${WOW_COOKIE}=${next}; path=/; max-age=${
|
||||
60 * 60 * 24 * 365
|
||||
}; samesite=lax`;
|
||||
// Server components rely on the cookie — reload so the SSR'd lists refresh.
|
||||
// Use a microtask so React state has a chance to commit first.
|
||||
setTimeout(() => {
|
||||
if (typeof window !== "undefined") {
|
||||
window.location.reload();
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const value = useMemo<WowVersionContextValue>(
|
||||
() => ({ wowVersion, setWowVersion, versions: WOW_VERSIONS }),
|
||||
[wowVersion, setWowVersion]
|
||||
);
|
||||
|
||||
return (
|
||||
<WowVersionContext.Provider value={value}>
|
||||
{children}
|
||||
</WowVersionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useWowVersion() {
|
||||
const ctx = useContext(WowVersionContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useWowVersion must be used inside <WowVersionProvider>");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user