Skip to content

Commit 533e9fc

Browse files
authored
feat: add mainnet support to leaderboard with network toggle (#3052)
1 parent 5e77399 commit 533e9fc

File tree

9 files changed

+87
-70
lines changed

9 files changed

+87
-70
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export NX_ADD_PLUGINS=false
99

1010
export ESLINT_USE_FLAT_CONFIG=false
1111

12-
export VITE_GRAPHQL_ENDPOINT='https://testnet-gql.tangle.tools/graphql'
12+
export VITE_GRAPHQL_ENDPOINT='https://mainnet-gql.tangle.tools/graphql'

apps/leaderboard/src/features/indexingProgress/components/SyncProgressIndicator.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CrossCircledIcon } from '@radix-ui/react-icons';
22
import { StatusIndicator } from '@tangle-network/icons';
3+
import { NetworkType } from '@tangle-network/tangle-shared-ui/graphql/graphql';
34
import {
45
Chip,
56
EMPTY_VALUE_PLACEHOLDER,
@@ -8,8 +9,12 @@ import {
89
import { useMemo } from 'react';
910
import { useIndexingProgress } from '../queries';
1011

11-
export const SyncProgressIndicator = () => {
12-
const { data, error, isPending } = useIndexingProgress();
12+
export const SyncProgressIndicator = ({
13+
network,
14+
}: {
15+
network: NetworkType;
16+
}) => {
17+
const { data, error, isPending } = useIndexingProgress(network);
1318

1419
const progress = useMemo(() => {
1520
if (!data?.lastProcessedHeight || !data?.targetHeight) {

apps/leaderboard/src/features/indexingProgress/queries/indexingProgressQuery.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BLOCK_TIME_MS } from '@tangle-network/dapp-config/constants/tangle';
22
import { graphql } from '@tangle-network/tangle-shared-ui/graphql';
3+
import { NetworkType } from '@tangle-network/tangle-shared-ui/graphql/graphql';
34
import { executeGraphQL } from '@tangle-network/tangle-shared-ui/utils/executeGraphQL';
45
import { useQuery } from '@tanstack/react-query';
56
import { INDEXING_PROGRESS_QUERY_KEY } from '../../../constants/query';
@@ -13,15 +14,15 @@ const IndexingProgressQueryDocument = graphql(/* GraphQL */ `
1314
}
1415
`);
1516

16-
const fetcher = async () => {
17-
const result = await executeGraphQL(IndexingProgressQueryDocument);
17+
const fetcher = async (network: NetworkType) => {
18+
const result = await executeGraphQL(network, IndexingProgressQueryDocument);
1819
return result.data._metadata;
1920
};
2021

21-
export function useIndexingProgress() {
22+
export function useIndexingProgress(network: NetworkType) {
2223
return useQuery({
23-
queryKey: [INDEXING_PROGRESS_QUERY_KEY],
24-
queryFn: fetcher,
24+
queryKey: [INDEXING_PROGRESS_QUERY_KEY, network],
25+
queryFn: () => fetcher(network),
2526
refetchInterval: BLOCK_TIME_MS,
2627
placeholderData: (prev) => prev,
2728
});

apps/leaderboard/src/features/leaderboard/components/LeaderboardTable.tsx

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
isSubstrateAddress,
99
KeyValueWithButton,
1010
Table,
11+
TabsListWithAnimation,
12+
TabsRoot,
13+
TabsTriggerWithAnimation,
1114
Tooltip,
1215
TooltipBody,
1316
TooltipTrigger,
@@ -203,8 +206,7 @@ export const LeaderboardTable = () => {
203206
pageSize: 15,
204207
});
205208

206-
// TODO: Add network tabs when we support both mainnet and testnet
207-
const [networkTab] = useState<NetworkType>('TESTNET' as NetworkType);
209+
const [networkTab, setNetworkTab] = useState<NetworkType>('MAINNET');
208210

209211
const {
210212
data: latestBlock,
@@ -243,6 +245,7 @@ export const LeaderboardTable = () => {
243245
isPending,
244246
isFetching,
245247
} = useLeaderboard(
248+
networkTab,
246249
pagination.pageSize,
247250
pagination.pageIndex * pagination.pageSize,
248251
blockNumberSevenDaysAgo,
@@ -304,52 +307,36 @@ export const LeaderboardTable = () => {
304307
return (
305308
<Card className="space-y-6 !bg-transparent !border-transparent p-0">
306309
<div className="flex items-center justify-between flex-wrap sm:flex-nowrap gap-4">
307-
{/* <TabsRoot
308-
className="max-w-xs flex-auto"
309-
value={networkTab}
310-
onValueChange={(tab) =>
311-
setNetworkTab(tab as 'all' | 'mainnet' | 'testnet')
312-
}
310+
<TabsRoot
311+
className="max-w-xs flex-auto"
312+
value={networkTab}
313+
onValueChange={(tab) => setNetworkTab(tab as NetworkType)}
314+
>
315+
<TabsListWithAnimation>
316+
<TabsTriggerWithAnimation
317+
className="min-h-8"
318+
value="MAINNET"
319+
isActive={networkTab === 'MAINNET'}
320+
hideSeparator
321+
labelVariant="body2"
322+
labelClassName="py-1 px-0"
323+
>
324+
Mainnet
325+
</TabsTriggerWithAnimation>
326+
<TabsTriggerWithAnimation
327+
className="min-h-8"
328+
value="TESTNET"
329+
isActive={networkTab === 'TESTNET'}
330+
hideSeparator
331+
labelVariant="body2"
332+
labelClassName="py-1 px-0"
313333
>
314-
<TabsListWithAnimation>
315-
<TabsTriggerWithAnimation
316-
className="min-h-8"
317-
value="all"
318-
isActive={networkTab === 'all'}
319-
hideSeparator
320-
labelVariant="body2"
321-
labelClassName="py-1 px-0"
322-
>
323-
<span className="block xs:hidden !text-inherit">All</span>
324-
325-
<span className="hidden xs:block !text-inherit">
326-
All Networks
327-
</span>
328-
</TabsTriggerWithAnimation>
329-
<TabsTriggerWithAnimation
330-
className="min-h-8"
331-
value="mainnet"
332-
isActive={networkTab === 'mainnet'}
333-
hideSeparator
334-
labelVariant="body2"
335-
labelClassName="py-1 px-0"
336-
>
337-
Mainnet
338-
</TabsTriggerWithAnimation>
339-
<TabsTriggerWithAnimation
340-
className="min-h-8"
341-
value="testnet"
342-
isActive={networkTab === 'testnet'}
343-
hideSeparator
344-
labelVariant="body2"
345-
labelClassName="py-1 px-0"
346-
>
347-
Testnet
348-
</TabsTriggerWithAnimation>
349-
</TabsListWithAnimation>
350-
</TabsRoot> */}
351-
352-
<SyncProgressIndicator />
334+
Testnet
335+
</TabsTriggerWithAnimation>
336+
</TabsListWithAnimation>
337+
</TabsRoot>
338+
339+
<SyncProgressIndicator network={networkTab} />
353340

354341
<div className="flex items-center justify-end gap-2 grow">
355342
<Input

apps/leaderboard/src/features/leaderboard/queries/leaderboardQuery.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
SafeNestedType,
44
} from '@tangle-network/dapp-types/utils/types';
55
import { graphql } from '@tangle-network/tangle-shared-ui/graphql';
6-
import { LeaderboardTableDocumentQuery } from '@tangle-network/tangle-shared-ui/graphql/graphql';
6+
import {
7+
LeaderboardTableDocumentQuery,
8+
NetworkType,
9+
} from '@tangle-network/tangle-shared-ui/graphql/graphql';
710
import { executeGraphQL } from '@tangle-network/tangle-shared-ui/utils/executeGraphQL';
811
import { useQuery } from '@tanstack/react-query';
912
import { LEADERBOARD_QUERY_KEY } from '../../../constants/query';
@@ -104,12 +107,13 @@ const LeaderboardQueryDocument = graphql(/* GraphQL */ `
104107
`);
105108

106109
const fetcher = async (
110+
network: NetworkType,
107111
first: number,
108112
offset: number,
109113
blockNumberSevenDaysAgo: number,
110114
accountIdQuery?: string,
111115
) => {
112-
const result = await executeGraphQL(LeaderboardQueryDocument, {
116+
const result = await executeGraphQL(network, LeaderboardQueryDocument, {
113117
first,
114118
offset,
115119
blockNumberSevenDaysAgo,
@@ -120,6 +124,7 @@ const fetcher = async (
120124
};
121125

122126
export function useLeaderboard(
127+
network: NetworkType,
123128
first: number,
124129
offset: number,
125130
blockNumberSevenDaysAgo: number,
@@ -128,13 +133,14 @@ export function useLeaderboard(
128133
return useQuery({
129134
queryKey: [
130135
LEADERBOARD_QUERY_KEY,
136+
network,
131137
first,
132138
offset,
133139
blockNumberSevenDaysAgo,
134140
accountIdQuery,
135141
],
136142
queryFn: () =>
137-
fetcher(first, offset, blockNumberSevenDaysAgo, accountIdQuery),
143+
fetcher(network, first, offset, blockNumberSevenDaysAgo, accountIdQuery),
138144
enabled: first > 0 && offset >= 0 && blockNumberSevenDaysAgo > 0,
139145
placeholderData: (prev) => prev,
140146
});

apps/tangle-dapp/src/features/points/data/useActiveAccountPoints.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { executeGraphQL } from '@tangle-network/tangle-shared-ui/utils/executeGr
44
import { useQuery } from '@tanstack/react-query';
55
import { useMemo } from 'react';
66
import { ReactQueryKey } from '../../../constants/reactQuery';
7+
import useNetworkStore from '@tangle-network/tangle-shared-ui/context/useNetworkStore';
8+
import { NetworkId } from '@tangle-network/ui-components/constants/networks';
9+
import { NetworkType } from '@tangle-network/tangle-shared-ui/graphql/graphql';
710

811
const GetAccountPointsQueryDocument = graphql(/* GraphQL */ `
912
query GetAccountPoints($account: String!) {
@@ -14,24 +17,29 @@ const GetAccountPointsQueryDocument = graphql(/* GraphQL */ `
1417
}
1518
`);
1619

17-
const fetcher = async (activeAccount: string | null) => {
20+
const fetcher = async (network: NetworkType, activeAccount: string | null) => {
1821
if (activeAccount === null) {
1922
return null;
2023
}
2124

22-
const result = await executeGraphQL(GetAccountPointsQueryDocument, {
25+
const result = await executeGraphQL(network, GetAccountPointsQueryDocument, {
2326
account: activeAccount,
2427
});
2528

2629
return result.data;
2730
};
2831

2932
export default function useActiveAccountPoints() {
33+
const network = useNetworkStore((state) => state.network);
3034
const activeAccount = useSubstrateAddress(false);
3135

3236
const { data: accountPointsResponse, ...rest } = useQuery({
3337
queryKey: [ReactQueryKey.GetAccountPoints, activeAccount],
34-
queryFn: () => fetcher(activeAccount),
38+
queryFn: () =>
39+
fetcher(
40+
network.id === NetworkId.TANGLE_MAINNET ? 'MAINNET' : 'TESTNET',
41+
activeAccount,
42+
),
3543
retry: 10,
3644
refetchInterval: 6000,
3745
});

codegen.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { DEFAULT_GRAPHQL_ENDPOINT } from './libs/dapp-config/src/constants/graphql';
21
import type { CodegenConfig } from '@graphql-codegen/cli';
32
import 'dotenv/config';
43

4+
export const DEFAULT_GRAPHQL_ENDPOINT =
5+
'https://mainnet-gql.tangle.tools/graphql';
6+
57
const VITE_GRAPHQL_ENDPOINT =
68
process.env.VITE_GRAPHQL_ENDPOINT ?? DEFAULT_GRAPHQL_ENDPOINT;
79

libs/dapp-config/src/constants/graphql.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

libs/tangle-shared-ui/src/utils/executeGraphQL.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { DEFAULT_GRAPHQL_ENDPOINT } from '@tangle-network/dapp-config/constants/graphql';
2-
import type { TypedDocumentString } from '../graphql/graphql';
3-
4-
const ENDPOINT =
5-
import.meta.env.VITE_GRAPHQL_ENDPOINT ?? DEFAULT_GRAPHQL_ENDPOINT;
1+
import type { NetworkType, TypedDocumentString } from '../graphql/graphql';
62

73
export async function executeGraphQL<TResult, TVariables>(
4+
network: NetworkType,
85
query: TypedDocumentString<TResult, TVariables>,
96
...[variables]: TVariables extends Record<string, never> ? [] : [TVariables]
107
) {
11-
const response = await fetch(ENDPOINT, {
8+
const endpoint = getGraphQLEndpointFromNetwork(network);
9+
10+
const response = await fetch(endpoint, {
1211
method: 'POST',
1312
headers: {
1413
'Content-Type': 'application/json',
@@ -26,3 +25,14 @@ export async function executeGraphQL<TResult, TVariables>(
2625

2726
return response.json() as Promise<{ data: TResult }>;
2827
}
28+
29+
const getGraphQLEndpointFromNetwork = (network: NetworkType) => {
30+
switch (network) {
31+
case 'MAINNET':
32+
return 'https://mainnet-gql.tangle.tools/graphql';
33+
case 'TESTNET':
34+
return 'https://testnet-gql.tangle.tools/graphql';
35+
default:
36+
throw new Error(`Invalid network: ${network}`);
37+
}
38+
};

0 commit comments

Comments
 (0)