mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-01-15 15:48:05 +00:00
Co-authored-by: Alex Kim <alexkim@Alexs-MacBook-Pro.local> Co-authored-by: Fredrik Burmester <fredrik.burmester@gmail.com> Co-authored-by: Simon-Eklundh <simon.eklundh@proton.me>
38 lines
911 B
TypeScript
38 lines
911 B
TypeScript
import { createContext, type ReactNode, useContext } from "react";
|
|
|
|
const UNSET = Symbol("OfflineModeNotProvided");
|
|
|
|
const OfflineModeContext = createContext<boolean | typeof UNSET>(UNSET);
|
|
|
|
interface OfflineModeProviderProps {
|
|
isOffline: boolean;
|
|
children: ReactNode;
|
|
}
|
|
|
|
/**
|
|
* Provides offline mode state to all child components.
|
|
* Wrap pages that support offline mode with this provider.
|
|
*/
|
|
export function OfflineModeProvider({
|
|
isOffline,
|
|
children,
|
|
}: OfflineModeProviderProps) {
|
|
return (
|
|
<OfflineModeContext.Provider value={isOffline}>
|
|
{children}
|
|
</OfflineModeContext.Provider>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns whether the current view is in offline mode.
|
|
* Must be used within an OfflineModeProvider (set at page level).
|
|
*/
|
|
export function useOfflineMode(): boolean {
|
|
const context = useContext(OfflineModeContext);
|
|
if (context === UNSET) {
|
|
return false;
|
|
}
|
|
return context;
|
|
}
|