|
1 | | -import { CardWithTextTitle } from "@/components/Card"; |
2 | | -import { useState } from "react"; |
| 1 | +import { CardWithTabsTitle } from "@/components/Card"; |
| 2 | +import { EditableJSON } from "@/components/JWPVisualizer"; |
| 3 | +import { useEffect, useState } from "react"; |
3 | 4 |
|
4 | | -type PayloadItem = { |
5 | | - claim: string; |
6 | | - decoded: string; |
7 | | - raw: string; |
8 | | - disclosed: boolean; |
| 5 | +type Payload = { |
| 6 | + claims: string[]; |
| 7 | + decodedString: string; |
| 8 | + disclosed: boolean[]; |
9 | 9 | }; |
10 | 10 |
|
11 | 11 | type PayloadInputProps = { |
12 | | - payload: PayloadItem[]; |
13 | | - onChange?: (payload: PayloadItem[]) => void; |
| 12 | + payload: Payload; |
| 13 | + onChange?: (payload: Payload) => void; |
14 | 14 | }; |
15 | 15 |
|
16 | 16 | export const PayloadInput: React.FC<PayloadInputProps> = ({ |
17 | 17 | payload, |
18 | 18 | onChange, |
19 | 19 | }) => { |
20 | | - const [tableData, setTableData] = useState<PayloadItem[]>(payload); |
| 20 | + const [activeTabKey, setActiveTabKey] = useState<"json" | "table">("json"); |
21 | 21 |
|
22 | | - const handleDecodedChange = (index: number, newDecoded: string) => { |
23 | | - setTableData((prev) => |
24 | | - prev.map((item, i) => |
25 | | - i === index ? { ...item, decoded: newDecoded } : item, |
26 | | - ), |
27 | | - ); |
28 | | - onChange?.(tableData); |
| 22 | + const [decodedString, setDecodedString] = useState<string>( |
| 23 | + payload.decodedString, |
| 24 | + ); |
| 25 | + |
| 26 | + const [disclosed, setDisclosed] = useState<boolean[]>(payload.disclosed); |
| 27 | + |
| 28 | + const handleJsonChange = (newJsonString: string) => { |
| 29 | + setDecodedString(newJsonString); |
| 30 | + |
| 31 | + try { |
| 32 | + JSON.parse(newJsonString); |
| 33 | + |
| 34 | + onChange?.({ |
| 35 | + claims: payload.claims, |
| 36 | + decodedString: newJsonString, |
| 37 | + disclosed, |
| 38 | + }); |
| 39 | + } catch (error) { |
| 40 | + console.warn("JSON Parse Error", error); |
| 41 | + } |
29 | 42 | }; |
30 | 43 |
|
31 | 44 | const handleDisclosedChange = (index: number, newVal: boolean) => { |
32 | | - setTableData((prev) => |
33 | | - prev.map((item, i) => |
34 | | - i === index ? { ...item, disclosed: newVal } : item, |
35 | | - ), |
36 | | - ); |
| 45 | + const newDisclosed = [...disclosed]; |
| 46 | + newDisclosed[index] = newVal; |
| 47 | + setDisclosed(newDisclosed); |
| 48 | + |
| 49 | + onChange?.({ |
| 50 | + claims: payload.claims, |
| 51 | + decodedString, |
| 52 | + disclosed: newDisclosed, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const getTableData = () => { |
| 57 | + try { |
| 58 | + const parsedDecoded = JSON.parse(decodedString); |
| 59 | + if (Array.isArray(parsedDecoded)) { |
| 60 | + return payload.claims.map((claim, index) => ({ |
| 61 | + claim, |
| 62 | + decodedValue: |
| 63 | + parsedDecoded[index] !== undefined |
| 64 | + ? typeof parsedDecoded[index] === "object" |
| 65 | + ? JSON.stringify(parsedDecoded[index]) |
| 66 | + : String(parsedDecoded[index]) |
| 67 | + : "", |
| 68 | + disclosed: disclosed[index] || false, |
| 69 | + })); |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + console.warn("Table Data Generation Error:", error); |
| 73 | + } |
| 74 | + // Fallback if JSON parsing fails or not an array |
| 75 | + return payload.claims.map((claim, index) => ({ |
| 76 | + claim, |
| 77 | + decodedValue: "", |
| 78 | + disclosed: disclosed[index] || false, |
| 79 | + })); |
37 | 80 | }; |
38 | 81 |
|
| 82 | + // Synchronization process when payload changes |
| 83 | + useEffect(() => { |
| 84 | + setDecodedString(payload.decodedString); |
| 85 | + setDisclosed([...payload.disclosed]); |
| 86 | + }, [payload.decodedString, payload.disclosed]); |
| 87 | + |
39 | 88 | return ( |
40 | | - <CardWithTextTitle title="PAYLOAD (Check to disclose)"> |
41 | | - <table className="min-w-full border-collapse border text-xs"> |
42 | | - <thead className="bg-gray-50"> |
43 | | - <tr> |
44 | | - <th className="border px-2 py-1 text-left">Claim</th> |
45 | | - <th className="border px-2 py-1 text-left">Decoded</th> |
46 | | - <th className="border px-2 py-1 text-center" /> |
47 | | - </tr> |
48 | | - </thead> |
49 | | - <tbody> |
50 | | - {tableData.map((item, index) => ( |
51 | | - <tr key={index} className={!item.disclosed ? "bg-gray-300" : ""}> |
52 | | - <td className="border px-2 py-1">{item.claim}</td> |
53 | | - <td className="border px-2 py-1"> |
54 | | - <input |
55 | | - className="w-full rounded border px-2 py-1" |
56 | | - value={item.decoded} |
57 | | - onChange={(e) => handleDecodedChange(index, e.target.value)} |
58 | | - /> |
59 | | - </td> |
60 | | - <td className="border px-1 text-center"> |
61 | | - <input |
62 | | - type="checkbox" |
63 | | - checked={item.disclosed} |
64 | | - onChange={(e) => |
65 | | - handleDisclosedChange(index, e.target.checked) |
66 | | - } |
67 | | - /> |
68 | | - </td> |
| 89 | + <CardWithTabsTitle |
| 90 | + activeTabKey={activeTabKey} |
| 91 | + tabTitles={{ json: "JSON", table: "TABLE (Check to disclose)" }} |
| 92 | + onTabClick={(item) => setActiveTabKey(item as "json" | "table")} |
| 93 | + > |
| 94 | + {activeTabKey === "json" ? ( |
| 95 | + <div className="p-4"> |
| 96 | + <EditableJSON jsonData={decodedString} onChange={handleJsonChange} /> |
| 97 | + </div> |
| 98 | + ) : ( |
| 99 | + <table className="min-w-full border-collapse border text-xs"> |
| 100 | + <thead className="bg-gray-50"> |
| 101 | + <tr> |
| 102 | + <th className="border px-2 py-1 text-left">Claim</th> |
| 103 | + <th className="border px-2 py-1 text-left">Decoded Value</th> |
| 104 | + <th className="border px-2 py-1 text-center" /> |
69 | 105 | </tr> |
70 | | - ))} |
71 | | - </tbody> |
72 | | - </table> |
73 | | - </CardWithTextTitle> |
| 106 | + </thead> |
| 107 | + <tbody> |
| 108 | + {getTableData().map((item, index) => ( |
| 109 | + <tr key={index} className={!item.disclosed ? "bg-gray-100" : ""}> |
| 110 | + <td className="border px-2 py-1">{item.claim}</td> |
| 111 | + <td className="border px-2 py-1"> |
| 112 | + <div className="w-full overflow-x-auto px-2 py-1 font-mono"> |
| 113 | + {item.decodedValue} |
| 114 | + </div> |
| 115 | + </td> |
| 116 | + <td className="border px-1 text-center"> |
| 117 | + <input |
| 118 | + type="checkbox" |
| 119 | + checked={item.disclosed} |
| 120 | + onChange={(e) => |
| 121 | + handleDisclosedChange(index, e.target.checked) |
| 122 | + } |
| 123 | + /> |
| 124 | + </td> |
| 125 | + </tr> |
| 126 | + ))} |
| 127 | + </tbody> |
| 128 | + </table> |
| 129 | + )} |
| 130 | + </CardWithTabsTitle> |
74 | 131 | ); |
75 | 132 | }; |
0 commit comments