SkateSpots/src/hooks/useShops.ts
SkateSpots 127819e3da feat(nip05): multiple handles for profiles, crews and shops
A NIP-05 name maps to one key, so "who owns it" decides where it lives:
- Crew: repeatable nip05 tags (up to 5), verified against the founder key.
- Shop: same on kind 30017, verified against the owner key (new field).
- Profile: kind-0 has ONE standard nip05; extra personal handles live in a
  separate NIP-78 record (kind 30078, d=skatespots:profile-handles) so another
  client re-saving the profile can't wipe them.

Shared pieces:
- components/Nip05Badges.tsx — generic verify-and-render chip row (any owner).
- components/Nip05HandlesEditor.tsx — shared add/remove editor with live
  verified/mismatch status; used by profile, crew and shop forms.
- CrewNip05Badge now delegates to Nip05Badges; CrewForm uses the shared editor.
- Profile page shows primary + extra handles, and each crew/shop chips its own
  verified handle.
- lib/profileHandles.ts + hooks/useProfileHandles.ts (read/publish the record).
- types: SkateShop gains nip05/nip05s; parseSkateShop parses repeatable nip05s.
- NIP.md + AGENTS.md + i18n (EN/ES) updated.
2026-10-03 17:26:59 +07:00

201 lines
5.7 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { SKATE_SHOP_KIND, SKATE_SPOT_KIND, parseSkateShop, parseSkateSpot, type SkateShop } from '@/lib/types';
import { useSkateRelays } from '@/hooks/useSkateRelays';
export function useShops(limit = 100) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['shop-directory', 'list', limit, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(10000);
// Query new shop kind (30017)
const newShops = await querySkateContent(
[
{
kinds: [SKATE_SHOP_KIND],
'#t': ['skateshop'],
limit,
},
],
{ signal, requireAny: true }
);
// Query old shops (kind 30818 with 'shop' type) for backward compatibility
const oldShops = await querySkateContent(
[
{
kinds: [SKATE_SPOT_KIND],
'#t': ['shop'],
limit,
},
],
{ signal }
);
const allEvents = [...newShops, ...oldShops];
const shops: SkateShop[] = [];
for (const event of allEvents) {
// Try parsing as new shop (kind 30017)
let shop = parseSkateShop(event);
// If that fails, try parsing as old spot and convert to shop
if (!shop && event.kind === SKATE_SPOT_KIND) {
const spot = parseSkateSpot(event);
if (spot && spot.types.includes('shop')) {
// Convert old spot-type shop to new shop format
shop = {
id: spot.id,
pubkey: spot.pubkey,
identifier: spot.identifier,
name: spot.title,
description: spot.description,
geohash: spot.geohash,
location: spot.location,
images: spot.images,
phone: undefined,
email: undefined,
website: undefined,
instagram: undefined,
hours: undefined,
nip05s: [],
createdAt: spot.createdAt,
event: spot.event,
};
}
}
if (shop) {
shops.push(shop);
}
}
// Sort by created_at descending
return shops.sort((a, b) => b.createdAt - a.createdAt);
},
staleTime: 5 * 60 * 1000,
gcTime: 30 * 60 * 1000,
refetchOnMount: true,
});
}
export function useShop(identifier: string, pubkey?: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['shop-directory', 'item', identifier, pubkey, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(10000);
// Query new shop format (kind 30017)
const newFilter: {
kinds: number[];
'#d': string[];
authors?: string[];
limit: number;
} = {
kinds: [SKATE_SHOP_KIND],
'#d': [identifier],
limit: 1,
};
if (pubkey) newFilter.authors = [pubkey];
const newShops = await querySkateContent([newFilter], { signal });
// Query old shop format (kind 30818 with 'shop' type) for backward compatibility
const oldFilter: {
kinds: number[];
'#d': string[];
'#t': string[];
authors?: string[];
limit: number;
} = {
kinds: [SKATE_SPOT_KIND],
'#d': [identifier],
'#t': ['shop'],
limit: 1,
};
if (pubkey) oldFilter.authors = [pubkey];
const oldShops = await querySkateContent([oldFilter], { signal });
const allEvents = [...newShops, ...oldShops];
if (allEvents.length === 0) return null;
// If multiple events, take the most recent
const mostRecent = allEvents.reduce((latest, current) =>
current.created_at > latest.created_at ? current : latest
);
// Try parsing as new shop first
let shop = parseSkateShop(mostRecent);
// If that fails, try parsing as old spot and convert
if (!shop && mostRecent.kind === SKATE_SPOT_KIND) {
const spot = parseSkateSpot(mostRecent);
if (spot && spot.types.includes('shop')) {
shop = {
id: spot.id,
pubkey: spot.pubkey,
identifier: spot.identifier,
name: spot.title,
description: spot.description,
geohash: spot.geohash,
location: spot.location,
images: spot.images,
phone: undefined,
email: undefined,
website: undefined,
instagram: undefined,
hours: undefined,
nip05s: [],
createdAt: spot.createdAt,
event: spot.event,
};
}
}
return shop;
},
enabled: !!identifier,
staleTime: 5 * 60 * 1000,
gcTime: 30 * 60 * 1000,
refetchOnMount: true,
});
}
export function useUserShops(pubkey: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['shop-directory', 'user', pubkey, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(10000);
const events = await querySkateContent(
[
{
kinds: [SKATE_SHOP_KIND],
authors: [pubkey],
'#t': ['skateshop'],
limit: 50,
},
],
{ signal }
);
const shops: SkateShop[] = [];
for (const event of events) {
const shop = parseSkateShop(event);
if (shop) shops.push(shop);
}
return shops.sort((a, b) => b.createdAt - a.createdAt);
},
enabled: !!pubkey,
staleTime: 5 * 60 * 1000,
gcTime: 30 * 60 * 1000,
refetchOnMount: true,
});
}