SkateSpots/src/hooks/useSkateSpots.ts
SkateSpots e44f8b81a7 feat(moderation): reversible verdicts (undo) + community photos as a signal
Undo (latest verdict wins):
- MODERATION_LABEL_CLEARED = 'cleared' — the neutral undo verdict.
- useAdminModeration no longer UNIONS labels (which made a hide permanent in the
  UI); it keeps the NEWEST verdict per target (created_at, with device-local
  verdicts overlaid by timestamp), so 'cleared' supersedes an earlier
  'hidden'/'verified'. useAdminClear() publishes it.
- Local mirror is now a verdict map (skatespots_local_admin_verdicts_v1: key ->
  {verdict, at}) with migration from the legacy hidden/confirmed arrays.
- useSkateSpots(limit, { includeHidden: true }) — ADMIN TOOLING ONLY: without it
  a hidden spot leaves the list, so a verdict could never be undone. The review
  queue uses it, keeps actioned rows (Hidden/Confirmed badge + Undo), and sorts
  un-actioned first.

Community photos as a signal:
- useBatchSpotPhotos: ONE #a query for every spot (the single-spot hook already
  queried by #a, so batching is free), counting only photos from people OTHER
  than the mapper. Folded into the 'no community signals' flag.

Verified by seeding local verdicts: none -> flags + Confirm/Hide; hidden ->
Undo; verified -> Undo; cleared -> back to neutral. tsc/eslint clean, 101 tests,
smoke PASS.
2026-09-28 01:06:04 +07:00

162 lines
4.9 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { SKATE_SPOT_KIND, parseSkateSpot, type SkateSpot } from '@/lib/types';
import { useSkateRelays } from '@/hooks/useSkateRelays';
import { useAdminModeration, filterAdminHidden } from '@/hooks/useAdminModeration';
import { cacheSpots, getAllCachedSpots, isOffline } from '@/lib/offlineCache';
// Spots change infrequently — cache for 5 minutes, keep for 30 minutes
const SPOTS_STALE_TIME = 5 * 60 * 1000; // 5 min
const SPOTS_GC_TIME = 30 * 60 * 1000; // 30 min
const QUERY_TIMEOUT = 15000; // 15s — mobile cold-start needs more time
/**
* Spots, newest first.
*
* `includeHidden` is for ADMIN TOOLING ONLY (the review queue): without it a spot
* you just hid vanishes from the list, so there is no way to undo the verdict.
* Normal callers must leave it off so hidden spots stay hidden.
*/
export function useSkateSpots(limit = 100, options: { includeHidden?: boolean } = {}) {
const { includeHidden = false } = options;
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-spots', limit, includeHidden, getQueryKeySuffix()],
select: (spots: SkateSpot[]) =>
includeHidden ? spots : filterAdminHidden(spots, SKATE_SPOT_KIND, hidden),
queryFn: async () => {
// If offline, return cached spots
if (isOffline()) {
const cachedSpots = await getAllCachedSpots();
return cachedSpots.sort((a, b) => b.createdAt - a.createdAt).slice(0, limit);
}
const signal = AbortSignal.timeout(QUERY_TIMEOUT);
const events = await querySkateContent(
[
{
kinds: [SKATE_SPOT_KIND],
'#t': ['skatespot'],
limit,
},
],
{ signal }
);
const spots: SkateSpot[] = [];
const spotsWithEvents: Array<{ event: typeof events[0]; spot: SkateSpot }> = [];
for (const event of events) {
const spot = parseSkateSpot(event);
if (spot) {
spots.push(spot);
spotsWithEvents.push({ event, spot });
}
}
// Cache spots in the background for offline use
if (spotsWithEvents.length > 0) {
cacheSpots(spotsWithEvents).catch(() => {});
}
return spots.sort((a, b) => b.createdAt - a.createdAt);
},
staleTime: SPOTS_STALE_TIME,
gcTime: SPOTS_GC_TIME,
// Don't force a relay round-trip on every navigation — use cache if fresh
refetchOnMount: true,
});
}
export function useSkateSpot(identifier: string, pubkey?: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-spot', identifier, pubkey, getQueryKeySuffix()],
select: (spot: SkateSpot | null) =>
spot && hidden.has({ coord: `${SKATE_SPOT_KIND}:${spot.pubkey}:${spot.identifier}`, id: spot.id })
? null
: spot,
queryFn: async () => {
// If offline, try to get from cache first
if (isOffline() && pubkey) {
const { getCachedSpot } = await import('@/lib/offlineCache');
const cached = await getCachedSpot(pubkey, identifier);
if (cached) return cached;
}
const signal = AbortSignal.timeout(QUERY_TIMEOUT);
const filter: {
kinds: number[];
'#d': string[];
authors?: string[];
limit: number;
} = {
kinds: [SKATE_SPOT_KIND],
'#d': [identifier],
limit: 1,
};
if (pubkey) {
filter.authors = [pubkey];
}
const events = await querySkateContent([filter], { signal });
if (events.length === 0) return null;
const spot = parseSkateSpot(events[0]);
// Cache the spot for offline use
if (spot) {
const { cacheSpot } = await import('@/lib/offlineCache');
cacheSpot(events[0], spot).catch(() => {});
}
return spot;
},
enabled: !!identifier,
staleTime: SPOTS_STALE_TIME,
gcTime: SPOTS_GC_TIME,
refetchOnMount: true,
});
}
export function useUserSkateSpots(pubkey: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['user-skate-spots', pubkey, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(QUERY_TIMEOUT);
const events = await querySkateContent(
[
{
kinds: [SKATE_SPOT_KIND],
authors: [pubkey],
'#t': ['skatespot'],
limit: 50,
},
],
{ signal }
);
const spots: SkateSpot[] = [];
for (const event of events) {
const spot = parseSkateSpot(event);
if (spot) spots.push(spot);
}
return spots.sort((a, b) => b.createdAt - a.createdAt);
},
enabled: !!pubkey,
staleTime: SPOTS_STALE_TIME,
gcTime: SPOTS_GC_TIME,
refetchOnMount: true,
});
}