This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,31 @@ | ||
{ | ||
"projectType": "application", | ||
"root": "packages/explorer", | ||
"serve": { | ||
"executor": "@nrwl/workspace:run-commands", | ||
"options": { | ||
"commands": [ | ||
"nx build lib", | ||
"nx build ipfs", | ||
"cd packages/explorer && yarn serve" | ||
], | ||
"cwd": "/" | ||
} | ||
}, | ||
"sourceRoot": "packages/explorer/src", | ||
"tags": [], | ||
"targets": { | ||
"build": { | ||
"dependsOn": [ | ||
{ | ||
"projects": "dependencies", | ||
"target": "ipfs:build" | ||
}, | ||
{ | ||
"projects": "dependencies", | ||
"target": "lib:build" | ||
} | ||
"executor": "@nrwl/workspace:run-commands", | ||
"options": { | ||
"commands": [ | ||
"nx build lib", | ||
"nx build ipfs", | ||
"yarn build" | ||
], | ||
"executor": "@nrwl/workspace:run-commands", | ||
"options": { | ||
"commands": [ | ||
"yarn build" | ||
], | ||
"cwd": "packages/explorer" | ||
}, | ||
"outputs": [ | ||
"packages/explorer/dist" | ||
] | ||
"cwd": "/" | ||
}, | ||
"serve": { | ||
"dependsOn": [ | ||
{ | ||
"projects": "dependencies", | ||
"target": "ipfs:build" | ||
}, | ||
{ | ||
"projects": "dependencies", | ||
"target": "lib:build" | ||
} | ||
], | ||
"executor": "@nrwl/workspace:run-commands", | ||
"options": { | ||
"commands": [ | ||
"yarn serve" | ||
], | ||
"cwd": "packages/explorer" | ||
} | ||
} | ||
"outputs": [ | ||
"packages/explorer/dist" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,39 @@ | ||
import { create } from '@dstack-js/ipfs'; | ||
import { Stack } from '@dstack-js/lib'; | ||
import React, { useState } from 'react'; | ||
import { Dashboard } from './dashboard'; | ||
|
||
export const App: React.FunctionComponent<{}> = () => { | ||
const [ready, setReady] = useState(false); | ||
const [stack, setStack] = useState<Stack | null>(null); | ||
const [fillInfo, setFillInfo] = useState(true); | ||
const [namespace, setNamespace] = useState('dstack'); | ||
|
||
const init = async (): Promise<void> => { | ||
setFillInfo(false); | ||
localStorage.setItem('debug', '*'); | ||
|
||
const ipfs = await create(); | ||
const stack = await Stack.create('stack', ipfs); | ||
const stack = await Stack.create(namespace, ipfs); | ||
stack.debug(); | ||
|
||
// @ts-expect-error | ||
window.stack = stack; | ||
|
||
setReady(true); | ||
setStack(stack); | ||
}; | ||
|
||
React.useEffect(() => { | ||
init(); | ||
}, []); | ||
if (fillInfo) { | ||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={namespace} | ||
onChange={(e) => setNamespace(e.target.value)} | ||
></input> | ||
<button onClick={init}>Start</button> | ||
</div> | ||
); | ||
} | ||
|
||
return ready ? ( | ||
<pre>use `window.stack` to get started</pre> | ||
) : ( | ||
<h3>Initializing</h3> | ||
); | ||
return stack ? <Dashboard stack={stack} /> : <h3>Initializing</h3>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Stack, Peer } from '@dstack-js/lib'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
export const Dashboard: React.FunctionComponent<{ stack: Stack }> = (props) => { | ||
const [peers, setPeers] = useState<Peer[]>([]); | ||
const [topic, setTopic] = useState<string>('date'); | ||
const [events, setEvents] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
props.stack.pubsub.subscribe('$store', (msg) => { | ||
events.push(`$store: ${msg.from} ${(msg.data as any).key}`); | ||
setEvents(events.slice(1).slice(-5)); | ||
}); | ||
|
||
const updateState = async () => { | ||
setPeers(await props.stack.peers()); | ||
}; | ||
|
||
const interval = setInterval(() => { | ||
updateState(); | ||
}, 100); | ||
|
||
return () => { | ||
props.stack.pubsub.unsubscribe('$store'); | ||
clearInterval(interval); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
props.stack.pubsub.subscribe(topic, (msg) => { | ||
events.push(`${topic}: ${msg.from} ${msg.data}`); | ||
setEvents(events.slice(1).slice(-5)); | ||
}); | ||
|
||
return () => { | ||
props.stack.pubsub.unsubscribe(topic); | ||
}; | ||
}, [topic]); | ||
|
||
return ( | ||
<div> | ||
<pre>use `window.stack` to interact with DStack</pre> | ||
<br /> | ||
Topic: | ||
<input | ||
type="text" | ||
value={topic} | ||
onChange={(e) => setTopic(e.target.value)} | ||
/> | ||
<br /> | ||
<button | ||
onClick={() => | ||
props.stack.pubsub.publish(topic, new Date().toISOString()) | ||
} | ||
> | ||
Publish event that contains date | ||
</button> | ||
<br /> | ||
<h3>Connected Peers:</h3> | ||
{peers.map((peer) => ( | ||
<li key={peer.id}>{peer.id}</li> | ||
))} | ||
<br /> | ||
<h3>Captured events:</h3> | ||
{events.map((event) => ( | ||
<div key={event}> | ||
{event} <br /> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters