11import './app.css'
22import 'react'
3- import { Component } from 'react'
4- import { createRoot } from 'react-dom/client'
5- import { getBrowserTheme } from './utils/get-theme.js'
6- import { Inspector } from './panels/inspector.js'
7- import { FloatingPanel } from './panels/floating-panel.js'
83import { LIBP2P_DEVTOOLS_METRICS_KEY } from '@libp2p/devtools-metrics'
9- import type { DevToolsEvents , MetricsRPC , Peer , RPCMessage } from '@libp2p/devtools-metrics'
10- import { evalOnPage } from './utils/eval-on-page.js'
4+ import { valueCodecs } from '@libp2p/devtools-metrics/rpc'
5+ import { TypedEventEmitter } from '@libp2p/interface'
6+ import { pipe } from 'it-pipe'
7+ import { pushable } from 'it-pushable'
8+ import { rpc } from 'it-rpc'
9+ import { base64 } from 'multiformats/bases/base64'
10+ import { Component , type ReactElement } from 'react'
11+ import { createRoot } from 'react-dom/client'
1112import SyntaxHighlighter from 'react-syntax-highlighter'
1213import { dark } from 'react-syntax-highlighter/dist/esm/styles/hljs'
13- import { sendMessage , events } from './utils/send-message.js'
14- import { defer , type DeferredPromise } from './utils/defer.js'
15- import { getPlatform } from './utils/get-platform.js'
14+ import { DetectingPanel } from './panels/detecting.js'
15+ import { FloatingPanel } from './panels/floating-panel.js'
1616import { GrantPermissions } from './panels/grant-permissions.js'
17- import { pushable , type Pushable } from 'it-pushable'
18- import { rpc , type RPC } from 'it-rpc'
19- import { valueCodecs } from '@libp2p/devtools-metrics/rpc'
20- import { base64 } from 'multiformats/bases/base64'
21- import { pipe } from 'it-pipe'
22- import { TypedEventEmitter } from '@libp2p/interface'
23- import { DetectingPanel } from './panels/detecting'
24- import type { TypedEventTarget } from '@libp2p/interface'
17+ import { Inspector } from './panels/inspector.js'
18+ import { defer } from './utils/defer.js'
19+ import { evalOnPage } from './utils/eval-on-page.js'
20+ import { getPlatform } from './utils/get-platform.js'
21+ import { getBrowserTheme } from './utils/get-theme.js'
22+ import { sendMessage , events } from './utils/send-message.js'
23+ import type { DeferredPromise } from './utils/defer.js'
24+ import type { DevToolsEvents , MetricsRPC , Peer , RPCMessage } from '@libp2p/devtools-metrics'
25+ import type { TypedEventTarget , Message } from '@libp2p/interface'
26+ import type { Pushable } from 'it-pushable'
27+ import type { RPC } from 'it-rpc'
2528
2629const theme = getBrowserTheme ( )
2730const platform = getPlatform ( )
2831
29- console . info ( 'theme is' , theme , 'browser is' , platform )
30-
31-
3232interface OfflineAppState {
3333 status : 'init' | 'missing' | 'permissions'
3434}
@@ -43,11 +43,13 @@ interface OnlineAppState {
4343 self : Peer
4444 peers : Peer [ ]
4545 debug : string
46+ capabilities : Record < string , string [ ] >
47+ pubsub : Record < string , Message [ ] >
4648}
4749
4850type AppState = OfflineAppState | ErrorAppState | OnlineAppState
4951
50- const ErrorPanel = ( { error } : { error : Error } ) => {
52+ const ErrorPanel = ( { error } : { error : Error } ) : ReactElement => {
5153 return (
5254 < >
5355 < FloatingPanel >
@@ -61,7 +63,7 @@ const ErrorPanel = ({ error }: { error: Error }) => {
6163 )
6264}
6365
64- const MissingPanel = ( ) => {
66+ const MissingPanel = ( ) : ReactElement => {
6567 return (
6668 < >
6769 < FloatingPanel >
@@ -82,17 +84,19 @@ const node = await createLibp2p({
8284 )
8385}
8486
85- const GrantPermissionsPanel = ( ) => {
87+ const GrantPermissionsPanel = ( ) : ReactElement => {
8688 return (
8789 < >
8890 < FloatingPanel >
8991 < h2 > Permissions</ h2 >
9092 < p > No data has been received from the libp2p node running on the page.</ p >
9193 < p > You may need to grant this extension access to the current page.</ p >
9294 {
93- platform === 'unknown' ? (
94- < p > Please see your browser documentation for how to do this.</ p >
95- ) : < GrantPermissions />
95+ platform === 'unknown'
96+ ? (
97+ < p > Please see your browser documentation for how to do this.</ p >
98+ )
99+ : < GrantPermissions />
96100 }
97101 </ FloatingPanel >
98102 </ >
@@ -141,11 +145,24 @@ class App extends Component {
141145 } ) )
142146 } )
143147
148+ this . events . addEventListener ( 'pubsub:message' , ( evt ) => {
149+ this . setState ( s => ( {
150+ ...s ,
151+ pubsub : {
152+ ...( s . pubsub ?? { } ) ,
153+ [ evt . detail . topic ?? '' ] : [
154+ ...( s . pubsub [ evt . detail . topic ] ?? [ ] ) ,
155+ evt . detail
156+ ]
157+ }
158+ } ) )
159+ } )
160+
144161 this . rpc . createTarget ( 'devTools' , this . events )
145162
146163 // send RPC messages
147164 Promise . resolve ( )
148- . then ( async ( ) => {
165+ . then ( async ( ) => {
149166 await pipe (
150167 this . rpcQueue ,
151168 this . rpc ,
@@ -158,10 +175,10 @@ class App extends Component {
158175 }
159176 }
160177 )
161- } )
162- . catch ( err => {
163- console . error ( 'error while reading RPC messages' , err )
164- } )
178+ } )
179+ . catch ( err => {
180+ console . error ( 'error while reading RPC messages' , err )
181+ } )
165182
166183 // receive RPC messages
167184 events . addEventListener ( 'libp2p-rpc' , ( event ) => {
@@ -183,7 +200,7 @@ class App extends Component {
183200 this . init ( )
184201 }
185202
186- init ( ) {
203+ init ( ) : void {
187204 Promise . resolve ( ) . then ( async ( ) => {
188205 const metricsPresent = await evalOnPage < boolean > ( `${ LIBP2P_DEVTOOLS_METRICS_KEY } === true` )
189206
@@ -197,15 +214,17 @@ class App extends Component {
197214 const signal = AbortSignal . timeout ( 2000 )
198215
199216 try {
200- const { self, peers, debug } = await this . metrics . init ( {
217+ const { self, peers, debug, capabilities } = await this . metrics . init ( {
201218 signal
202219 } )
203220
204221 this . setState ( {
205222 status : 'online' ,
206223 self,
207224 peers,
208- debug
225+ debug,
226+ capabilities,
227+ pubsub : { }
209228 } )
210229 } catch ( err : any ) {
211230 if ( signal . aborted ) {
@@ -221,17 +240,17 @@ class App extends Component {
221240 } )
222241 }
223242 } )
224- . catch ( err => {
225- console . error ( 'error communicating with page' , err )
243+ . catch ( err => {
244+ console . error ( 'error communicating with page' , err )
226245
227- this . setState ( {
228- status : 'error' ,
229- error : err
246+ this . setState ( {
247+ status : 'error' ,
248+ error : err
249+ } )
230250 } )
231- } )
232251 }
233252
234- render ( ) {
253+ render ( ) : ReactElement {
235254 if ( this . state . status === 'init' ) {
236255 return (
237256 < DetectingPanel />
0 commit comments