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 search for token balances #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
48 changes: 43 additions & 5 deletions pages/balances.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
{{ balanceError.message }}
</CommonErrorBlock>
</CommonCardWithLineButtons>
<div v-else>
<div v-for="(group, index) in balanceGroups" :key="index">
<TypographyCategoryLabel v-if="group.title">
{{ group.title }}
</TypographyCategoryLabel>
<div v-else class="space-y-4">
<CommonInputSearch
v-model.trim="search"
class="mb-block-padding-1/4"
placeholder="Search by symbol or address"
autofocus="desktop"
>
<template #icon>
<MagnifyingGlassIcon aria-hidden="true" />
</template>
</CommonInputSearch>

<div v-for="(group, index) in filteredBalances" :key="index">
<TypographyCategoryLabel v-if="group.title"> {{ group.title }} </TypographyCategoryLabel>
<CommonCardWithLineButtons>
<TokenBalance
v-for="item in group.balances"
Expand All @@ -37,6 +46,8 @@
</template>

<script lang="ts" setup>
import type { TokenAmount } from "@/types";

const onboardStore = useOnboardStore();
const walletEraStore = useZkSyncWalletStore();
const { isConnected } = storeToRefs(onboardStore);
Expand All @@ -45,8 +56,35 @@ const { eraNetwork } = storeToRefs(useZkSyncProviderStore());

const { loading, reset: resetSingleLoading } = useSingleLoading(computed(() => balanceInProgress.value));

const search = ref("");
const balanceGroups = groupBalancesByAmount(balance);

const filterBalanceGroups = (
balancesGroups: globalThis.ComputedRef<
{
title: string | null;
balances: TokenAmount[];
}[]
>
) => {
const lowercaseSearch = search.value.toLowerCase();

return balancesGroups.value.map((balanceGroup) => {
const filteredGroupBalances = balanceGroup.balances.filter(({ symbol, name, l1Address, address }) => {
return Object.values({ address, name, symbol, l1Address })
.filter((e) => typeof e === "string")
.some((value) => value!.toLowerCase().includes(lowercaseSearch));
});

return {
title: balanceGroup.title,
balances: filteredGroupBalances,
};
});
};

const filteredBalances = computed(() => filterBalanceGroups(balanceGroups));

const fetch = () => {
walletEraStore.requestBalance();
};
Expand Down
Loading