1import { useState, useEffect, useCallback } from 'react';
2import { Nothing } from 'folktale/maybe';
3import * as ob from 'urbit-ob';
5import SolarisClient from 'lib/SolarisClient';
6import { usePointCursor } from 'store/pointCursor';
7import useKeyfileGenerator from 'lib/useKeyfileGenerator';
17 CONNECTING: 'CONNECTING',
19 CONNECTED: 'CONNECTED',
22const mockFetch = status => ({
25 json: () => Promise.resolve({ status }),
28function useHostingStore(url) {
29 const client = new SolarisClient(url);
31 const { pointCursor } = usePointCursor();
33 const [state, _setState] = useState(STATE.UNKNOWN);
34 const setState = useCallback(
42 const [sysEvents, setSysEvents] = useState([]);
43 const [newEvents, setNewEvents] = useState([]);
44 const [runEvents, setRunEvents] = useState([]);
46 const [error, _setError] = useState();
47 const setError = useCallback(
51 setState(STATE.UNKNOWN);
58 const unknown = state === STATE.UNKNOWN;
59 const missing = state === STATE.MISSING;
60 const running = state === STATE.RUNNING;
61 const pending = state === STATE.PENDING;
62 const querying = state === STATE.QUERYING;
63 const booting = state === STATE.BOOTING;
64 const connecting = state === STATE.CONNECTING;
65 const connected = state === STATE.CONNECTED;
67 const resetEvents = useCallback(() => {
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]);
109 // }, [pointCursor, syncStatus]);
111 const getEvents = useCallback(() => {
113 if (Nothing.hasInstance(pointCursor)) {
116 const patp = ob.patp(pointCursor.value);
121 setState(STATE.CONNECTING);
123 const url = client.getShipsByPatpUrl(patp) + '/events';
124 const source = new EventSource(url);
126 source.onopen = () => setState(STATE.CONNECTED);
127 // source.onerror = () => setState(STATE.UNKNOWN);
128 // source.onmessage = console.log;
130 source.addEventListener('sys', event => {
132 setSysEvents(previous => [...previous, JSON.parse(event.data)]);
135 source.addEventListener('new', event => {
137 if (event.data !== '') {
138 setNewEvents(previous => [...previous, event.data]);
142 source.addEventListener('run', event => {
144 if (event.data !== '') {
145 setRunEvents(previous => [...previous, event.data]);
151 }, [setError, resetEvents, client, pointCursor, setState]);
153 const create = useCallback(
156 if (Nothing.hasInstance(pointCursor)) {
159 const patp = ob.patp(pointCursor.value);
162 setState(STATE.BOOTING);
164 const response = await client.postShips({
172 setError(response.statusText);
180 [pointCursor, setError, setState, client, getEvents]
207export default useHostingStore;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list