|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { ccc } from "@ckb-ccc/connector-react"; |
| 3 | +import JsonView from "@uiw/react-json-view"; |
| 4 | +import { useApp } from "@/src/context"; |
| 5 | +import { Button } from "@/src/components/Button"; |
| 6 | +import { Textarea } from "@/src/components/Textarea"; |
| 7 | +import { darkTheme } from "@uiw/react-json-view/dark"; |
| 8 | +import { Dropdown } from "@/src/components/Dropdown"; |
| 9 | +export type UnpackType = |
| 10 | + | string |
| 11 | + | number |
| 12 | + | undefined |
| 13 | + | { [property: string]: UnpackType } |
| 14 | + | UnpackType[]; |
| 15 | + |
| 16 | +type Props = { |
| 17 | + codec: ccc.mol.Codec<any, any> | undefined; |
| 18 | + mode: "decode" | "encode"; |
| 19 | +}; |
| 20 | + |
| 21 | +const formatInput = (input: string): string => { |
| 22 | + if (!input.startsWith("0x")) { |
| 23 | + return `0x${input}`; |
| 24 | + } |
| 25 | + return input; |
| 26 | +}; |
| 27 | + |
| 28 | +const isBlank = (data: UnpackType): boolean => { |
| 29 | + if (!data) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + return false; |
| 33 | +}; |
| 34 | + |
| 35 | +export const DataInput: React.FC<Props> = ({ codec, mode }) => { |
| 36 | + const [inputData, setInputData] = useState<string>(""); |
| 37 | + const [decodeResult, setDecodeResult] = useState<UnpackType>(undefined); |
| 38 | + const [encodeResult, setEncodeResult] = useState<ccc.Hex | undefined>( |
| 39 | + undefined, |
| 40 | + ); |
| 41 | + const { createSender } = useApp(); |
| 42 | + const { log, error } = createSender("Molecule"); |
| 43 | + |
| 44 | + const handleDecode = () => { |
| 45 | + if (!codec) { |
| 46 | + error("please select codec"); |
| 47 | + return; |
| 48 | + } |
| 49 | + try { |
| 50 | + const result = codec.decode(formatInput(inputData)); |
| 51 | + log("Successfully decoded data"); |
| 52 | + setDecodeResult(result); |
| 53 | + } catch (e: unknown) { |
| 54 | + setDecodeResult(undefined); |
| 55 | + error((e as Error).message); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const handleEncode = () => { |
| 60 | + if (!codec) { |
| 61 | + error("please select codec"); |
| 62 | + return; |
| 63 | + } |
| 64 | + try { |
| 65 | + const inputObject = JSON.parse(inputData); |
| 66 | + const result = codec.encode(inputObject); |
| 67 | + log("Successfully encoded data"); |
| 68 | + setEncodeResult(ccc.hexFrom(result)); |
| 69 | + } catch (e: unknown) { |
| 70 | + setEncodeResult(undefined); |
| 71 | + error((e as Error).message); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // If mode changes, clear the input data |
| 76 | + useEffect(() => { |
| 77 | + setInputData(""); |
| 78 | + setDecodeResult(undefined); |
| 79 | + setEncodeResult(undefined); |
| 80 | + }, [mode]); |
| 81 | + |
| 82 | + return ( |
| 83 | + <div> |
| 84 | + <div style={{ marginBottom: 16 }}> |
| 85 | + <label htmlFor="input-data">Input data</label> |
| 86 | + <div> |
| 87 | + <Textarea |
| 88 | + id="input-data" |
| 89 | + state={[inputData, setInputData]} |
| 90 | + placeholder={ |
| 91 | + mode === "decode" ? "0x..." : "Please input data in JSON Object" |
| 92 | + } |
| 93 | + /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + |
| 97 | + {mode === "decode" && ( |
| 98 | + <div style={{ marginBottom: 16 }}> |
| 99 | + <Button type="button" onClick={handleDecode}> |
| 100 | + Decode |
| 101 | + </Button> |
| 102 | + </div> |
| 103 | + )} |
| 104 | + |
| 105 | + {mode === "encode" && ( |
| 106 | + <div style={{ marginBottom: 16 }}> |
| 107 | + <Button type="button" onClick={handleEncode}> |
| 108 | + Encode |
| 109 | + </Button> |
| 110 | + </div> |
| 111 | + )} |
| 112 | + |
| 113 | + {!isBlank(decodeResult) && ( |
| 114 | + <div> |
| 115 | + <JsonView |
| 116 | + value={ |
| 117 | + typeof decodeResult === "object" |
| 118 | + ? decodeResult |
| 119 | + : { value: decodeResult } |
| 120 | + } |
| 121 | + style={darkTheme} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + )} |
| 125 | + |
| 126 | + {encodeResult && ( |
| 127 | + <div> |
| 128 | + <JsonView value={{ value: encodeResult }} style={darkTheme} /> |
| 129 | + </div> |
| 130 | + )} |
| 131 | + </div> |
| 132 | + ); |
| 133 | +}; |
0 commit comments