SkateSpots/src/hooks/useTours.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

127 lines
3.5 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) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery({
queryKey: ['skate-tours', limit, getQueryKeySuffix()],
select: (tours: SkateTour[]) => 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 }
);
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,
});
}