Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract Explorer: Contract storage filters #1233

Merged
merged 12 commits into from
Feb 3, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { useSEContractStorage } from "@/query/external/useSEContracStorage";
import { formatEpochToDate } from "@/helpers/formatEpochToDate";
import { formatNumber } from "@/helpers/formatNumber";
import { capitalizeString } from "@/helpers/capitalizeString";
import { decodeScVal } from "@/helpers/decodeScVal";

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

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

export const ContractStorage = ({
isActive,
Expand Down Expand Up @@ -60,18 +65,81 @@ export const ContractStorage = ({
);
}

const parsedKeyValueData = () => {
return storageData.map((i) => ({
jeesunikim marked this conversation as resolved.
Show resolved Hide resolved
...i,
keyJson: i.key ? decodeScVal(i.key) : undefined,
jeesunikim marked this conversation as resolved.
Show resolved Hide resolved
valueJson: i.value ? decodeScVal(i.value) : undefined,
quietbits marked this conversation as resolved.
Show resolved Hide resolved
}));
};

const parsedData = parsedKeyValueData();

const getKeyValueFilters = () => {
return parsedData.reduce(
(
res: {
key: string[];
value: string[];
},
cur,
) => {
// Key
if (cur.keyJson && Array.isArray(cur.keyJson)) {
const keyFilter = cur.keyJson[0];

if (!res.key.includes(keyFilter)) {
res.key = [...res.key, keyFilter];
}
}

// Value
if (cur.valueJson && typeof cur.valueJson === "object") {
// Excluding keys that start with _ because on the UI structure is
// different. For example, for Instance type.
const valueFilters = Object.keys(cur.valueJson).filter(
(f) => !f.startsWith("_"),
);

valueFilters.forEach((v) => {
if (!res.value.includes(v)) {
res.value = [...res.value, v];
}
});
}

return res;
},
{ key: [], value: [] },
);
};

const keyValueFilters = getKeyValueFilters();

return (
<DataTable
tableId="contract-storage"
tableData={storageData}
tableData={parsedData}
tableHeaders={[
{ id: "key", value: "Key", isSortable: false },
{ id: "value", value: "Value", isSortable: false },
{
id: "key",
value: "Key",
isSortable: false,
filter: keyValueFilters.key,
},
{
id: "value",
value: "Value",
isSortable: false,
filter: keyValueFilters.value,
},
{ id: "durability", value: "Durability", isSortable: true },
{ id: "ttl", value: "TTL", isSortable: true },
{ id: "updated", value: "Updated", isSortable: true },
]}
formatDataRow={(vh: ContractStorageResponseItem) => [
formatDataRow={(
vh: ContractStorageProcessedItem<ContractStorageResponseItem>,
) => [
{
value: (
<div className="CodeBox">
Expand Down
Loading