SkateSpots/src/hooks/useSkateSpots.ts
shakespeare.diy 13eece3a1e Admins can delete/take down any listing (spot, crew, tour, event, shop)
The two SkateSpots admin pubkeys (already in src/lib/admins.ts) can now remove
any listing, not just their own. Because a NIP-09 deletion signed by a
non-author isn't honored by most relays, admin takedowns are made reliable with
a client-side moderation layer:

- useDeleteEntity: owners still publish a plain NIP-09 kind:5. Admins acting on
  someone else's content publish the kind:5 AND a NIP-32 kind:1985 `hidden`
  label referencing the target coordinate/id, authored by the admin.
- useAdminModeration (new): fetches admin-authored `hidden` labels app-wide and
  exposes a fast lookup + filterAdminHidden() helper. Only labels whose author
  is in the hardcoded ADMIN_PUBKEYS are honored (author = trust boundary,
  re-verified client-side; never derived from a d-tag).
- Wired the hide filter into the list + detail hooks for spots, crews (incl.
  by-focus), tours, events, and shops via React Query `select`, so removed
  content disappears from lists and 404s on its detail page.

UI:
- DeleteEntityDialog now renders for owner OR admin, with distinct
  "Remove (admin)" framing and copy (used on Crew, Coach, Tour, Event pages).
- SpotDetailPage & ShopDetailPage: routed their inline delete through
  useDeleteEntity, show the delete button for admins, and relabel the confirm
  dialog for admin takedowns.
- Added the delete control to Tour and Event detail pages (previously none).

SECURITY.md: documented the admin moderation trust boundary and extended the
auditor file map. Verified build + no console errors.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
2026-08-18 17:51:34 +07:00

153 lines
4.5 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
export function useSkateSpots(limit = 100) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-spots', limit, getQueryKeySuffix()],
select: (spots: SkateSpot[]) => 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,
});
}