on master help sign in
add localHosting hook
51e7d9dLiam Fitzgerald
src/lib/useLocaHosting.jsnew file+51
@@ -0,0 +1,51 @@
1+import { useState, useEffect, useMemo } from 'react';
2+import { Just } from 'folktale/maybe';
3+import * as ob from 'urbit-ob';
4+
5+import { usePointCursor } from 'store/pointCursor';
6+
7+const DOMAIN = 'arvo.network';
8+
9+const STATE = {
10+ UNKNOWN: 'UNKNOWN',
11+ MISSING: 'MISSING',
12+ RUNNING: 'RUNNING',
13+};
14+
15+export default function useLocalHosting() {
16+ const [status, setStatus] = useState(STATE.UNKNOWN);
17+
18+ const { pointCursor } = usePointCursor();
19+
20+ useEffect(() => {
21+ const updateStatus = async point => {
22+ const patp = ob.patp(point).slice(1);
23+ try {
24+ const { who } = await fetch(`https://${patp}.${DOMAIN}/who.json`).then(
25+ r => r.json()
26+ );
27+ if (who === patp) {
28+ setStatus(STATE.RUNNING);
29+ } else {
30+ setStatus(STATE.MISSING);
31+ }
32+ } catch (e) {
33+ setStatus(STATE.MISSING);
34+ }
35+ };
36+ if (Just.hasInstance(pointCursor)) {
37+ updateStatus(pointCursor.value);
38+ }
39+ }, [pointCursor]);
40+
41+ const url = useMemo(
42+ () =>
43+ pointCursor.value &&
44+ `https://${ob.patp(pointCursor.value).slice(1)}.${DOMAIN}`,
45+ [pointCursor]
46+ );
47+
48+ const running = useMemo(() => status === STATE.RUNNING, [status]);
49+
50+ return { running, url };
51+}
src/views/UrbitOS/Home.js+35−14
@@ -1,4 +1,4 @@
1−import React, { useCallback, useState, useEffect } from 'react';
1+import React, { useCallback, useState, useEffect, useMemo } from 'react';
2import cn from 'classnames';
2import cn from 'classnames';
3import { Grid, Button, SelectInput, Flex } from 'indigo-react';
3import { Grid, Button, SelectInput, Flex } from 'indigo-react';
4import * as ob from 'urbit-ob';
4import * as ob from 'urbit-ob';
@@ -22,6 +22,7 @@ import { useLocalRouter } from 'lib/LocalRouter';
22import { useHosting } from 'store/hosting';
22import { useHosting } from 'store/hosting';
23import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
23import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
24import useLifecycle from 'lib/useLifecycle';
24import useLifecycle from 'lib/useLifecycle';
25+import useLocalHosting from 'lib/useLocaHosting';
25
26
26export default function UrbitOSHome({ manualNetworkSeed }) {
27export default function UrbitOSHome({ manualNetworkSeed }) {
27 const { pointCursor } = usePointCursor();
28 const { pointCursor } = usePointCursor();
@@ -104,6 +105,13 @@ function Hosting({ manualNetworkSeed }) {
104 disabled,
105 disabled,
105 } = ship;
106 } = ship;
106
107
108+ const { running: localRunning, url: localUrl } = useLocalHosting();
109+
110+ let running = useMemo(() => localRunning || ship.running, [
111+ ship,
112+ localRunning,
113+ ]);
114+
107 useLifecycle(() => {
115 useLifecycle(() => {
108 syncStatus();
116 syncStatus();
109 });
117 });
@@ -114,6 +122,19 @@ function Hosting({ manualNetworkSeed }) {
114 const options = [{ text: 'Tlon', value: 'tlon' }];
122 const options = [{ text: 'Tlon', value: 'tlon' }];
115
123
116 const renderMain = useCallback(() => {
124 const renderMain = useCallback(() => {
125+ if (localRunning) {
126+ return (
127+ <Grid.Item
128+ full
129+ as={LoginButton}
130+ solid
131+ success
132+ url={localUrl}
133+ code={code}>
134+ Open OS
135+ </Grid.Item>
136+ );
137+ }
117 if (ship.running) {
138 if (ship.running) {
118 return (
139 return (
119 <>
140 <>
@@ -158,6 +179,7 @@ function Hosting({ manualNetworkSeed }) {
158 );
179 );
159 }
180 }
160 }, [
181 }, [
182+ localRunning,
161 ship.missing,
183 ship.missing,
162 ship.running,
184 ship.running,
163 ship.pending,
185 ship.pending,
@@ -202,11 +224,8 @@ function Hosting({ manualNetworkSeed }) {
202 <Grid.Item full className="f5" as={Flex}>
224 <Grid.Item full className="f5" as={Flex}>
203 <Flex.Item>Urbit OS </Flex.Item>
225 <Flex.Item>Urbit OS </Flex.Item>
204 <Flex.Item
226 <Flex.Item
205− className={cn(
206− { green3: ship.running, gray4: !ship.running },
227+ className={cn({ green3: running, gray4: !running }, 'ml3')}>
207− 'ml3'
208− )}>
209− {ship.running ? 'Connected' : 'Disconnected'}
228+ {running ? 'Connected' : 'Disconnected'}
210 </Flex.Item>
229 </Flex.Item>
211 </Grid.Item>
230 </Grid.Item>
212 <BridgeForm initialValues={{ provider: 'tlon' }}>
231 <BridgeForm initialValues={{ provider: 'tlon' }}>
@@ -214,14 +233,16 @@ function Hosting({ manualNetworkSeed }) {
214 <>
233 <>
215 {renderDetails()}
234 {renderDetails()}
216 {renderMain()}
235 {renderMain()}
236+ {!localRunning && (
217− <Grid.Item
237+ <Grid.Item
218− full
238+ full
219− as={SelectInput}
239+ as={SelectInput}
220− name="provider"
240+ name="provider"
221− label="Host Provider"
241+ label="Host Provider"
222− options={options}
242+ options={options}
223− disabled
243+ disabled
224− />
244+ />
245+ )}
225 </>
246 </>
226 )}
247 )}
227 </BridgeForm>
248 </BridgeForm>