Files
Mav-Mobile-UI/src/hooks/useSightingFeed.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-08-20 08:27:05 +01:00
import { useEffect, useMemo, useRef, useState } from "react";
import type { SightingWidgetType } from "../types/types";
2025-08-22 10:38:28 +01:00
import { useQuery } from "@tanstack/react-query";
2025-08-20 08:27:05 +01:00
2025-08-22 10:38:28 +01:00
// const url = `http://100.82.205.44/SightingListFront/sightingSummary?mostRecentRef=-1`;
async function fetchSighting(url: string, ref: number, signal?: AbortSignal) {
const dynamicUrl = `${url}${ref}`;
const res = await fetch(dynamicUrl, { signal });
if (!res.ok) throw new Error(String(res.status));
return (await res.json()) as SightingWidgetType;
}
export function useSightingFeed(url: string) {
const [sightings, setSightings] = useState<SightingWidgetType[]>(
() => Array(7).fill(null) as unknown as SightingWidgetType[]
2025-08-20 08:27:05 +01:00
);
2025-08-22 10:38:28 +01:00
const [noSighting, setNoSighting] = useState(false);
2025-08-20 08:27:05 +01:00
const [selectedRef, setSelectedRef] = useState<number | null>(null);
const [mostRecent, setMostRecent] = useState<SightingWidgetType | null>(null);
const mostRecentRef = useRef<number>(-1);
2025-08-22 10:38:28 +01:00
const lastSeenRef = useRef<number | null>(null);
2025-08-20 08:27:05 +01:00
2025-08-22 10:38:28 +01:00
const { data, isPending } = useQuery({
queryKey: ["sighting"],
queryFn: ({ signal }) => fetchSighting(url, mostRecentRef.current, signal),
refetchInterval: 200,
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
staleTime: 0,
notifyOnChangeProps: ["data"],
});
2025-08-20 08:27:05 +01:00
useEffect(() => {
2025-08-22 10:38:28 +01:00
if (!data) return;
2025-08-20 08:27:05 +01:00
2025-08-22 10:38:28 +01:00
if (data.ref === -1) {
setNoSighting(true);
} else {
setNoSighting(false);
}
if (data.ref === lastSeenRef.current) return; // duplicate payload → do nothing
lastSeenRef.current = data.ref;
2025-08-20 08:27:05 +01:00
2025-08-22 10:38:28 +01:00
setSightings((prev) => {
const existing = prev.find((p) => p?.ref === data.ref);
const next = existing
? prev
: [data, ...prev.filter(Boolean)].slice(0, 7);
2025-08-20 08:27:05 +01:00
2025-08-22 10:38:28 +01:00
const stillHasSelection =
selectedRef != null && next.some((s) => s?.ref === selectedRef);
if (!stillHasSelection) {
setSelectedRef(data.ref);
2025-08-20 08:27:05 +01:00
}
2025-08-22 10:38:28 +01:00
return next;
});
setMostRecent(sightings[0]);
mostRecentRef.current = data.ref ?? -1;
}, [data, selectedRef, sightings]);
const selected = useMemo(
() =>
selectedRef == null
? null
: sightings.find((s) => s?.ref === selectedRef) ?? null,
[sightings, selectedRef]
);
const effectiveSelected = selected ?? mostRecent ?? null;
2025-08-20 08:27:05 +01:00
return {
2025-08-22 10:38:28 +01:00
sightings,
2025-08-20 08:27:05 +01:00
selectedRef,
setSelectedRef,
mostRecent,
effectiveSelected,
mostRecentRef,
2025-08-22 10:38:28 +01:00
isPending,
noSighting,
2025-08-20 08:27:05 +01:00
};
}