Skip to content

Commit

Permalink
Format sc key and value
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits committed Jan 16, 2025
1 parent 5776c65 commit 9d9c109
Show file tree
Hide file tree
Showing 8 changed files with 527 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Loader, Text } from "@stellar/design-system";
import { scValToNative, xdr } from "@stellar/stellar-sdk";
import { stringify } from "lossless-json";

import { ErrorText } from "@/components/ErrorText";
import { Box } from "@/components/layout/Box";
import { DataTable } from "@/components/DataTable";
import { ScValPrettyJson } from "@/components/ScValPrettyJson";

import { useSEContractStorage } from "@/query/external/useSEContracStorage";
import { formatEpochToDate } from "@/helpers/formatEpochToDate";
import { formatNumber } from "@/helpers/formatNumber";
import { capitalizeString } from "@/helpers/capitalizeString";

import { useIsXdrInit } from "@/hooks/useIsXdrInit";

import { ContractStorageResponseItem, NetworkType } from "@/types/types";

export const ContractStorage = ({
Expand All @@ -24,6 +25,8 @@ export const ContractStorage = ({
networkId: NetworkType;
totalEntriesCount: number | undefined;
}) => {
const isXdrInit = useIsXdrInit();

const {
data: storageData,
error: storageError,
Expand All @@ -35,6 +38,7 @@ export const ContractStorage = ({
contractId,
totalEntriesCount,
});

// Loading, error, and no data states
if (isStorageLoading || isStorageFetching) {
return (
Expand All @@ -51,32 +55,14 @@ export const ContractStorage = ({
if (!storageData || storageData.length === 0) {
return (
<Text as="div" size="sm">
No version history
No contract storage
</Text>
);
}

const formatScv = (xdrString: string) => {
// TODO: handle formatting
try {
const scv = xdr.ScVal.fromXDR(xdrString, "base64");

// TODO: handle instance
const res = scValToNative(scv);

if (typeof res === "object") {
return stringify(res);
}

return res;
} catch (e: any) {
return xdrString;
}
};

return (
<DataTable
tableId="contract-version-history"
tableId="contract-storage"
tableData={storageData}
tableHeaders={[
{ id: "key", value: "Key", isSortable: false },
Expand All @@ -86,8 +72,20 @@ export const ContractStorage = ({
{ id: "updated", value: "Updated", isSortable: true },
]}
formatDataRow={(vh: ContractStorageResponseItem) => [
{ value: <div>{formatScv(vh.key)}</div> },
{ value: <div>{formatScv(vh.value)}</div> },
{
value: (
<div className="CodeBox">
<ScValPrettyJson xdrString={vh.key} isReady={isXdrInit} />
</div>
),
},
{
value: (
<div className="CodeBox">
<ScValPrettyJson xdrString={vh.value} isReady={isXdrInit} />
</div>
),
},
{ value: capitalizeString(vh.durability) },
{ value: formatNumber(vh.ttl) },
{ value: formatEpochToDate(vh.updated, "short") || "-" },
Expand Down
4 changes: 3 additions & 1 deletion src/components/DataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DataTable = <T,>({
tableData: T[];
formatDataRow: (item: T) => DataTableCell[];
}) => {
const PAGE_SIZE = 10;
const PAGE_SIZE = 20;
const tableDataSize = tableData.length;

// Sort by
Expand Down Expand Up @@ -161,6 +161,8 @@ export const DataTable = <T,>({
</Card>

<Box gap="lg" direction="row" align="center" justify="end">
{/* TODO: add color legend */}

{/* Pagination */}
<Box gap="xs" direction="row" align="center">
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/components/DataTable/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

td,
th {
padding: pxToRem(8px) pxToRem(12px);
padding: pxToRem(8px) pxToRem(6px);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
Loading

0 comments on commit 9d9c109

Please sign in to comment.