on master help sign in
bridge/src/lib/useRollerOptions.ts
59 lines 1.5 kB history raw
1import { Options } from '@urbit/roller-api';
2import { useMemo } from 'react';
3import { ROLLER_HOSTS, ROLLER_PATH } from './constants';
4import { convertToInt } from './convertToInt';
5import { isMainnet, isGoerli, providedRollerOptions } from './flags';
7type TransportType =
8 | 'websocket'
9 | 'http'
10 | 'https'
11 | 'postmessagewindow'
12 | 'postmessageiframe';
14/**
15 * This hook produces a Roller API options config based on the runtime env.
16 *
17 * By default, it uses the hardcoded values from constants.ts, but optionally
18 * a consumer of Bridge can provide a custom host, port, and transport type to
19 * override these settings.
20 *
21 * @returns options (type Options from @urbit/roller-api)
22 */
23export const useRollerOptions = () => {
24 const {
25 host: providedHost,
26 port: providedPort,
27 type: providedType,
28 } = providedRollerOptions;
30 const options: Options = useMemo(() => {
31 const type =
32 (providedType as TransportType | undefined) ||
33 (isMainnet || isGoerli ? 'https' : 'http');
34 const host =
35 providedHost ||
36 (isMainnet
37 ? ROLLER_HOSTS.MAINNET
38 : isGoerli
39 ? ROLLER_HOSTS.GOERLI
40 : ROLLER_HOSTS.LOCAL);
41 const port =
42 (providedPort && convertToInt(providedPort, 10)) ||
43 (isMainnet || isGoerli ? 443 : 8080);
44 const path = ROLLER_PATH;
46 return {
47 transport: {
48 type,
49 host,
50 port,
51 path,
52 },
53 };
54 }, [providedHost, providedPort, providedType]);
56 return {
57 options,
58 };
59};