main
tsx 129 lines 3.48 KB
Raw
1 import { Component } from 'react'
2 import { getBytes } from '../../utils/get-bytes.js'
3 import { Button } from '../button.js'
4 import { Panel } from '../panel.js'
5 import { SmallError, SmallSuccess } from '../status.js'
6 import { TextInput } from '../text-input.js'
7 import type { MetricsRPC } from '@ipshipyard/libp2p-inspector-metrics'
8 import type { FormEvent, JSX } from 'react'
9
10 export interface PutProps {
11 metrics: MetricsRPC
12 }
13
14 export interface PutState {
15 key: string
16 value: string
17 error: string
18 details: JSX.Element[]
19 }
20
21 export class Put extends Component<PutProps, PutState> {
22 constructor (props: PutProps) {
23 super(props)
24
25 this.state = {
26 key: '',
27 value: '',
28 error: '',
29 details: []
30 }
31 }
32
33 componentDidMount (): void {
34 this.setState({
35 key: '',
36 value: '',
37 error: '',
38 details: []
39 })
40 }
41
42 private put (evt: FormEvent | Event, key: string, value: string): boolean {
43 evt.preventDefault()
44
45 this.setState({
46 error: '',
47 details: []
48 })
49
50 Promise.resolve()
51 .then(async () => {
52 await this.props.metrics.contentRouting.put(getBytes(key), getBytes(value), {
53 onProgress: (event) => {
54 let message: string = event.type
55
56 if (event.type === 'dial:already-connected') {
57 message = 'Already connected to this peer'
58 } else if (event.type === 'dial:add-to-dial-queue') {
59 message = 'Adding dial to queue'
60 } else if (event.type === 'dial:already-in-dial-queue') {
61 message = 'Dial to this peer already in queue'
62 } else if (event.type === 'dial:calculate-addresses') {
63 message = 'Calculating addresses'
64 } else if (event.type === 'dial:selected-transport') {
65 message = `Selected transport ${event.detail}`
66 }
67
68 this.setState(s => {
69 return {
70 details: [
71 ...s.details,
72 <p key={`event-${s.details.length}`}>{message}</p>
73 ]
74 }
75 })
76 }
77 })
78
79 this.setState(s => {
80 return {
81 error: '',
82 details: [
83 ...s.details,
84 <SmallSuccess key={`event-${s.details.length}`} message='Get successful' />
85 ]
86 }
87 })
88 })
89 .catch(err => {
90 this.setState(s => {
91 return {
92 details: [
93 ...s.details,
94 <SmallError key={`event-${s.details.length}`} error={err} />
95 ]
96 }
97 })
98 })
99
100 return false
101 }
102
103 render (): JSX.Element {
104 return (
105 <Panel>
106 <p>Enter a multibase encoded key and value store in the routing:</p>
107 <form onSubmit={(evt) => this.put(evt, this.state.key, this.state.value)}>
108 <TextInput
109 type='text' value={this.state.key} placeholder='mKey...' onChange={(e) => {
110 this.setState({
111 key: e.target.value
112 })
113 }}
114 />
115 <TextInput
116 type='text' value={this.state.value} placeholder='mValue...' onChange={(e) => {
117 this.setState({
118 value: e.target.value
119 })
120 }}
121 />
122 <Button onClick={(evt) => this.put(evt, this.state.key, this.state.value)} primary>Get</Button>
123 </form>
124 {this.state.error ? <SmallError error={this.state.error} /> : undefined}
125 {this.state.details}
126 </Panel>
127 )
128 }
129 }