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

142 lines
3.9 KiB
TypeScript
Raw Normal View History

import { useEffect, useMemo, useRef, useState } from "react";
import { useQuery } from "@tanstack/react-query";
2025-09-16 14:20:38 +01:00
import type { SightingType } from "../types/types";
import { useSoundOnChange } from "react-sounds";
import { useSoundContext } from "../context/SoundContext";
import { getSoundFileURL } from "../utils/utils";
import switchSound from "../assets/sounds/ui/switch.mp3";
2025-08-20 08:27:05 +01:00
async function fetchSighting(
url: string | undefined,
ref: number
): Promise<SightingType> {
const res = await fetch(`${url}${ref}`);
2025-08-22 10:38:28 +01:00
if (!res.ok) throw new Error(String(res.status));
return res.json();
2025-08-22 10:38:28 +01:00
}
export function useSightingFeed(url: string | undefined) {
const { state } = useSoundContext();
2025-09-16 14:20:38 +01:00
const [sightings, setSightings] = useState<SightingType[]>([]);
2025-08-20 08:27:05 +01:00
const [selectedRef, setSelectedRef] = useState<number | null>(null);
const [sessionStarted, setSessionStarted] = useState(false);
const [sessionList, setSessionList] = useState<SightingType[]>([]);
const mostRecent = sightings[0] ?? null;
const latestRef = mostRecent?.ref ?? null;
2025-09-12 08:21:52 +01:00
const [selectedSighting, setSelectedSighting] = useState<SightingType | null>(
null
);
const first = useRef(true);
const trigger = useMemo(() => {
if (latestRef == null) return null;
if (first.current) {
first.current = false;
return Symbol("skip");
}
return latestRef;
}, [latestRef]);
const soundSrc = useMemo(() => {
return getSoundFileURL(state.sightingSound) ?? switchSound;
}, [state.sightingSound]);
2025-08-20 08:27:05 +01:00
//use latestref instead of trigger to revert back
useSoundOnChange(soundSrc, trigger, {
volume: 1,
});
const currentRef = useRef<number>(-1);
const lastValidTimestamp = useRef<number>(Date.now());
2025-08-20 08:27:05 +01:00
const query = useQuery({
queryKey: ["sighting-feed", url],
enabled: !!url,
queryFn: () => fetchSighting(url, currentRef.current),
refetchInterval: (q) => {
const data = q.state.data as SightingType | undefined;
const now = Date.now();
if (data && data.ref !== -1) {
lastValidTimestamp.current = now;
return 100;
2025-08-20 08:27:05 +01:00
}
if (now - lastValidTimestamp.current > 60_000) {
currentRef.current = -1;
lastValidTimestamp.current = now;
}
return 400;
},
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
retry: false,
staleTime: 0,
});
useEffect(() => {
if (sessionStarted) {
if (!mostRecent) return;
setSessionList([...sessionList, mostRecent]);
}
}, [mostRecent, sessionList, sessionStarted]);
useEffect(() => {
const data = query.data;
if (!data) return;
const now = Date.now();
2025-08-22 10:38:28 +01:00
if (data.ref === -1) {
// setSightings((prev) => {
// if (prev[0]?.ref === data.ref) return prev;
// const dedupPrev = prev.filter((s) => s.ref !== data.ref);
// return [data, ...dedupPrev].slice(0, 7);
// });
2025-08-22 10:38:28 +01:00
return;
}
// if (Notification.permission === "granted") {
// new Notification("New Sighting!", {
// body: `Ref: ${data.ref}`,
// icon: "/MAV-blue.svg",
// });
// }
currentRef.current = data.ref;
lastValidTimestamp.current = now;
setSightings((prev) => {
if (prev[0]?.ref === data.ref) return prev;
const dedupPrev = prev.filter((s) => s.ref !== data.ref);
return [data, ...dedupPrev].slice(0, 7);
});
setSelectedRef(data.ref);
}, [query.data]);
useEffect(() => {
if (query.error) {
// console.error("Sighting feed error:", query.error);
}
}, [query.error]);
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,
2025-09-12 08:21:52 +01:00
selectedSighting,
sessionList,
sessionStarted,
setSessionStarted,
setSelectedSighting,
data: query.data,
isLoading: query.isLoading,
isFetching: query.isFetching,
isError: query.isError,
error: query.error as Error | null,
refetch: query.refetch,
2025-08-20 08:27:05 +01:00
};
}