SkateSpots/src/hooks/useSkateSpots.ts
shakespeare.diy e29d35f1e4 fix: restore full skate-spots source tree from misplaced src/src/ copy
The entire skate-spots codebase was accidentally copied into src/src/ and
public/public/ instead of src/ and public/ directly. This left the real
src/ directory as blank MKStack template stubs, explaining why the landing
page showed the default template instead of SkateSpots.

Changes made:
- Copied all files from src/src/ up to src/ (App.tsx, AppRouter.tsx, main.tsx,
  all pages, components, hooks, contexts, lib, i18n, test)
- Copied all files from public/public/ up to public/ (images, manifest, etc.)
- Deleted src/src/ and public/public/ nested directories
- Removed HelloWorld.tsx template stub from pages/
- Retained the previously fixed index.css (Tailwind v3 directives)

Full skate-spots feature set is now active:
- All 30+ routes (map, events, spots, coaches, marketplace, wallet, etc.)
- NWCProvider, LanguageProvider, PWAInstallPrompt, AmberCallbackHandler
- Skate community relays (goskatespots.nostr1.com, wotr.relatr.xyz, etc.)
- Outfit Variable font, Amber pre-check in main.tsx
- All hooks (useSkateSpots, useCoaches, useZaps, etc.)
- i18n support (en/es)

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
2026-06-15 22:18:28 +07:00

154 lines
4.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { SKATE_SPOT_KIND, parseSkateSpot, type SkateSpot } from '@/lib/types';
import { useSkateRelays } from '@/hooks/useSkateRelays';
import { cacheSpots, getAllCachedSpots, isOffline } from '@/lib/offlineCache';
export function useSkateSpots(limit = 100) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['skate-spots', limit, getQueryKeySuffix()],
queryFn: async () => {
// If offline, return cached spots
if (isOffline()) {
console.log('[useSkateSpots] Offline, returning cached spots');
const cachedSpots = await getAllCachedSpots();
return cachedSpots.sort((a, b) => b.createdAt - a.createdAt).slice(0, limit);
}
const signal = AbortSignal.timeout(30000);
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 });
} else {
console.warn('[useSkateSpots] Failed to parse event:', event);
}
}
console.log('[useSkateSpots] Parsed spots:', spots.length);
// Cache spots in the background for offline use
if (spotsWithEvents.length > 0) {
cacheSpots(spotsWithEvents).catch(err =>
console.warn('[useSkateSpots] Failed to cache spots:', err)
);
}
// Sort by created_at descending
return spots.sort((a, b) => b.createdAt - a.createdAt);
},
staleTime: 30000,
gcTime: 300000,
refetchOnMount: 'always',
});
}
export function useSkateSpot(identifier: string, pubkey?: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['skate-spot', identifier, pubkey, getQueryKeySuffix()],
queryFn: async () => {
// If offline, try to get from cache first
if (isOffline() && pubkey) {
console.log('[useSkateSpot] Offline, checking cache');
const { getCachedSpot } = await import('@/lib/offlineCache');
const cached = await getCachedSpot(pubkey, identifier);
if (cached) {
console.log('[useSkateSpot] Found in cache:', identifier);
return cached;
}
}
const signal = AbortSignal.timeout(30000);
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) {
console.warn(`[useSkateSpot] No events found for identifier: ${identifier}`);
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(err =>
console.warn('[useSkateSpot] Failed to cache spot:', err)
);
}
return spot;
},
enabled: !!identifier,
staleTime: 30000,
refetchOnMount: 'always',
});
}
export function useUserSkateSpots(pubkey: string) {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
return useQuery({
queryKey: ['user-skate-spots', pubkey, getQueryKeySuffix()],
queryFn: async () => {
const signal = AbortSignal.timeout(30000);
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: 30000,
refetchOnMount: 'always',
});
}