Skip to content

Commit

Permalink
chore(pol): removes loading from vaults homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
bearpong committed Jan 9, 2025
1 parent 9686ca9 commit c7096ff
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 121 deletions.
17 changes: 3 additions & 14 deletions apps/hub/src/app/vaults/[address]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import React from "react";
import { type Metadata } from "next";
import { notFound } from "next/navigation";
import { isIPFS } from "@bera/config";
import {
Address,
PublicClient,
createPublicClient,
http,
isAddress,
} from "viem";
import { Address, PublicClient, isAddress } from "viem";
import { VaultDetails } from "./components/VaultDetails";
import { defaultBeraNetworkConfig } from "@bera/wagmi/config";

import {
getRewardVault,
getRewardVaultStakingToken,
} from "@bera/berajs/actions";
import { ApiVaultFragment } from "@bera/graphql/pol/api";
import { getServerSidePublicClient } from "~/utils/serverSidePublicClient";

export function generateMetadata(): Metadata {
return {
Expand All @@ -42,12 +36,7 @@ export default async function PoolPage({
notFound();
}

const publicClient = createPublicClient({
// @ts-ignore viem types
chain: defaultBeraNetworkConfig.chain,
transport: http(),
});

const publicClient = await getServerSidePublicClient();
const vaultPromise = getRewardVault(params.address);

try {
Expand Down
52 changes: 52 additions & 0 deletions apps/hub/src/app/vaults/components/RewardVaults.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import React from "react";
import { usePollGlobalData, usePollGlobalDataQueryKey } from "@bera/berajs";

import GlobalGaugeWeightChart from "~/components/global-gauge-weight-chart";
import GaugeTables from "./gauge-tables";
import GaugeInfoCard from "./gauge-info-card";
import { GaugeCreation } from "./gauge-creation";
import { SWRFallback } from "@bera/berajs";
import { unstable_serialize } from "swr";
import { useRewardVaultsQueryKey } from "@bera/berajs";
import { getRewardVaultsFilter } from "~/components/getRewardVaultsFilter";
import { GetGaugeData, GlobalData } from "@bera/berajs/actions";

export default function RewardVaults({
vaults,
globalInfo,
}: {
vaults: GetGaugeData | undefined;
globalInfo: GlobalData | undefined;
}) {
return (
<SWRFallback
fallback={{
[unstable_serialize(useRewardVaultsQueryKey(getRewardVaultsFilter()))]:
vaults,
[unstable_serialize(usePollGlobalDataQueryKey())]: globalInfo,
}}
>
<Gauge />
</SWRFallback>
);
}

export function Gauge() {
const { data, isLoading: isGlobalDataLoading } = usePollGlobalData();
return (
<div className="grid grid-cols-1 gap-6">
<div className="grid grid-cols-1 lg:grid-cols-[3fr_1fr] gap-6">
<GaugeInfoCard />
<GlobalGaugeWeightChart
gaugeWeights={data?.globalCuttingBoard ?? []}
isLoading={isGlobalDataLoading}
totalBgtDistributed={data?.totalDistributedBGTAmount ?? "0"}
/>
</div>
<GaugeTables />
<GaugeCreation />
</div>
);
}
17 changes: 8 additions & 9 deletions apps/hub/src/app/vaults/components/gauge-info-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { Badge } from "@bera/ui/badge";
export default function GaugeInfoCard() {
const { data: globalData, isLoading } = usePollGlobalData();

const { data: bgtInflation, isLoading: isBgtInflationLoading } =
useBgtInflation();
const { data: bgtInflation } = useBgtInflation();

const blockTime = useBlockTime();

Expand All @@ -30,7 +29,7 @@ export default function GaugeInfoCard() {
<div className="text-sm font-medium leading-5 text-muted-foreground">
Active Reward Vaults
</div>
{!isLoading ? (
{globalData ? (
<span className="text-2xl font-semibold leading-8">
{globalData?.activeRewardVaultCount}
</span>
Expand All @@ -42,7 +41,7 @@ export default function GaugeInfoCard() {
<div className="text-sm font-medium leading-5 text-muted-foreground">
Active Incentives
</div>
{!isLoading && globalData ? (
{globalData ? (
<FormattedNumber
value={globalData.totalActiveIncentivesValueUSD}
symbol="USD"
Expand All @@ -56,7 +55,7 @@ export default function GaugeInfoCard() {
<div className="text-sm font-medium leading-5 text-muted-foreground">
Total Circulating BGT
</div>
{isLoading ? (
{!globalData ? (
<Skeleton className="h-8 w-full" />
) : (
<div className="flex items-center gap-1">
Expand All @@ -72,7 +71,7 @@ export default function GaugeInfoCard() {
<div className="text-sm font-medium leading-5 text-muted-foreground">
BGT Distribution (Yearly)
</div>
{isBgtInflationLoading ? (
{!bgtInflation ? (
<Skeleton className="h-8 w-12" />
) : (
<div className="sm:flex gap-2">
Expand All @@ -84,7 +83,7 @@ export default function GaugeInfoCard() {
/>
<Badge variant="success">
<FormattedNumber
value={(bgtInflation?.annualizedBGTInflation ?? 0) / 100}
value={bgtInflation?.annualizedBGTInflation ?? 0}
percent
/>
</Badge>
Expand All @@ -98,7 +97,7 @@ export default function GaugeInfoCard() {
<div className="text-sm font-medium leading-5 text-muted-foreground">
# Of Active Validators
</div>
{isLoading || !globalData ? (
{!globalData ? (
<Skeleton className="h-8 w-16" />
) : (
<div className="text-2xl font-semibold">
Expand All @@ -111,7 +110,7 @@ export default function GaugeInfoCard() {
<div className="text-xs font-medium uppercase leading-5 tracking-wider text-muted-foreground ">
Top 3 Validators
</div>
{!isLoading && globalData ? (
{globalData ? (
globalData.top3EmittingValidators?.map((validator, index) => {
const estimatedBgtPerYear = getValidatorEstimatedBgtPerYear(
validator,
Expand Down
27 changes: 0 additions & 27 deletions apps/hub/src/app/vaults/components/gauge.tsx

This file was deleted.

28 changes: 25 additions & 3 deletions apps/hub/src/app/vaults/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import React from "react";

import Gauge from "./components/gauge";
import RewardVaultsPage from "./components/RewardVaults";
import { getGlobalData, getRewardVaults } from "@bera/berajs/actions";
import { getRewardVaultsFilter } from "~/components/getRewardVaultsFilter";
import { getServerSidePublicClient } from "~/utils/serverSidePublicClient";
import { defaultBeraConfig } from "@bera/berajs/config";

export default function Page() {
return <Gauge />;
export default async function Page() {
const publicClient = await getServerSidePublicClient();

const [vaults, globalData] = await Promise.allSettled([
getRewardVaults({ filter: getRewardVaultsFilter() }),
getGlobalData(
// @ts-ignore viem types
publicClient,
defaultBeraConfig,
),
]);

return (
<RewardVaultsPage
vaults={vaults.status === "fulfilled" ? vaults.value : undefined}
globalInfo={
globalData.status === "fulfilled" ? globalData.value : undefined
}
/>
);
}
32 changes: 32 additions & 0 deletions apps/hub/src/components/getRewardVaultsFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
GqlRewardVaultOrderBy,
GqlRewardVaultOrderDirection,
} from "@bera/graphql/dex/api";
import type { SortingState } from "@tanstack/react-table";

const map: Record<string, GqlRewardVaultOrderBy> = {
allTimeReceivedBGTAmount: GqlRewardVaultOrderBy.AllTimeBgtReceived,
dynamicData_bgtCapturePercentage: GqlRewardVaultOrderBy.BgtCapturePercentage,
};
export const REWARD_VAULTS_PAGE_SIZE = 10;

export const getRewardVaultsFilter = (
sorting: SortingState = [],
page = 0,
pageSize = REWARD_VAULTS_PAGE_SIZE,
keywords = "",
) => {
return {
orderBy: map[sorting[0]?.id],
orderDirection:
sorting[0] !== undefined
? sorting[0]?.desc
? GqlRewardVaultOrderDirection.Desc
: GqlRewardVaultOrderDirection.Asc
: undefined,
skip: pageSize * page,
// filterByProduct: markets,
pageSize: pageSize,
search: keywords === "" || !keywords ? undefined : keywords,
};
};
4 changes: 2 additions & 2 deletions apps/hub/src/components/global-gauge-weight-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ export default function GlobalGaugeWeightChart({
</div>

{isLoading ? (
<Skeleton className="relative mx-auto rounded-full" />
<Skeleton className="relative mx-auto rounded-full aspect-square" />
) : (
<div className="relative mx-auto">
<div className="relative mx-auto aspect-square">
<BeraChart
data={dataP}
className="w-full"
Expand Down
38 changes: 13 additions & 25 deletions apps/hub/src/components/global-gauge-weight-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ import type {
} from "@tanstack/react-table";

import { AllRewardVaultColumns } from "~/columns/global-gauge-weight-columns";
import {
GqlRewardVaultOrderBy,
GqlRewardVaultOrderDirection,
} from "@bera/graphql/pol/api";

const GAUGE_PAGE_SIZE = 10;
import {
REWARD_VAULTS_PAGE_SIZE,
getRewardVaultsFilter,
} from "./getRewardVaultsFilter";

const map: Record<string, GqlRewardVaultOrderBy> = {
allTimeReceivedBGTAmount: GqlRewardVaultOrderBy.AllTimeBgtReceived,
dynamicData_bgtCapturePercentage: GqlRewardVaultOrderBy.BgtCapturePercentage,
};
export default function GlobalGaugeWeightTable({
myGauge = false,
keywords = "",
Expand All @@ -48,19 +43,12 @@ export default function GlobalGaugeWeightTable({
}, [markets]);

const { data, isLoading, isValidating } = useRewardVaults(
{
orderBy: map[sorting[0]?.id],
orderDirection:
sorting[0] !== undefined
? sorting[0]?.desc
? GqlRewardVaultOrderDirection.Desc
: GqlRewardVaultOrderDirection.Asc
: undefined,
skip: GAUGE_PAGE_SIZE * page,
// filterByProduct: markets,
pageSize: GAUGE_PAGE_SIZE,
search: isTyping ? "" : keywords,
},
getRewardVaultsFilter(
sorting,
page,
REWARD_VAULTS_PAGE_SIZE,
isTyping ? "" : keywords,
),
{ opts: { keepPreviousData: true } },
);

Expand Down Expand Up @@ -97,7 +85,7 @@ export default function GlobalGaugeWeightTable({
typeof updater === "function"
? updater({
pageIndex: prev ?? 0,
pageSize: GAUGE_PAGE_SIZE,
pageSize: REWARD_VAULTS_PAGE_SIZE,
})
: updater;
return newPaginationState.pageIndex ?? 0;
Expand All @@ -120,14 +108,14 @@ export default function GlobalGaugeWeightTable({
state: {
pagination: {
pageIndex: page,
pageSize: GAUGE_PAGE_SIZE,
pageSize: REWARD_VAULTS_PAGE_SIZE,
},
sorting,
},
manualSorting: true,
manualPagination: true,
autoResetPageIndex: false,
pageCount: Math.ceil(gaugeCounts / GAUGE_PAGE_SIZE),
pageCount: Math.ceil(gaugeCounts / REWARD_VAULTS_PAGE_SIZE),
onPaginationChange: handlePaginationChange,
onSortingChange: handleSortingChange,
},
Expand Down
11 changes: 11 additions & 0 deletions apps/hub/src/utils/serverSidePublicClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defaultBeraNetworkConfig } from "@bera/wagmi/config";
import "server-only";
import { createPublicClient, http } from "viem";

export const getServerSidePublicClient = async () => {
return createPublicClient({
// @ts-ignore viem types
chain: defaultBeraNetworkConfig.chain,
transport: http(),
});
};
4 changes: 1 addition & 3 deletions packages/berajs/src/actions/bgt/getBGTGlobalInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export interface GlobalInfo {
annualizedBGTInflation: string;
}

export const getBGTGlobalInfo = async (
config: BeraConfig,
): Promise<GlobalInfo | undefined> => {
export const getBGTGlobalInfo = async (): Promise<GlobalInfo> => {
const apiRes = await bexApiGraphqlClient.query<
GlobalDataQuery,
GlobalDataQueryVariables
Expand Down
Loading

0 comments on commit c7096ff

Please sign in to comment.