File size: 623 Bytes
ea270a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { useQuery } from '@tanstack/react-query';

async function fetchMapboxToken(): Promise<string | null> {
  try {
    const res = await fetch('/api/config/mapbox-token', { credentials: 'include' });
    if (!res.ok) return null;
    const data = await res.json();
    if (data.configured === false) return null;
    return data.token ?? null;
  } catch {
    return null;
  }
}

export function useMapboxToken() {
  const { data: token, isLoading } = useQuery({
    queryKey: ['mapbox-token'],
    queryFn: fetchMapboxToken,
    staleTime: Infinity,
    retry: 1,
  });
  return { token: token ?? null, isLoading };
}