Add video feed feature with related components and hooks
- Implemented VideoFeed component to display sightings. - Created useSightingList and useVideoFeed hooks for data fetching and state management. - Added AppProviders for context management. - Updated Dashboard to include VideoFeed. - Introduced types for sightings in utils/types.tsx. - Added Header and Footer components for layout. - Configured React Query for data handling.
This commit is contained in:
26
src/features/dashboard/hooks/useSightingList.ts
Normal file
26
src/features/dashboard/hooks/useSightingList.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useVideoFeed } from "./useVideoFeed";
|
||||
import type { SightingType } from "../../../utils/types";
|
||||
|
||||
export const useSightingList = () => {
|
||||
const [sightingList, setSightingList] = useState<SightingType[]>([]);
|
||||
const { videoFeedQuery } = useVideoFeed();
|
||||
const latestSighting = videoFeedQuery?.data;
|
||||
const lastProcessedRef = useRef<number>(-1);
|
||||
|
||||
useEffect(() => {
|
||||
if (!latestSighting || latestSighting.ref === undefined || latestSighting.ref === -1) return;
|
||||
|
||||
if (latestSighting.ref !== lastProcessedRef.current) {
|
||||
lastProcessedRef.current = latestSighting.ref;
|
||||
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
setSightingList((prevList) => {
|
||||
if (prevList[0]?.ref === latestSighting.ref) return prevList;
|
||||
const dedupPrev = prevList.filter((s) => s.ref !== latestSighting.ref);
|
||||
return [latestSighting, ...dedupPrev].slice(0, 10);
|
||||
});
|
||||
}
|
||||
}, [latestSighting, latestSighting?.ref]);
|
||||
return { sightingList };
|
||||
};
|
||||
Reference in New Issue
Block a user