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

feat: add codes table of an account #221

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#221](https://github.com/alleslabs/celatone-frontend/pull/221) Add codes of an account
- [#218](https://github.com/alleslabs/celatone-frontend/pull/218) Add instantiated and admin contracts of an account
- [#192](https://github.com/alleslabs/celatone-frontend/pull/192) Add alternative sidebar with only icons
- [#210](https://github.com/alleslabs/celatone-frontend/pull/210) New design for token card, currently support price
Expand Down
18 changes: 18 additions & 0 deletions src/lib/components/table/codes/CodesTableHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { GridProps } from "@chakra-ui/react";
import { Grid } from "@chakra-ui/react";

import { TableHeader } from "lib/components/table";

export const CodesTableHeader = ({
templateColumns,
}: {
templateColumns: GridProps["templateColumns"];
}) => (
<Grid templateColumns={templateColumns} minW="min-content">
<TableHeader borderTopStyle="none">Code ID</TableHeader>
<TableHeader>Code Name</TableHeader>
<TableHeader>Contracts</TableHeader>
<TableHeader>Uploader</TableHeader>
<TableHeader>Permission</TableHeader>
</Grid>
);
83 changes: 83 additions & 0 deletions src/lib/components/table/codes/CodesTableRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Flex, Text, Grid, HStack } from "@chakra-ui/react";

import { useInternalNavigate } from "lib/app-provider";
import { InstantiateButton } from "lib/components/button";
import { CodeNameCell } from "lib/components/CodeNameCell";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { SaveOrRemoveCodeModal } from "lib/components/modal/code/SaveOrRemoveCode";
import { PermissionChip } from "lib/components/PermissionChip";
import { TableRow } from "lib/components/table/tableComponents";
import type { CodeInfo } from "lib/types";

interface CodesTableRowProps {
codeInfo: CodeInfo;
templateColumns: string;
}

export const CodesTableRow = ({
codeInfo,
templateColumns,
}: CodesTableRowProps) => {
const navigate = useInternalNavigate();

return (
<Grid
templateColumns={templateColumns}
onClick={() =>
navigate({
pathname: `/code/${codeInfo.id}`,
})
}
songwongtp marked this conversation as resolved.
Show resolved Hide resolved
_hover={{ bg: "pebble.900" }}
transition="all .25s ease-in-out"
cursor="pointer"
minW="min-content"
>
<TableRow>
<ExplorerLink
type="code_id"
value={codeInfo.id.toString()}
canCopyWithHover
/>
</TableRow>
<TableRow>
<CodeNameCell code={codeInfo} />
</TableRow>
<TableRow>
<Text
variant="body2"
onClick={(e) => e.stopPropagation()}
cursor="text"
w="fit-content"
m="auto"
px={2}
>
{codeInfo.contractCount ?? "N/A"}
</Text>
</TableRow>
<TableRow>
<ExplorerLink
value={codeInfo.uploader}
type="user_address"
canCopyWithHover
/>
</TableRow>
<TableRow>
<Flex justify="space-between" align="center" w="full">
<PermissionChip
instantiatePermission={codeInfo.instantiatePermission}
permissionAddresses={codeInfo.permissionAddresses}
/>
<HStack onClick={(e) => e.stopPropagation()}>
<InstantiateButton
instantiatePermission={codeInfo.instantiatePermission}
permissionAddresses={codeInfo.permissionAddresses}
codeId={codeInfo.id}
/>
<SaveOrRemoveCodeModal codeInfo={codeInfo} />
</HStack>
</Flex>
</TableRow>
</Grid>
);
};
2 changes: 2 additions & 0 deletions src/lib/components/table/codes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./CodesTableHeader";
export * from "./CodesTableRow";
19 changes: 19 additions & 0 deletions src/lib/components/table/contracts/ContractsTableHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { GridProps } from "@chakra-ui/react";
import { Grid } from "@chakra-ui/react";

import { TableHeader } from "lib/components/table";

export const ContractsTableHeader = ({
templateColumns,
}: {
templateColumns: GridProps["templateColumns"];
}) => (
<Grid templateColumns={templateColumns} minW="min-content">
<TableHeader borderTopStyle="none">Contract Address</TableHeader>
<TableHeader>Contract Name</TableHeader>
<TableHeader>Tags</TableHeader>
<TableHeader>Instantiator</TableHeader>
<TableHeader>Timestamp</TableHeader>
<TableHeader />
</Grid>
);
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ const StyledIconButton = chakra(IconButton, {
},
});

interface ContractTableRowProps {
interface ContractsTableRowProps {
contractInfo: ContractInfo;
templateColumnsStyle: string;
templateColumns: string;
}

const InstantiatorRender = ({
contractInfo: { remark, latestUpdater },
}: Pick<ContractTableRowProps, "contractInfo">) => {
}: Pick<ContractsTableRowProps, "contractInfo">) => {
const getAddressType = useGetAddressType();

/**
Expand Down Expand Up @@ -83,15 +83,15 @@ const InstantiatorRender = ({
}
};

export const ContractTableRow = ({
export const ContractsTableRow = ({
contractInfo,
templateColumnsStyle,
}: ContractTableRowProps) => {
templateColumns,
}: ContractsTableRowProps) => {
const navigate = useInternalNavigate();

return (
<Grid
templateColumns={templateColumnsStyle}
templateColumns={templateColumns}
onClick={() =>
navigate({
pathname: `/contract/${contractInfo.contractAddress}`,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/table/contracts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ContractsTableHeader";
export * from "./ContractsTableRow";
4 changes: 2 additions & 2 deletions src/lib/model/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { HumanAddr } from "lib/types";
* Counts for stored codes, contract admin, contract instances, transactions, and opened proposals tables
*/
export const useAccountDetailsTableCounts = (walletAddress: HumanAddr) => {
const { data: codesCount, refetch: refetchCodes } =
const { data: codesCount, refetch: refetchCodesCount } =
useCodeListCountByWalletAddress(walletAddress);
const { data: contractsAdminCount, refetch: refetchContractsAdminCount } =
useContractListCountByAdmin(walletAddress);
Expand Down Expand Up @@ -44,7 +44,7 @@ export const useAccountDetailsTableCounts = (walletAddress: HumanAddr) => {
countTxs,
proposalCount,
},
refetchCodes,
refetchCodesCount,
refetchContractsAdminCount,
refetchContractsCount,
refetchCountTxs,
Expand Down
124 changes: 124 additions & 0 deletions src/lib/pages/account-details/components/tables/codes/CodesTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Box, Flex } from "@chakra-ui/react";
import { observer } from "mobx-react-lite";
import type { ChangeEvent } from "react";

import { Loading } from "lib/components/Loading";
import { Pagination } from "lib/components/pagination";
import { usePaginator } from "lib/components/pagination/usePaginator";
import { EmptyState } from "lib/components/state/EmptyState";
import { TableContainer } from "lib/components/table";
import { CodesTableHeader, CodesTableRow } from "lib/components/table/codes";
import { TableTitle } from "lib/components/table/TableTitle";
import { ViewMore } from "lib/components/table/ViewMore";
import { useCodeStored } from "lib/pages/account-details/data";
import type { HumanAddr, Option } from "lib/types";

interface CodesTableProps {
walletAddress: HumanAddr;
songwongtp marked this conversation as resolved.
Show resolved Hide resolved
scrollComponentId: string;
totalData: Option<number>;
refetchCount: () => void;
onViewMore?: () => void;
}

const CodesTableBody = observer(
({
walletAddress,
scrollComponentId,
totalData,
refetchCount,
onViewMore,
}: CodesTableProps) => {
const {
pagesQuantity,
currentPage,
setCurrentPage,
pageSize,
setPageSize,
offset,
} = usePaginator({
total: totalData,
initialState: {
pageSize: 10,
currentPage: 1,
isDisabled: false,
},
});
const { codes, isLoading } = useCodeStored(
walletAddress,
offset,
onViewMore ? 5 : pageSize
);

const onPageChange = (nextPage: number) => {
refetchCount();
setCurrentPage(nextPage);
};

const onPageSizeChange = (e: ChangeEvent<HTMLSelectElement>) => {
const size = Number(e.target.value);
refetchCount();
setPageSize(size);
setCurrentPage(1);
};

const templateColumns =
"max(80px) minmax(320px, 1fr) max(120px) max(160px) minmax(320px, 0.75fr)";

if (isLoading) return <Loading />;
if (!codes?.length)
return (
<Flex
py="64px"
direction="column"
borderY="1px solid"
borderColor="pebble.700"
>
<EmptyState message="This account did not stored any codes before." />
</Flex>
);
return (
<>
<TableContainer>
<CodesTableHeader templateColumns={templateColumns} />
{codes.map((code) => (
<CodesTableRow
key={code.id + code.uploader + code.name}
codeInfo={code}
templateColumns={templateColumns}
/>
))}
</TableContainer>
{totalData &&
(onViewMore
? totalData > 5 && <ViewMore onClick={onViewMore} />
: totalData > 10 && (
<Pagination
currentPage={currentPage}
pagesQuantity={pagesQuantity}
offset={offset}
totalData={totalData}
scrollComponentId={scrollComponentId}
pageSize={pageSize}
onPageChange={onPageChange}
onPageSizeChange={onPageSizeChange}
/>
))}
</>
);
}
);

export const CodesTable = ({
totalData,
...componentProps
}: CodesTableProps) => (
<Box mt={12} mb={4}>
<TableTitle
title="Stored Codes"
count={totalData ?? 0}
helperText="This account stored the following codes"
/>
<CodesTableBody totalData={totalData} {...componentProps} />
</Box>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./CodesTable";
Loading