Skip to content

Commit 747b609

Browse files
committed
refactor: [GSW-2428] extract query options into constant objects
- Add CacheTime, StaleTime, and RefetchInterval constant objects
1 parent 6611066 commit 747b609

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

packages/web/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"i18next-http-backend": "^2.5.2",
4343
"i18next-localstorage-backend": "^4.2.0",
4444
"jotai": "2.2.1",
45+
"ms": "^2.1.3",
4546
"next": "15.5.2",
4647
"next-i18next": "^15.3.0",
4748
"qrcode.react": "^3.1.0",
@@ -79,6 +80,7 @@
7980
"@types/crypto-js": "^4.2.1",
8081
"@types/d3": "^7.4.0",
8182
"@types/jest": "29.4.0",
83+
"@types/ms": "^2.1.0",
8284
"@types/node": "^20.19.0",
8385
"@types/react": "18.2.5",
8486
"@types/react-dom": "18.2.5",

packages/web/src/common/values/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from "./style-constant";
55
export * from "./text-constant";
66
export * from "./launchpad-constant";
77
export * from "./breakpoint.constant";
8+
export * from "./query-constant";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ms from "ms";
2+
3+
export const CacheTime = {
4+
None: 0,
5+
Quick: ms("1m"),
6+
Short: ms("2m"),
7+
Medium: ms("5m"),
8+
Long: ms("10m"),
9+
Extended: ms("30m"),
10+
} as const;
11+
12+
export const StaleTime = {
13+
None: 0,
14+
Instant: ms("30s"),
15+
Fast: ms("1m"),
16+
Standard: ms("5m"),
17+
} as const;
18+
19+
export const RefetchInterval = {
20+
None: false,
21+
Frequent: ms("5s"),
22+
Regular: ms("10s"),
23+
Moderate: ms("30s"),
24+
Rare: ms("1m"),
25+
} as const;

packages/web/src/layouts/swap/components/swap-info-chart/swap-token-info/SwapTokenInfo.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22

33
import { LineGraphData } from "@components/common/line-graph/LineGraph";
44
import { isNativeTokenByType, TokenModel } from "@models/token/token-model";
5+
import { RefetchInterval } from "@common/values";
56

67
import useElementWidth from "@hooks/common/use-element-width";
78
import { useGetTokenPrices } from "@query/token";
@@ -47,7 +48,10 @@ const SwapTokenInfo = ({ token }: SwapTokenInfoProps) => {
4748
data: { priceGradeType, last7d = [] } = {},
4849
isLoading,
4950
isFetched,
50-
} = useGetTokenPrices(tokenData.path as string, { enabled: !!tokenData.path, refetchInterval: 5_000 });
51+
} = useGetTokenPrices(tokenData.path as string, {
52+
enabled: !!tokenData.path,
53+
refetchInterval: RefetchInterval.Frequent,
54+
});
5155

5256
const handleMouseMove = React.useCallback(
5357
(data?: LineGraphData) => {

packages/web/src/layouts/token-detail/containers/token-chart-container/TokenChartContainer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { DEVICE_TYPE } from "@styles/media";
1818
import { getLabelChartV2, getLocalizeTime, getNumberOfAxis } from "@utils/chart";
1919
import { checkPositivePrice, generateDateSequence } from "@utils/common";
2020
import { formatPrice } from "@utils/new-number-utils";
21+
import { RefetchInterval } from "@common/values";
2122

2223
import TokenChart, { ChartInfo, TokenInfo } from "../../components/token-chart/TokenChart";
2324

@@ -140,7 +141,7 @@ const TokenChartContainer: React.FC = () => {
140141
path === "gnot" ? wugnotPath : path,
141142
{
142143
enabled: !!path,
143-
refetchInterval: 5_000,
144+
refetchInterval: RefetchInterval.Frequent,
144145
},
145146
);
146147

packages/web/src/react-query/token/use-get-token-prices.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useGnoswapContext } from "@hooks/common/use-gnoswap-context";
55
import { QUERY_KEY } from "../query-keys";
66
import { TokenPriceModel } from "@models/token/token-price-model";
77
import { TokenError } from "@common/errors/token";
8+
import { CacheTime, StaleTime } from "@common/values";
89

910
export const useGetTokenPrices = (path: string | null, options?: UseQueryOptions<TokenPriceModel, Error>) => {
1011
const { tokenRepository } = useGnoswapContext();
@@ -19,8 +20,8 @@ export const useGetTokenPrices = (path: string | null, options?: UseQueryOptions
1920
},
2021
refetchOnMount: true,
2122
refetchOnReconnect: true,
22-
cacheTime: 0,
23-
staleTime: 0,
23+
cacheTime: CacheTime.None,
24+
staleTime: StaleTime.None,
2425
...options,
2526
});
2627
};

yarn.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,7 @@ __metadata:
25442544
"@types/crypto-js": "npm:^4.2.1"
25452545
"@types/d3": "npm:^7.4.0"
25462546
"@types/jest": "npm:29.4.0"
2547+
"@types/ms": "npm:^2.1.0"
25472548
"@types/node": "npm:^20.19.0"
25482549
"@types/react": "npm:18.2.5"
25492550
"@types/react-dom": "npm:18.2.5"
@@ -2571,6 +2572,7 @@ __metadata:
25712572
jest-environment-jsdom: "npm:29.5.0"
25722573
jotai: "npm:2.2.1"
25732574
lint-staged: "npm:13.1.2"
2575+
ms: "npm:^2.1.3"
25742576
next: "npm:15.5.2"
25752577
next-i18next: "npm:^15.3.0"
25762578
prettier: "npm:3.6.2"
@@ -5244,7 +5246,7 @@ __metadata:
52445246
languageName: node
52455247
linkType: hard
52465248

5247-
"@types/ms@npm:*":
5249+
"@types/ms@npm:*, @types/ms@npm:^2.1.0":
52485250
version: 2.1.0
52495251
resolution: "@types/ms@npm:2.1.0"
52505252
checksum: 10/532d2ebb91937ccc4a89389715e5b47d4c66e708d15942fe6cc25add6dc37b2be058230a327dd50f43f89b8b6d5d52b74685a9e8f70516edfc9bdd6be910eff4

0 commit comments

Comments
 (0)