on 2b2da70 help sign in
bridge/src/lib/useRoller.ts
55 lines 1.2 kB history raw
1import { useCallback, useEffect, useMemo, useState } from 'react';
2import { Config, RollerRPCAPI, Options } from '@urbit/roller-api';
3import { isDevelopment } from './flags';
5export default function useRoller() {
6 const [config, setConfig] = useState<Config | null>(null);
8 const options: Options = useMemo(() => {
9 const type = isDevelopment ? 'http' : 'https';
10 // TODO: What is the prod host?
11 const host = isDevelopment ? 'localhost' : 'bridge.urbit.org';
12 const port = 80;
13 const path = '/v1/roller';
14 return {
15 transport: {
16 type,
17 host,
18 port,
19 path,
20 },
21 };
22 }, []);
24 const api = useMemo(() => {
25 return new RollerRPCAPI(options);
26 }, [options]);
28 const fetchConfig = useCallback(async () => {
29 api
30 .getRollerConfig()
31 .then(response => {
32 setConfig(response);
33 })
34 .catch(err => {
35 // TODO: more elegant error handling
36 console.warn(
37 '[fetchConfig:failed] is roller running on localhost?\n',
38 err
39 );
40 });
41 }, [api]);
43 // On load, get initial config
44 useEffect(() => {
45 if (config) {
46 return;
47 }
49 fetchConfig();
50 }, [config, fetchConfig]);
52 return {
53 config,
54 };