2025-12-19 16:04:06 +00:00
|
|
|
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);
|
2025-12-22 12:19:00 +00:00
|
|
|
const isLoading = videoFeedQuery?.isPending;
|
2025-12-19 16:04:06 +00:00
|
|
|
|
|
|
|
|
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]);
|
2025-12-22 12:19:00 +00:00
|
|
|
return { sightingList, isLoading };
|
2025-12-19 16:04:06 +00:00
|
|
|
};
|