2025-09-30 14:51:37 +01:00
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
2025-10-15 11:00:52 +01:00
|
|
|
import { Query, useQuery } from "@tanstack/react-query";
|
2025-09-16 14:20:38 +01:00
|
|
|
import type { SightingType } from "../types/types";
|
2025-09-23 13:03:54 +01:00
|
|
|
import { useSoundOnChange } from "react-sounds";
|
2025-09-30 14:51:37 +01:00
|
|
|
import { useSoundContext } from "../context/SoundContext";
|
2025-10-01 15:21:07 +01:00
|
|
|
import { getSoundFileURL } from "../utils/utils";
|
2025-09-30 15:32:00 +01:00
|
|
|
import switchSound from "../assets/sounds/ui/switch.mp3";
|
2025-08-20 08:27:05 +01:00
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
async function fetchSighting(url: string | undefined, ref: number): Promise<SightingType> {
|
2025-10-06 14:21:56 +01:00
|
|
|
const res = await fetch(`${url}${ref}`, {
|
|
|
|
|
signal: AbortSignal.timeout(5000),
|
|
|
|
|
});
|
2025-08-22 10:38:28 +01:00
|
|
|
if (!res.ok) throw new Error(String(res.status));
|
2025-09-17 11:39:26 +01:00
|
|
|
return res.json();
|
2025-08-22 10:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 10:38:49 +01:00
|
|
|
export function useSightingFeed(url: string | undefined) {
|
2025-10-15 11:00:52 +01:00
|
|
|
const { state, audioArmed } = 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);
|
2025-09-25 10:38:49 +01:00
|
|
|
const [sessionStarted, setSessionStarted] = useState(false);
|
2025-10-15 11:00:52 +01:00
|
|
|
const [selectedSighting, setSelectedSighting] = useState<SightingType | null>(null);
|
|
|
|
|
|
2025-09-17 11:39:26 +01:00
|
|
|
const mostRecent = sightings[0] ?? null;
|
2025-09-23 13:03:54 +01:00
|
|
|
const latestRef = mostRecent?.ref ?? null;
|
2025-10-15 11:00:52 +01:00
|
|
|
|
2025-10-01 15:21:07 +01:00
|
|
|
const first = useRef(true);
|
2025-10-15 11:00:52 +01:00
|
|
|
const lastSoundAt = useRef(0);
|
|
|
|
|
const COOLDOWN_MS = 1500;
|
|
|
|
|
const currentRef = useRef<number>(-1);
|
|
|
|
|
const lastValidTimestamp = useRef<number>(Date.now());
|
2025-10-01 15:21:07 +01:00
|
|
|
|
|
|
|
|
const trigger = useMemo(() => {
|
2025-10-15 11:00:52 +01:00
|
|
|
if (latestRef == null || !audioArmed) return null;
|
2025-10-01 15:21:07 +01:00
|
|
|
if (first.current) {
|
|
|
|
|
first.current = false;
|
|
|
|
|
return Symbol("skip");
|
|
|
|
|
}
|
2025-10-15 11:00:52 +01:00
|
|
|
const now = Date.now();
|
|
|
|
|
if (now - lastSoundAt.current < COOLDOWN_MS) return Symbol("skip");
|
|
|
|
|
lastSoundAt.current = now;
|
2025-10-01 15:21:07 +01:00
|
|
|
return latestRef;
|
2025-10-15 11:00:52 +01:00
|
|
|
}, [audioArmed, latestRef]);
|
|
|
|
|
|
2025-09-30 14:51:37 +01:00
|
|
|
const soundSrc = useMemo(() => {
|
2025-10-08 11:08:41 +01:00
|
|
|
return getSoundFileURL(state?.sightingSound) ?? switchSound;
|
2025-09-30 14:51:37 +01:00
|
|
|
}, [state.sightingSound]);
|
2025-08-20 08:27:05 +01:00
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
function refetchInterval(query: Query<SightingType, Error, SightingType, (string | undefined)[]>) {
|
|
|
|
|
if (!query) return;
|
|
|
|
|
const data = query.state.data as SightingType | undefined;
|
|
|
|
|
const now = Date.now();
|
2025-09-23 13:03:54 +01:00
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
if (data && data.ref !== -1) {
|
|
|
|
|
lastValidTimestamp.current = now;
|
|
|
|
|
return 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (now - lastValidTimestamp.current > 60_000) {
|
|
|
|
|
currentRef.current = -1;
|
|
|
|
|
lastValidTimestamp.current = now;
|
|
|
|
|
}
|
|
|
|
|
return 400;
|
|
|
|
|
}
|
2025-08-20 08:27:05 +01:00
|
|
|
|
2025-09-17 11:39:26 +01:00
|
|
|
const query = useQuery({
|
|
|
|
|
queryKey: ["sighting-feed", url],
|
|
|
|
|
enabled: !!url,
|
|
|
|
|
queryFn: () => fetchSighting(url, currentRef.current),
|
2025-10-15 11:00:52 +01:00
|
|
|
refetchInterval: (q) => refetchInterval(q),
|
2025-09-17 11:39:26 +01:00
|
|
|
refetchIntervalInBackground: true,
|
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
|
retry: false,
|
|
|
|
|
staleTime: 0,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
//use latestref instead of trigger to revert back
|
2025-10-17 16:12:02 +01:00
|
|
|
|
2025-10-15 11:00:52 +01:00
|
|
|
useSoundOnChange(soundSrc, trigger, {
|
2025-10-17 16:12:02 +01:00
|
|
|
volume: state.sightingVolume,
|
2025-10-15 11:00:52 +01:00
|
|
|
initial: false,
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-17 11:39:26 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const data = query.data;
|
2025-10-15 11:00:52 +01:00
|
|
|
if (!data || data.ref === -1) return;
|
2025-09-17 11:39:26 +01:00
|
|
|
|
|
|
|
|
const now = Date.now();
|
2025-08-22 10:38:28 +01:00
|
|
|
|
2025-09-17 11:39:26 +01:00
|
|
|
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]);
|
|
|
|
|
|
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,
|
2025-09-25 10:38:49 +01:00
|
|
|
sessionStarted,
|
|
|
|
|
setSessionStarted,
|
2025-09-17 11:39:26 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
}
|