@@ -0,0 +1,138 @@
1+import { useCallback, useState } from 'react';
2+import ob from 'urbit-ob';
4+import SolarisClient from 'lib/SolarisClient';
5+import useDeepEqualReference from './useDeepEqualReference';
8+ NOT_FOUND: 'NOT_FOUND',
11+ CONNECTING: 'CONNECTING',
12+ CONNECTED: 'CONNECTED',
16+export function useHostedShip(point, endpoint = 'http://localhost:3030') {
17+ const patp = ob.patp(point.value);
18+ const client = new SolarisClient(endpoint);
20+ const [state, setState] = useState(STATE.UNKNOWN);
21+ const [events, setEvents] = useState([]);
22+ const [error, _setError] = useState();
24+ const setError = useCallback(
27+ setState(STATE.UNKNOWN);
29+ console.error(error);
35+ const addEvent = event => {
36+ setEvents(current => current.push(event));
39+ const notFound = state === STATE.NOT_FOUND;
40+ const running = state === STATE.RUNNING;
41+ const booting = state === STATE.BOOTING;
42+ const connecting = state === STATE.CONNECTING;
43+ const connected = state === STATE.CONNECTED;
44+ const unknown = state === STATE.UNKNOWN;
46+ const checkShipExists = useCallback(async () => {
50+ const response = await client.getShipsByPatp(patp);
52+ if (response.status === 404) {
53+ setState(STATE.NOT_FOUND);
54+ } else if (!response.ok) {
55+ setError(response.statusText);
57+ const ship = await response.json();
59+ if (ship.status === 'Running') {
60+ setState(STATE.RUNNING);
68+ }, [client, patp, setError]);
70+ const getShipEvents = useCallback(async () => {
73+ setState(STATE.CONNECTING);
75+ const response = await client.getShipsByPatpEvents(patp);
78+ setError(response.statusText);
80+ const reader = response.body.getReader();
82+ reader.read().then(function yieldEvent({ done, value }) {
83+ setState(STATE.CONNECTED);
85+ // Result objects contain two properties:
86+ // done - true if the stream has already given you all its data.
87+ // value - some data. Always undefined when done is true.
92+ addEvent(JSON.parse(value));
94+ // Read some more, and call this function again
95+ return reader.read().then(yieldEvent);
101+ }, [client, patp, setError]);
103+ const createNewShip = useCallback(async () => {
105+ setError(undefined);
106+ setState(STATE.BOOTING);
108+ const response = await client.postShips({
114+ setError(response.statusText);
121+ }, [checkShipExists, client, patp, setError]);
123+ const values = useDeepEqualReference({
137+ return { ...values, bind: values };