forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderContractMetadata.tsx
More file actions
160 lines (148 loc) · 3.87 KB
/
Copy pathRenderContractMetadata.tsx
File metadata and controls
160 lines (148 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React from "react";
import { Box } from "../../components/layout/Box";
import { ContractMetadata } from "../util/loadContractMetada";
import {
Badge,
Card,
Input,
Label,
Link,
Table,
Text,
} from "@stellar/design-system";
interface RenderContractMetadataProps {
metadata?: ContractMetadata;
}
const metaDocsLink =
"https://developers.stellar.org/docs/learn/fundamentals/contract-development/overview#contract-meta";
enum ContractMetadataType {
SCEnvMetaEntry = "SCEnvMetaEntry",
SCMetaEntry = "SCMetaEntry",
}
interface TableItem {
metaType: ContractMetadataType;
val: string;
key: string;
id: string;
}
const RenderContractMetadata: React.FC<RenderContractMetadataProps> = ({
metadata,
}) => {
if (!metadata) return null;
const formatTableItems = (
data: unknown,
metaType: ContractMetadataType,
): TableItem[] => {
if (typeof data !== "object" || data === null) {
return [];
}
return Object.entries(data).map(([key, value]) => ({
key: String(key),
val: value as string,
id: String(key),
metaType: metaType,
}));
};
const getTableData = (): TableItem[] => {
const tableEntries: TableItem[] = [
...formatTableItems(
metadata.contractmetav0,
ContractMetadataType.SCMetaEntry,
),
...formatTableItems(
metadata.contractenvmetav0,
ContractMetadataType.SCEnvMetaEntry,
),
];
return tableEntries;
};
const renderTextCell = (item: string) => (
<td
style={{
paddingLeft: "1rem",
paddingRight: "1rem",
paddingTop: "0.5rem",
paddingBottom: "0.5rem",
}}
>
<Text as="p" size="xs">
{item}
</Text>
</td>
);
const renderBadgeCell = (item: string) => (
<td
style={{
paddingLeft: "1rem",
paddingRight: "1rem",
paddingTop: "0.5rem",
paddingBottom: "0.5rem",
}}
>
<Badge size="sm" variant="secondary">
{item}
</Badge>
</td>
);
const renderRow = (key: string, val: string, type: ContractMetadataType) => (
<>
{renderBadgeCell(type)}
{renderTextCell(key)}
{renderTextCell(val)}
</>
);
return (
<>
<Input
label="Contract Wasm Hash"
id="contract-wasm-hash"
fieldSize="md"
copyButton={{
position: "right",
}}
readOnly
value={metadata?.wasmHash}
/>
<Box gap="sm" direction="column">
<Label size="sm" htmlFor="contract-metadata">
Contract Metadata
</Label>
<Card variant="primary">
<Text as="span" size="xs">
This section contains the metadata of the contract, which is a
collection of key-value pairs that provide additional information
about the contract. This data is added to the contract during
compilation and can be retrieved directly from the WASM file. See{" "}
<Link href={metaDocsLink} target="_blank" rel="noopener noreferrer">
Contract Metadata Documentation
</Link>{" "}
for further details.
</Text>
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
marginTop: "1.5rem",
}}
>
<Table
breakpoint={300}
hideNumberColumn
columnLabels={[
{ id: "type", label: "Type" },
{ id: "key", label: "Key" },
{ id: "value", label: "Value" },
]}
data={getTableData()}
renderItemRow={(item: TableItem) =>
renderRow(item.key, item.val, item.metaType)
}
/>
</div>
</Card>
</Box>
</>
);
};
export default RenderContractMetadata;