@@ -1,9 +1,10 @@
1−import { useState, useEffect, useCallback } from 'react';
1+import { useState, useCallback, useMemo } from 'react';
2−import { Nothing } from 'folktale/maybe';
2+import { Just, Nothing } from 'folktale/maybe';
3import * as ob from 'urbit-ob';
3import * as ob from 'urbit-ob';
5+import { getVaneName, getVaneNumber } from 'lib/hosting';
5import SolarisClient from 'lib/SolarisClient';
6import SolarisClient from 'lib/SolarisClient';
6−import { usePointCursor } from 'store/pointCursor';
7+// import { usePointCursor } from 'store/pointCursor';
7import useKeyfileGenerator from 'lib/useKeyfileGenerator';
8import useKeyfileGenerator from 'lib/useKeyfileGenerator';
8const STATE = {
9const STATE = {
9 // container states
10 // container states
@@ -11,32 +12,26 @@ const STATE = {
11 MISSING: 'MISSING',
12 MISSING: 'MISSING',
12 RUNNING: 'RUNNING',
13 RUNNING: 'RUNNING',
13 PENDING: 'PENDING',
14 PENDING: 'PENDING',
15− QUERYING: 'QUERYING',
17− CONNECTING: 'CONNECTING',
19− CONNECTED: 'CONNECTED',
22−const mockFetch = status => ({
25− json: () => Promise.resolve({ status }),
28function useHostingStore(url) {
17function useHostingStore(url) {
29 const client = new SolarisClient(url);
18 const client = new SolarisClient(url);
31− const { pointCursor } = usePointCursor();
20+ // const { pointCursor } = usePointCursor();
22+ const pointCursor = Just(ob.patp2dec('~wicdev-wisryt'));
24+ const [bootProgress, setBootProgress] = useState(0);
25+ const [bootMessage, setBootMessage] = useState('Assembling Urbit');
26+ const [startTime] = useState(Date.now());
33− const [state, _setState] = useState(STATE.UNKNOWN);
28+ const [status, _setStatus] = useState(STATE.UNKNOWN);
34− const setState = useCallback(
29+ const setStatus = useCallback(
36 console.log(s);
31 console.log(s);
37− _setState(s);
32+ _setStatus(s);
39− [_setState]
34+ [_setStatus]
42 const [sysEvents, setSysEvents] = useState([]);
37 const [sysEvents, setSysEvents] = useState([]);
@@ -48,65 +43,25 @@ function useHostingStore(url) {
48 error => {
43 error => {
49 _setError(error);
44 _setError(error);
50 if (error) {
45 if (error) {
51− setState(STATE.UNKNOWN);
46+ setStatus(STATE.UNKNOWN);
52 console.error(error);
47 console.error(error);
55− [_setError, setState]
50+ [_setError, setStatus]
58− const unknown = state === STATE.UNKNOWN;
53+ const unknown = status === STATE.UNKNOWN;
59− const missing = state === STATE.MISSING;
54+ const missing = status === STATE.MISSING;
60− const running = state === STATE.RUNNING;
55+ const running = status === STATE.RUNNING;
61− const pending = state === STATE.PENDING;
56+ const pending = status === STATE.PENDING;
62− const querying = state === STATE.QUERYING;
57+
63− const booting = state === STATE.BOOTING;
58+ let syncStatus;
64− const connecting = state === STATE.CONNECTING;
65− const connected = state === STATE.CONNECTED;
67 const resetEvents = useCallback(() => {
60 const resetEvents = useCallback(() => {
68 setSysEvents([]);
61 setSysEvents([]);
69 setNewEvents([]);
62 setNewEvents([]);
70 setRunEvents([]);
63 setRunEvents([]);
71− }, []);
64+ }, [setSysEvents, setNewEvents, setRunEvents]);
73− const syncStatus = useCallback(async () => {
75− if (Nothing.hasInstance(pointCursor)) {
78− const patp = ob.patp(pointCursor.value);
83− setState(STATE.QUERYING);
85− const response = await client.getShipsByPatp(patp);
87− if (response.status === 404) {
88− setState(STATE.MISSING);
89− } else if (!response.ok) {
90− setError(response.statusText);
92− const ship = await response.json();
94− if (ship.status === 'Pending') {
95− setState(STATE.PENDING);
96− } else if (ship.status === 'Running') {
97− setState(STATE.RUNNING);
105− }, [client, pointCursor, resetEvents, setError, setState]);
107− // useEffect(() => {
109− // }, [pointCursor, syncStatus]);
111 const getEvents = useCallback(() => {
66 const getEvents = useCallback(() => {
@@ -118,29 +73,53 @@ function useHostingStore(url) {
119 resetEvents();
74 resetEvents();
121− setState(STATE.CONNECTING);
123 const url = client.getShipsByPatpUrl(patp) + '/events';
76 const url = client.getShipsByPatpUrl(patp) + '/events';
124 const source = new EventSource(url);
77 const source = new EventSource(url);
126− source.onopen = () => setState(STATE.CONNECTED);
127− // source.onerror = () => setState(STATE.UNKNOWN);
79+ // source.onerror = () => setStatus(STATE.UNKNOWN);
128 // source.onmessage = console.log;
80 // source.onmessage = console.log;
130 source.addEventListener('sys', event => {
82 source.addEventListener('sys', event => {
131− console.log(event);
83+ // console.log(event);
84+ const sysEvent = JSON.parse(event.data);
85+ const time = new Date(sysEvent.object.firstTimestamp).getTime();
86+ if (time > startTime) {
87+ console.log(sysEvent);
132− setSysEvents(previous => [...previous, JSON.parse(event.data)]);
88+ setSysEvents(previous => [...previous, sysEvent]);
135 source.addEventListener('new', event => {
92 source.addEventListener('new', event => {
136− console.log(event);
93+ // console.log(event);
94+ const elapsed = (Date.now() - startTime) / 1000;
137 if (event.data !== '') {
95 if (event.data !== '') {
138 setNewEvents(previous => [...previous, event.data]);
96 setNewEvents(previous => [...previous, event.data]);
98+ if (event.data === '1-b') {
99+ setBootMessage('Compiling Hoon');
100+ setBootProgress(0.2);
101+ console.log(`${elapsed}: compiling Hoon`);
102+ } else if (event.data === '1-c') {
103+ setBootProgress(0.25);
104+ } else if (event.data === '%arvo-assembly') {
105+ setBootMessage('Compiling Urbit OS');
106+ setBootProgress(0.3);
107+ } else if (event.data.startsWith('[%vane %')) {
108+ const vane = getVaneName(event.data.slice(8, 9));
109+ setBootMessage(`Compiling vane: ${vane}`);
110+ setBootProgress(0.3 + 0.05 * getVaneNumber(event.data.slice(8, 9)));
111+ } else if (event.data === 'pier: boot complete') {
112+ setBootMessage('Ship starting');
113+ setBootProgress(0.85);
142 source.addEventListener('run', event => {
117 source.addEventListener('run', event => {
143− console.log(event);
118+ if (event.data === 'goad: recompiling all apps') {
119+ setBootProgress(1.0);
120+ setBootMessage('Ship Launched');
144 if (event.data !== '') {
123 if (event.data !== '') {
145 setRunEvents(previous => [...previous, event.data]);
124 setRunEvents(previous => [...previous, event.data]);
@@ -148,7 +127,54 @@ function useHostingStore(url) {
148 } catch (error) {
127 } catch (error) {
149 setError(error);
128 setError(error);
140+ syncStatus = useCallback(async () => {
142+ if (Nothing.hasInstance(pointCursor)) {
145+ const patp = ob.patp(pointCursor.value);
146+ setError(undefined);
151− }, [setError, resetEvents, client, pointCursor, setState]);
148+ resetEvents();
150+ const response = await client.getShipsByPatp(patp);
152+ if (response.status === 404) {
153+ setStatus(STATE.MISSING);
154+ } else if (!response.ok) {
155+ setError(response.statusText);
157+ const ship = await response.json();
159+ if (ship.status === 'Pending') {
160+ setStatus(STATE.PENDING);
162+ } else if (ship.status === 'Running') {
163+ setStatus(STATE.RUNNING);
171+ }, [client, getEvents, pointCursor, resetEvents, setError, setStatus]);
173+ const hostedShipUrl = useMemo(
175+ `http://${ob.patp(pointCursor.getOrElse('')).slice(1)}.liam.tlon.network`,
153 const create = useCallback(
179 const create = useCallback(
154 async keyfile => {
180 async keyfile => {
@@ -156,10 +182,15 @@ function useHostingStore(url) {
156 if (Nothing.hasInstance(pointCursor)) {
182 if (Nothing.hasInstance(pointCursor)) {
186+ '0wnE.Z56pm.i2HLZ.6Pf1R.tAVZH.65R~C.Zgy6y.caI5Z.bZ2vV.09M5A.QP5-p.29UfR.35h2U.2k8-D.QP~~p.6bL62.~x3Em.t9h0x.0y01N.w80c1';
159− const patp = ob.patp(pointCursor.value);
187+ const patp = ob.patp(pointCursor.value).slice(1);
160 setError(undefined);
188 setError(undefined);
162− setState(STATE.BOOTING);
190+ const start = Date.now();
192+ setStatus(STATE.PENDING);
193+ setBootProgress(0.1);
164 const response = await client.postShips({
195 const response = await client.postShips({
@@ -177,7 +208,7 @@ function useHostingStore(url) {
177 setError(error);
208 setError(error);
180− [pointCursor, setError, setState, client, getEvents]
211+ [pointCursor, setError, setStatus, client, getEvents]
@@ -186,10 +217,7 @@ function useHostingStore(url) {
189− querying,
220+ url: hostedShipUrl,
194 sysEvents,
222 sysEvents,
195 newEvents,
223 newEvents,
@@ -199,8 +227,11 @@ function useHostingStore(url) {
199 getEvents,
227 getEvents,
202 // debugging
233 // debugging