1- import React , { useContext , useMemo } from "react" ;
1+ import React , { useContext , useMemo , useState } from "react" ;
22import clsx from "clsx" ;
33import CodeBlock from "@theme/CodeBlock" ;
44import { MethodParam } from "@site/src/components/ParserOpenRPC/interfaces" ;
55import styles from "./styles.module.css" ;
66import global from "../global.module.css" ;
77import { Tooltip } from "@site/src/components/Tooltip" ;
88import { MetamaskProviderContext } from "@site/src/theme/Root" ;
9+ import Select from "react-dropdown-select" ;
910
1011interface RequestBoxProps {
1112 isMetamaskInstalled : boolean ;
@@ -22,6 +23,17 @@ interface RequestBoxProps {
2223 isLoading : boolean ;
2324}
2425
26+ const langOptions = [
27+ {
28+ value : "curl" ,
29+ label : "CURL"
30+ } ,
31+ {
32+ value : "wss" ,
33+ label : "WSS"
34+ }
35+ ]
36+
2537export default function RequestBox ( {
2638 isMetamaskInstalled,
2739 method,
@@ -37,15 +49,19 @@ export default function RequestBox({
3749 isLoading,
3850} : RequestBoxProps ) {
3951 const { userAPIKey } = useContext ( MetamaskProviderContext ) ;
52+ const [ currentLang , setCurrentLang ] = useState ( langOptions [ 0 ] ) ;
4053 const exampleRequest = useMemo ( ( ) => {
4154 const preparedParams = JSON . stringify ( paramsData , null , 2 ) ;
4255 const preparedShellParams = JSON . stringify ( paramsData ) ;
4356 const API_KEY = userAPIKey ? userAPIKey : "<YOUR-API-KEY>" ;
4457 if ( isMetamaskNetwork ) {
4558 return `await window.ethereum.request({\n "method": "${ method } ",\n "params": ${ preparedParams . replace ( / " ( [ ^ " ] + ) " : / g, '$1:' ) } ,\n});` ;
4659 }
60+ if ( currentLang . value === "wss" ) {
61+ return `wscat -c ${ requestURL . replace ( "https" , "wss" ) } ${ API_KEY } -x '{"jsonrpc":"2.0","method":"${ method } ","params": ${ preparedShellParams } ,"id":1}'`
62+ }
4763 return `curl ${ requestURL } ${ API_KEY } \\\n -X POST \\\n -H "Content-Type: application/json" \\\n -d '{\n "jsonrpc": "2.0",\n "method": "${ method } ",\n "params": ${ preparedShellParams } ,\n "id": 1\n }'` ;
48- } , [ userAPIKey , method , paramsData ] ) ;
64+ } , [ userAPIKey , method , paramsData , currentLang ] ) ;
4965
5066 const exampleResponse = useMemo ( ( ) => {
5167 if ( defExampleResponse && response === undefined ) {
@@ -80,8 +96,20 @@ export default function RequestBox({
8096 return (
8197 < >
8298 < div className = { styles . cardWrapper } >
83- < div className = { styles . cardHeader } >
99+ < div className = { clsx ( styles . cardHeader , ! isMetamaskNetwork && styles . addIndent ) } >
84100 < strong className = { styles . cardHeading } > Request</ strong >
101+ { ! isMetamaskNetwork && (
102+ < Select
103+ className = { "select-lang" }
104+ options = { langOptions }
105+ values = { [ currentLang ] }
106+ multi = { false }
107+ searchable = { false }
108+ onChange = { ( value ) => {
109+ setCurrentLang ( value [ 0 ] ) ;
110+ } }
111+ />
112+ ) }
85113 </ div >
86114 < div className = { styles . codeWrapper } >
87115 < CodeBlock
0 commit comments