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

79 lines
2.4 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { SKATE_SHOP_KIND, parseSkateShop, type SkateShop } from '@/lib/types';
import { useSkateRelays } from '@/hooks/useSkateRelays';
import { useAdminModeration, filterAdminHidden } from '@/hooks/useAdminModeration';
/**
* Hook to fetch skate shops using the skate relay logic
*/
export function useSkateShops(limit: number = 100) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery<SkateShop[], Error, SkateShop[]>({
queryKey: ['skate-shops', limit, getQueryKeySuffix()],
select: (shops: SkateShop[]) => filterAdminHidden(shops, SKATE_SHOP_KIND, hidden),
queryFn: async () => {
const signal = AbortSignal.timeout(10000);
const events = await querySkateContent(
[{
kinds: [SKATE_SHOP_KIND],
'#t': ['skateshop'],
limit
}],
{ signal }
);
// Parse events into SkateShop objects
const shops: SkateShop[] = [];
for (const event of events) {
const shop = parseSkateShop(event);
if (shop) shops.push(shop);
}
// Sort by created_at descending
return shops.sort((a, b) => b.createdAt - a.createdAt);
},
staleTime: 60000, // 1 minute
});
}
/**
* Hook to fetch a single skate shop by pubkey and identifier
*/
export function useSkateShop(pubkey: string | undefined, identifier: string | undefined) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const hidden = useAdminModeration();
return useQuery<SkateShop | null, Error, SkateShop | null>({
queryKey: ['skate-shop', pubkey, identifier, getQueryKeySuffix()],
select: (shop: SkateShop | null) =>
shop && hidden.has({ coord: `${SKATE_SHOP_KIND}:${shop.pubkey}:${shop.identifier}`, id: shop.id })
? null
: shop,
queryFn: async () => {
if (!pubkey || !identifier) return null;
const signal = AbortSignal.timeout(10000);
const events = await querySkateContent(
[{
kinds: [SKATE_SHOP_KIND],
authors: [pubkey],
'#d': [identifier],
limit: 1,
}],
{ signal }
);
if (events.length > 0) {
return parseSkateShop(events[0]);
}
return null;
},
enabled: !!pubkey && !!identifier,
staleTime: 30000,
});
}