added sighting feed
This commit is contained in:
50
src/context/SightingFeedContext.tsx
Normal file
50
src/context/SightingFeedContext.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import { createContext, useContext, type ReactNode } from "react";
|
||||
import type { SightingWidgetType } from "../types/types";
|
||||
import { useSightingFeed } from "../hooks/useSightingFeed";
|
||||
|
||||
type SightingFeedContextType = {
|
||||
items: (SightingWidgetType | null | undefined)[];
|
||||
selectedRef: number | null;
|
||||
setSelectedRef: (ref: number | null) => void;
|
||||
effectiveSelected: SightingWidgetType | null;
|
||||
};
|
||||
|
||||
type SightingFeedProviderProps = {
|
||||
baseUrl: string;
|
||||
entries?: number;
|
||||
pollMs?: number;
|
||||
autoSelectLatest?: boolean;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
const SightingFeedContext = createContext<SightingFeedContextType | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
export const SightingFeedProvider = ({
|
||||
baseUrl,
|
||||
entries = 7,
|
||||
pollMs = 500,
|
||||
autoSelectLatest = true,
|
||||
children,
|
||||
}: SightingFeedProviderProps) => {
|
||||
const { items, selectedRef, setSelectedRef, effectiveSelected } =
|
||||
useSightingFeed(baseUrl, { limit: entries, pollMs, autoSelectLatest });
|
||||
return (
|
||||
<SightingFeedContext.Provider
|
||||
value={{ items, selectedRef, setSelectedRef, effectiveSelected }}
|
||||
>
|
||||
{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;
|
||||
};
|
||||
Reference in New Issue
Block a user