Skip to content

Commit 9d9c109

Browse files
committed
Format sc key and value
1 parent 5776c65 commit 9d9c109

File tree

8 files changed

+527
-38
lines changed

8 files changed

+527
-38
lines changed

src/app/(sidebar)/smart-contracts/contract-explorer/components/ContractStorage.tsx

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Loader, Text } from "@stellar/design-system";
2-
import { scValToNative, xdr } from "@stellar/stellar-sdk";
3-
import { stringify } from "lossless-json";
42

53
import { ErrorText } from "@/components/ErrorText";
64
import { Box } from "@/components/layout/Box";
75
import { DataTable } from "@/components/DataTable";
6+
import { ScValPrettyJson } from "@/components/ScValPrettyJson";
87

98
import { useSEContractStorage } from "@/query/external/useSEContracStorage";
109
import { formatEpochToDate } from "@/helpers/formatEpochToDate";
1110
import { formatNumber } from "@/helpers/formatNumber";
1211
import { capitalizeString } from "@/helpers/capitalizeString";
1312

13+
import { useIsXdrInit } from "@/hooks/useIsXdrInit";
14+
1415
import { ContractStorageResponseItem, NetworkType } from "@/types/types";
1516

1617
export const ContractStorage = ({
@@ -24,6 +25,8 @@ export const ContractStorage = ({
2425
networkId: NetworkType;
2526
totalEntriesCount: number | undefined;
2627
}) => {
28+
const isXdrInit = useIsXdrInit();
29+
2730
const {
2831
data: storageData,
2932
error: storageError,
@@ -35,6 +38,7 @@ export const ContractStorage = ({
3538
contractId,
3639
totalEntriesCount,
3740
});
41+
3842
// Loading, error, and no data states
3943
if (isStorageLoading || isStorageFetching) {
4044
return (
@@ -51,32 +55,14 @@ export const ContractStorage = ({
5155
if (!storageData || storageData.length === 0) {
5256
return (
5357
<Text as="div" size="sm">
54-
No version history
58+
No contract storage
5559
</Text>
5660
);
5761
}
5862

59-
const formatScv = (xdrString: string) => {
60-
// TODO: handle formatting
61-
try {
62-
const scv = xdr.ScVal.fromXDR(xdrString, "base64");
63-
64-
// TODO: handle instance
65-
const res = scValToNative(scv);
66-
67-
if (typeof res === "object") {
68-
return stringify(res);
69-
}
70-
71-
return res;
72-
} catch (e: any) {
73-
return xdrString;
74-
}
75-
};
76-
7763
return (
7864
<DataTable
79-
tableId="contract-version-history"
65+
tableId="contract-storage"
8066
tableData={storageData}
8167
tableHeaders={[
8268
{ id: "key", value: "Key", isSortable: false },
@@ -86,8 +72,20 @@ export const ContractStorage = ({
8672
{ id: "updated", value: "Updated", isSortable: true },
8773
]}
8874
formatDataRow={(vh: ContractStorageResponseItem) => [
89-
{ value: <div>{formatScv(vh.key)}</div> },
90-
{ value: <div>{formatScv(vh.value)}</div> },
75+
{
76+
value: (
77+
<div className="CodeBox">
78+
<ScValPrettyJson xdrString={vh.key} isReady={isXdrInit} />
79+
</div>
80+
),
81+
},
82+
{
83+
value: (
84+
<div className="CodeBox">
85+
<ScValPrettyJson xdrString={vh.value} isReady={isXdrInit} />
86+
</div>
87+
),
88+
},
9189
{ value: capitalizeString(vh.durability) },
9290
{ value: formatNumber(vh.ttl) },
9391
{ value: formatEpochToDate(vh.updated, "short") || "-" },

src/components/DataTable/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const DataTable = <T,>({
1818
tableData: T[];
1919
formatDataRow: (item: T) => DataTableCell[];
2020
}) => {
21-
const PAGE_SIZE = 10;
21+
const PAGE_SIZE = 20;
2222
const tableDataSize = tableData.length;
2323

2424
// Sort by
@@ -161,6 +161,8 @@ export const DataTable = <T,>({
161161
</Card>
162162

163163
<Box gap="lg" direction="row" align="center" justify="end">
164+
{/* TODO: add color legend */}
165+
164166
{/* Pagination */}
165167
<Box gap="xs" direction="row" align="center">
166168
<Button

src/components/DataTable/styles.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
td,
2929
th {
30-
padding: pxToRem(8px) pxToRem(12px);
30+
padding: pxToRem(8px) pxToRem(6px);
3131
white-space: nowrap;
3232
overflow: hidden;
3333
text-overflow: ellipsis;

0 commit comments

Comments
 (0)