This repository has no description
1import { useQueries } from '@tanstack/react-query';
2import { getUrlMetadata } from '@/features/cards/lib/dal';
3import { getCollectionsForUrl } from '@/features/collections/lib/dal';
4import { collectionKeys } from '@/features/collections/lib/collectionKeys';
5
6interface Props {
7 url: string | undefined;
8 enabled?: boolean;
9}
10
11/**
12 * Fetches URL metadata and collections containing this URL for graph node popups
13 * Uses parallel queries for optimal performance
14 */
15export default function useGraphNodeUrl({ url, enabled = true }: Props) {
16 const results = useQueries({
17 queries: [
18 {
19 queryKey: url ? [url] : ['graph', 'url', 'metadata', 'empty'],
20 queryFn: () => {
21 if (!url) throw new Error('URL is required');
22 return getUrlMetadata({ url });
23 },
24 enabled: enabled && !!url,
25 staleTime: 3 * 60 * 1000, // 3 minutes - URLs change more often
26 retry: 1,
27 },
28 {
29 queryKey: url
30 ? collectionKeys.bySembleUrl(url)
31 : ['graph', 'url', 'collections', 'empty'],
32 queryFn: () => {
33 if (!url) throw new Error('URL is required');
34 return getCollectionsForUrl(url, { limit: 5 }); // Get first 5 collections
35 },
36 enabled: enabled && !!url,
37 staleTime: 3 * 60 * 1000,
38 retry: 1,
39 },
40 ],
41 });
42
43 const [metadataQuery, collectionsQuery] = results;
44
45 return {
46 metadata: metadataQuery.data?.metadata,
47 collections: collectionsQuery.data?.collections || [],
48 isLoading: metadataQuery.isLoading || collectionsQuery.isLoading,
49 error: metadataQuery.error || collectionsQuery.error,
50 metadataError: metadataQuery.error,
51 collectionsError: collectionsQuery.error,
52 };
53}