code fixes and adding modal

This commit is contained in:
2025-09-12 08:21:52 +01:00
parent fae17b88a4
commit d03f73f751
24 changed files with 524 additions and 303 deletions

View File

@@ -1,7 +1,10 @@
import { useEffect, useRef, useState } from "react";
import type { SightingWidgetType } from "../types/types";
import type { SightingType, SightingWidgetType } from "../types/types";
async function fetchSighting(url: string, ref: number): Promise<SightingWidgetType> {
async function fetchSighting(
url: string,
ref: number
): Promise<SightingWidgetType> {
const res = await fetch(`${url}${ref}`);
if (!res.ok) throw new Error(String(res.status));
return await res.json();
@@ -11,6 +14,9 @@ export function useSightingFeed(url: string) {
const [sightings, setSightings] = useState<SightingWidgetType[]>([]);
const [selectedRef, setSelectedRef] = useState<number | null>(null);
const [mostRecent, setMostRecent] = useState<SightingWidgetType | null>(null);
const [selectedSighting, setSelectedSighting] = useState<SightingType | null>(
null
);
const currentRef = useRef<number>(-1);
const pollingTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -35,7 +41,7 @@ export function useSightingFeed(url: string) {
currentRef.current = data.ref;
lastValidTimestamp.current = now;
setSightings(prev => {
setSightings((prev) => {
const updated = [data, ...prev].slice(0, 7);
return updated;
});
@@ -58,13 +64,15 @@ export function useSightingFeed(url: string) {
};
}, [url]);
const selected = sightings.find(s => s?.ref === selectedRef) ?? mostRecent;
// const selected = sightings.find(s => s?.ref === selectedRef) ?? mostRecent;
return {
sightings,
selectedRef,
setSelectedRef,
mostRecent,
effectiveSelected: selected,
setSelectedSighting,
selectedSighting,
// effectiveSelected: selected,
};
}