Skip to content

Commit

Permalink
Contract Explorer: Contract info
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits committed Dec 18, 2024
1 parent da2ec12 commit 4308bf8
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 113 deletions.
41 changes: 7 additions & 34 deletions src/app/(sidebar)/account/fund/components/SwitchNetwork.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
"use client";

import { Card, Text, Button } from "@stellar/design-system";

import { NetworkOptions } from "@/constants/settings";
import { useStore } from "@/store/useStore";

import { NetworkType } from "@/types/types";
import { Card, Text } from "@stellar/design-system";
import { SwitchNetworkButtons } from "@/components/SwitchNetworkButtons";

import "../../styles.scss";

export const SwitchNetwork = () => {
const { selectNetwork, updateIsDynamicNetworkSelect } = useStore();
const onSwitchNetwork = (network: NetworkType) => {
const selectedNetwork = NetworkOptions.find((n) => n.id === network);
if (selectedNetwork) {
updateIsDynamicNetworkSelect(true);
selectNetwork(selectedNetwork);
}
};

return (
<Card>
<div className="Account__card">
Expand All @@ -33,26 +20,12 @@ export const SwitchNetwork = () => {
fund an account with 10,000 lumens on the futurenet network.
</Text>
</div>
<div className="Account__CTA" data-testid="fundAccount-buttons">
<Button
size="md"
variant="tertiary"
onClick={() => {
onSwitchNetwork("futurenet");
}}
>
Switch to Futurenet
</Button>

<Button
size="md"
variant="tertiary"
onClick={() => {
onSwitchNetwork("testnet");
}}
>
Switch to Testnet
</Button>
<div className="Account__CTA" data-testid="fundAccount-buttons">
<SwitchNetworkButtons
includedNetworks={["futurenet", "testnet"]}
buttonSize="md"
/>
</div>
</div>
</Card>
Expand Down
24 changes: 5 additions & 19 deletions src/app/(sidebar)/account/saved/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { InputSideElement } from "@/components/InputSideElement";
import { SavedItemTimestampAndDelete } from "@/components/SavedItemTimestampAndDelete";
import { PageCard } from "@/components/layout/PageCard";
import { SaveToLocalStorageModal } from "@/components/SaveToLocalStorageModal";
import { SwitchNetworkButtons } from "@/components/SwitchNetworkButtons";

import { localStorageSavedKeypairs } from "@/helpers/localStorageSavedKeypairs";
import { arrayItem } from "@/helpers/arrayItem";
Expand Down Expand Up @@ -131,25 +132,10 @@ export default function SavedKeypairs() {
</Text>

<Box gap="sm" direction="row" wrap="wrap">
<Button
variant="tertiary"
size="sm"
onClick={() => {
getAndSetNetwork("futurenet");
}}
>
Switch to Futurenet
</Button>

<Button
variant="tertiary"
size="sm"
onClick={() => {
getAndSetNetwork("testnet");
}}
>
Switch to Testnet
</Button>
<SwitchNetworkButtons
includedNetworks={["futurenet", "testnet"]}
buttonSize="md"
/>
</Box>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { Avatar, Card, Icon, Logo } from "@stellar/design-system";
import { Box } from "@/components/layout/Box";
import { SdsLink } from "@/components/SdsLink";
import { formatEpochToDate } from "@/helpers/formatEpochToDate";
import { formatNumber } from "@/helpers/formatNumber";
import { stellarExpertAccountLink } from "@/helpers/stellarExpertAccountLink";
import { NetworkType } from "@/types/types";

export const ContractInfo = ({
infoData,
networkId,
}: {
infoData: any;
networkId: NetworkType;
}) => {
type ContractExplorerInfoField = {
id: string;
label: string;
};

const INFO_FIELDS: ContractExplorerInfoField[] = [
{
id: "repository",
label: "Source Code",
},
{
id: "created",
label: "Created",
},
{
id: "wasm",
label: "WASM Hash",
},
{
id: "versions",
label: "Versions",
},
{
id: "creator",
label: "Creator",
},
{
id: "storage_entries",
label: "Data Storage",
},
];

const InfoFieldItem = ({
label,
value,
}: {
label: string;
value: React.ReactNode;
}) => {
return (
<Box
gap="lg"
direction="row"
align="center"
addlClassName="InfoFieldItem"
>
<div className="InfoFieldItem__label">{label}</div>
<div className="InfoFieldItem__value">{value ?? "-"}</div>
</Box>
);
};

const formatEntriesText = (entriesCount: number) => {
if (!entriesCount) {
return "";
}

if (entriesCount === 1) {
return `1 entry`;
}

return `${formatNumber(entriesCount)} entries`;
};

const renderInfoField = (field: ContractExplorerInfoField) => {
switch (field.id) {
case "repository":
return (
<InfoFieldItem
key={field.id}
label={field.label}
value={
infoData.validation.repository ? (
<SdsLink
href={infoData.validation.repository}
addlClassName="Link--external"
>
<Logo.Github />
{infoData.validation.repository.replace(
"https://github.com/",
"",
)}
<Icon.LinkExternal01 />
</SdsLink>
) : null
}
/>
);
case "created":
return (
<InfoFieldItem
key={field.id}
label={field.label}
value={
infoData.created ? formatEpochToDate(infoData.created) : null
}
/>
);
case "wasm":
return (
<InfoFieldItem
key={field.id}
label={field.label}
value={infoData.wasm}
/>
);
case "versions":
return (
<InfoFieldItem
key={field.id}
label={field.label}
value={infoData.versions}
/>
);
case "creator":
return (
<InfoFieldItem
key={field.id}
label={field.label}
value={
infoData.creator ? (
<SdsLink
href={stellarExpertAccountLink(infoData.creator, networkId)}
>
<Avatar publicAddress={infoData.creator} size="sm" />
{infoData.creator}
<Icon.LinkExternal01 />
</SdsLink>
) : null
}
/>
);
case "storage_entries":
return (
<InfoFieldItem
key={field.id}
label={field.label}
// TODO: link to Contract Storage tab when ready
value={
infoData.storage_entries
? formatEntriesText(infoData.storage_entries)
: null
}
/>
);
default:
return null;
}
};

return (
<Card>
<Box gap="xs">
<>{INFO_FIELDS.map((f) => renderInfoField(f))}</>
</Box>
</Card>
);
};
Loading

0 comments on commit 4308bf8

Please sign in to comment.