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

Refactor/contract list #42

Merged
merged 8 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Improvements

### Bug fixes

- [#42](https://github.com/alleslabs/celatone-frontend/pull/42) Properly show CTAs on contract-list page and edit zero/disconnected state
50 changes: 26 additions & 24 deletions src/lib/components/modal/select-contract/AllContractLists.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, SimpleGrid } from "@chakra-ui/react";
import type { Dispatch, SetStateAction } from "react";
import { useMemo, useState } from "react";

import { TextInput } from "lib/components/forms";
import { EmptyState } from "lib/components/state/EmptyState";
Expand All @@ -8,50 +8,52 @@ import type { ContractListInfo } from "lib/stores/contract";
import { ListCard } from "./ListCard";

interface AllContractListsProps {
search: string;
setSearch: Dispatch<SetStateAction<string>>;
contractLists: ContractListInfo[];
handleListSelect: (value: string) => void;
isReadOnly: boolean;
isReadOnly?: boolean;
formLabelBgColor?: string;
}

export const AllContractLists = ({
search,
setSearch,
handleListSelect,
contractLists,
isReadOnly,
formLabelBgColor,
handleListSelect,
}: AllContractListsProps) => {
const filteredContractLists = contractLists.filter((item) =>
item.name.toLowerCase().includes(search.toLowerCase())
const [searchKeyword, setSearchKeyword] = useState("");

const filteredContractLists = useMemo(
() =>
contractLists.filter((item) =>
item.name.toLowerCase().includes(searchKeyword.toLowerCase())
poomthiti marked this conversation as resolved.
Show resolved Hide resolved
),
[contractLists, searchKeyword]
);

return (
<Box minH="xs" w="100%">
<TextInput
variant="floating"
value={search}
setInputState={setSearch}
value={searchKeyword}
setInputState={setSearchKeyword}
label="Search for your lists"
labelBgColor={formLabelBgColor}
size="md"
mb={isReadOnly ? 4 : 12}
/>
<Box my="18px" />
<SimpleGrid columns={{ sm: 1, md: 2, lg: 3 }} spacing={4} w="full">
{filteredContractLists.map((item) => (
<ListCard
key={item.slug}
item={item}
handleListSelect={handleListSelect}
isReadOnly={isReadOnly || !item.isInfoEditable}
showLastUpdated={item.isContractRemovable}
/>
))}
</SimpleGrid>
{filteredContractLists.length === 0 && (
{filteredContractLists.length === 0 ? (
<EmptyState message="None of your lists matches this search." />
) : (
<SimpleGrid columns={{ sm: 1, md: 2, lg: 3 }} spacing={4} w="full">
{filteredContractLists.map((item) => (
<ListCard
key={item.slug}
item={item}
handleListSelect={handleListSelect}
isReadOnly={isReadOnly || !item.isInfoEditable}
/>
))}
</SimpleGrid>
)}
</Box>
);
Expand Down
19 changes: 11 additions & 8 deletions src/lib/components/modal/select-contract/ListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ import dayjs from "dayjs";
import { MdMoreHoriz, MdMode, MdDelete } from "react-icons/md";

import { EditList, RemoveList } from "../list";
import { getListIcon } from "lib/data";
import { getListIcon, INSTANTIATED_LIST_NAME } from "lib/data";
import type { ContractListInfo } from "lib/stores/contract";
import { formatSlugName } from "lib/utils";

const iconProps = {
boxSize: "4",
display: "flex",
alignItems: "center",
};
poomthiti marked this conversation as resolved.
Show resolved Hide resolved

interface ListCardProps {
item: ContractListInfo;
handleListSelect: (value: string) => void;
isReadOnly: boolean;
showLastUpdated: boolean;
}

export const ListCard = ({
poomthiti marked this conversation as resolved.
Show resolved Hide resolved
item,
handleListSelect,
isReadOnly,
showLastUpdated,
}: ListCardProps) => {
const iconProps = {
boxSize: "4",
display: "flex",
alignItems: "center",
};
const showLastUpdated = item.slug !== formatSlugName(INSTANTIATED_LIST_NAME);

return (
<LinkBox cursor="pointer">
<Flex
Expand Down
89 changes: 44 additions & 45 deletions src/lib/components/modal/select-contract/ListDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Box, Flex } from "@chakra-ui/react";
import { matchSorter } from "match-sorter";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useMemo, useState } from "react";

import { TagSelection, TextInput } from "lib/components/forms";
import { EmptyState } from "lib/components/state/EmptyState";
Expand All @@ -13,82 +12,79 @@ import type { ContractInfo, ContractListInfo } from "lib/stores/contract";
import type { Option } from "lib/types";

interface FilteredListDetailProps {
search: string;
tagFilter: string[];
contracts: ContractInfo[];
isReadOnly: boolean;
isContractRemovable?: Option;
onContractSelect?: (addr: string) => void;
}

interface ListDetailProps {
search: string;
setSearch: Dispatch<SetStateAction<string>>;
contractListInfo: ContractListInfo;
isReadOnly: boolean;
isContractRemovable?: Option;
isReadOnly?: boolean;
contractRemovalInfo: Option | undefined;
poomthiti marked this conversation as resolved.
Show resolved Hide resolved
onContractSelect?: (addr: string) => void;
}

const FilteredListDetail = ({
search,
tagFilter,
contracts,
isReadOnly,
isContractRemovable,
contractRemovalInfo,
onContractSelect = () => {},
}: FilteredListDetailProps) => {
const filteredContracts = matchSorter(contracts, search, {
keys: ["name", "description", "label", "address"],
sorter: (sortedItem) => sortedItem,
}).filter((contract) =>
tagFilter.every((tag) => contract.tags?.includes(tag))
);
if (filteredContracts.length === 0)
if (contracts.length === 0)
return (
<EmptyState
message="No contracts match found.
Make sure you are searching with contract address, name, or description."
/>
);
if (!isReadOnly)
return (
<ContractListTable
contracts={filteredContracts}
isContractRemovable={isContractRemovable}
/>
);
return (

return isReadOnly ? (
<ContractListReadOnlyTable
contracts={filteredContracts}
contracts={contracts}
onContractSelect={onContractSelect}
/>
) : (
<ContractListTable
contracts={contracts}
contractRemovalInfo={contractRemovalInfo}
/>
);
};

interface ListDetailProps {
contractListInfo: ContractListInfo;
isReadOnly?: boolean;
isInstantiatedByMe?: boolean;
onContractSelect?: (addr: string) => void;
poomthiti marked this conversation as resolved.
Show resolved Hide resolved
}

export const ListDetail = ({
poomthiti marked this conversation as resolved.
Show resolved Hide resolved
search,
setSearch,
contractListInfo,
isReadOnly,
isContractRemovable,
isInstantiatedByMe = false,
onContractSelect,
}: ListDetailProps) => {
const userKey = useUserKey();
const { getAllTags } = useContractStore();

const [searchKeyword, setSearchKeyword] = useState("");
const [tagFilter, setTagFilter] = useState<string[]>([]);

const filteredContracts = useMemo(
() =>
matchSorter(contractListInfo.contracts, searchKeyword, {
keys: ["name", "description", "label", "address"],
sorter: (sortedItem) => sortedItem,
}).filter((contract) =>
tagFilter.every((tag) => contract.tags?.includes(tag))
),
[contractListInfo.contracts, searchKeyword, tagFilter]
);

return (
<Box minH="xs">
<Box minH="xs" pb="48px">
<Box px={isReadOnly ? "0px" : "48px"}>
<Flex gap={2} w="full" my={isReadOnly ? "24px" : "48px"}>
<TextInput
variant="floating"
value={search}
setInputState={setSearch}
value={searchKeyword}
setInputState={setSearchKeyword}
placeholder="Search with contract address, name, or description"
size="lg"
size={!isReadOnly ? "lg" : "md"}
/>
{!isReadOnly && (
<TagSelection
Expand All @@ -108,14 +104,17 @@ export const ListDetail = ({
<ZeroState
list={{ label: contractListInfo.name, value: contractListInfo.slug }}
isReadOnly={isReadOnly}
isInstantiatedByMe={isInstantiatedByMe}
/>
) : (
<FilteredListDetail
search={search}
tagFilter={tagFilter}
contracts={contractListInfo.contracts}
contracts={filteredContracts}
isReadOnly={isReadOnly}
isContractRemovable={isContractRemovable}
contractRemovalInfo={
contractListInfo.isContractRemovable
? { label: contractListInfo.name, value: contractListInfo.slug }
: undefined
}
onContractSelect={onContractSelect}
/>
)}
Expand Down
15 changes: 1 addition & 14 deletions src/lib/components/modal/select-contract/SelectContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export const SelectContract = ({
const [listSlug, setListSlug] = useState("");

const [searchManual, setSearchManual] = useState("");
const [searchList, setSearchList] = useState("");
const [searchInList, setSearchInList] = useState("");
const [invalid, setInvalid] = useState("");

const { getContractLists } = useContractStore();
Expand All @@ -57,8 +55,6 @@ export const SelectContract = ({
const resetOnClose = () => {
setListSlug("");
setSearchManual("");
setSearchList("");
setSearchInList("");
setInvalid("");
onClose();
};
Expand Down Expand Up @@ -147,12 +143,10 @@ export const SelectContract = ({
<Divider borderColor="gray.500" />
</Flex>

<Heading as="h6" variant="h6" mb="8px">
<Heading as="h6" variant="h6" mb={4}>
Select from your Contract List
</Heading>
<AllContractLists
search={searchList}
setSearch={setSearchList}
contractLists={contractLists}
handleListSelect={handleListSelect}
isReadOnly
Expand All @@ -177,15 +171,8 @@ export const SelectContract = ({
<ModalCloseButton />
<ModalBody>
<ListDetail
search={searchInList}
setSearch={setSearchInList}
contractListInfo={contractList}
isReadOnly
isContractRemovable={
contractList.isContractRemovable
? { label: contractList.name, value: contractList.slug }
: undefined
}
onContractSelect={onSelectThenClose}
/>
</ModalBody>
Expand Down
Loading