A React component library for rendering common AT Protocol records for applications such as Bluesky and Leaflet.
1import { useEffect, useState } from 'react'; 2import { useAtProto } from '../providers/AtProtoProvider'; 3 4/** 5 * Resolves the PDS service endpoint for a given DID and tracks loading state. 6 * 7 * @param did - DID whose PDS endpoint should be discovered. 8 * @returns {{ endpoint: string | undefined; error: Error | undefined; loading: boolean }} Object containing the resolved endpoint, error (if any), and loading flag. 9 */ 10export function usePdsEndpoint(did: string | undefined) { 11 const { resolver } = useAtProto(); 12 const [endpoint, setEndpoint] = useState<string | undefined>(); 13 const [error, setError] = useState<Error | undefined>(); 14 const [loading, setLoading] = useState(false); 15 16 useEffect(() => { 17 let cancelled = false; 18 if (!did) return; 19 setLoading(true); 20 resolver.pdsEndpointForDid(did) 21 .then(url => { if (!cancelled) setEndpoint(url); }) 22 .catch(e => { if (!cancelled) setError(e as Error); }) 23 .finally(() => { if (!cancelled) setLoading(false); }); 24 return () => { cancelled = true; }; 25 }, [did, resolver]); 26 27 return { endpoint, error, loading }; 28}