Files
Mav-Mobile-UI/src/context/SightingFeedContext.tsx

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-08-20 08:27:05 +01:00
import { createContext, useContext, type ReactNode } from "react";
import type { SightingWidgetType } from "../types/types";
import { useSightingFeed } from "../hooks/useSightingFeed";
type SightingFeedContextType = {
2025-08-22 10:38:28 +01:00
sightings: (SightingWidgetType | null | undefined)[];
2025-08-20 08:27:05 +01:00
selectedRef: number | null;
setSelectedRef: (ref: number | null) => void;
effectiveSelected: SightingWidgetType | null;
2025-08-22 10:38:28 +01:00
mostRecent: SightingWidgetType | null;
side: string;
isPending: boolean;
noSighting: boolean;
2025-08-20 08:27:05 +01:00
};
type SightingFeedProviderProps = {
2025-08-22 10:38:28 +01:00
url: string;
2025-08-20 08:27:05 +01:00
children: ReactNode;
2025-08-22 10:38:28 +01:00
side: string;
2025-08-20 08:27:05 +01:00
};
const SightingFeedContext = createContext<SightingFeedContextType | undefined>(
undefined
);
export const SightingFeedProvider = ({
children,
2025-08-22 10:38:28 +01:00
url,
side,
2025-08-20 08:27:05 +01:00
}: SightingFeedProviderProps) => {
2025-08-22 10:38:28 +01:00
const {
sightings,
selectedRef,
setSelectedRef,
effectiveSelected,
mostRecent,
isPending,
noSighting,
} = useSightingFeed(url);
2025-08-20 08:27:05 +01:00
return (
<SightingFeedContext.Provider
2025-08-22 10:38:28 +01:00
value={{
sightings,
selectedRef,
setSelectedRef,
effectiveSelected,
mostRecent,
side,
isPending,
noSighting,
}}
2025-08-20 08:27:05 +01:00
>
{children}
</SightingFeedContext.Provider>
);
};
// eslint-disable-next-line react-refresh/only-export-components
export const useSightingFeedContext = () => {
const ctx = useContext(SightingFeedContext);
if (!ctx)
throw new Error(
"useSightingFeedContext must be used within SightingFeedProvider"
);
return ctx;
};