SkateSpots/src/hooks/useTours.ts
SkateSpots c79373ed05 feat(offline): the map survives a dead relay (and says so) + skater@skate-spots.org
Relay outages no longer look like 'no spots'. querySkateContent swallowed every
relay error and returned [], so a dead relay would blank the map AND overwrite
good cached data with an empty list (then persist it). Now:

- querySkateContent/queryCanonical take an opt-in 'requireAny' that throws when
  NO relay answered. Only the map's own feeds use it, so the blast radius is
  small (79 other call sites keep today's behaviour).
- useSkateSpots/useSkateSpot fall back to the local IndexedDB cache on failure,
  so the map still works at the park; they only error when nothing is saved.
- The persister refuses to persist an empty array, so an outage can't wipe the
  last-known map on the next cold start.
- MapPage shows an honest 'Offline · showing saved spots' banner (reusing the
  existing relayProbe/useRelayStatus) and a 'Can't reach the relays' card
  instead of falsely claiming 'No spots mapped yet'.

Also seeds skater@skate-spots.org for the second admin (caba497f…) and reserves
the name so nobody else can claim it.
2026-09-28 12:48:54 +07:00

130 lines
3.6 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { SKATE_TOUR_KIND, parseSkateTour, isFeaturedActive, type SkateTour } from '@/lib/types';
import { useSkateRelays } from '@/hooks/useSkateRelays';
import { useAdminModeration, filterAdminHidden } from '@/hooks/useAdminModeration';
/** Sort featured (Pro) tours first, then newest. */
function sortTours(a: SkateTour, b: SkateTour): number {
const af = isFeaturedActive(a.featuredUntil) ? 1 : 0;
const bf = isFeaturedActive(b.featuredUntil) ? 1 : 0;
if (af !== bf) return bf - af;
return b.createdAt - a.createdAt;
}
const STALE_TIME = 5 * 60 * 1000; // 5 min
const GC_TIME = 30 * 60 * 1000; // 30 min
const TIMEOUT = 8000;
export function useTours(limit = 100, options: { includeHidden?: boolean } = {}) {
const { includeHidden = false } = options;
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-tours', limit, getQueryKeySuffix()],
// `includeHidden`: admin tooling only (review queue + undo).
select: (tours: SkateTour[]) =>
includeHidden ? tours : filterAdminHidden(tours, SKATE_TOUR_KIND, hidden),
queryFn: async () => {
const signal = AbortSignal.timeout(TIMEOUT);
const events = await querySkateContent(
[
{
kinds: [SKATE_TOUR_KIND],
'#t': ['skate-tour'],
limit,
},
],
{ signal, requireAny: true }
);
const tours: SkateTour[] = [];
for (const event of events) {
const tour = parseSkateTour(event);
if (tour) tours.push(tour);
}
return tours.sort(sortTours);
},
staleTime: STALE_TIME,
gcTime: GC_TIME,
refetchOnMount: true,
});
}
export function useTour(identifier: string, pubkey?: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-tour', identifier, pubkey, getQueryKeySuffix()],
select: (tour: SkateTour | null) =>
tour && hidden.has({ coord: `${SKATE_TOUR_KIND}:${tour.pubkey}:${tour.identifier}`, id: tour.id })
? null
: tour,
queryFn: async () => {
const signal = AbortSignal.timeout(TIMEOUT);
const filter: {
kinds: number[];
'#d': string[];
authors?: string[];
limit: number;
} = {
kinds: [SKATE_TOUR_KIND],
'#d': [identifier],
limit: 1,
};
if (pubkey) {
filter.authors = [pubkey];
}
const events = await querySkateContent([filter], { signal });
if (events.length === 0) return null;
return parseSkateTour(events[0]);
},
enabled: !!identifier,
staleTime: STALE_TIME,
gcTime: GC_TIME,
refetchOnMount: true,
});
}
export function useUserTours(pubkey: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['user-skate-tours', pubkey, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(TIMEOUT);
const events = await querySkateContent(
[
{
kinds: [SKATE_TOUR_KIND],
authors: [pubkey],
'#t': ['skate-tour'],
limit: 50,
},
],
{ signal }
);
const tours: SkateTour[] = [];
for (const event of events) {
const tour = parseSkateTour(event);
if (tour) tours.push(tour);
}
return tours.sort((a, b) => b.createdAt - a.createdAt);
},
enabled: !!pubkey,
staleTime: STALE_TIME,
gcTime: GC_TIME,
refetchOnMount: true,
});
}