From 4565569bce7a59b77db03d635eee366ac95f364f Mon Sep 17 00:00:00 2001 From: John Ballesteros <89079900+johnshift@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:25:20 +0800 Subject: [PATCH] refactor: add query staletime default (#124) --- src/filters/hooks/use-filter-config.ts | 3 ++- src/jobs/hooks/use-job-details.ts | 3 +++ src/jobs/hooks/use-job-list-query.ts | 3 +++ src/orgs/hooks/use-org-details.ts | 3 +++ src/orgs/hooks/use-org-list-query.ts | 3 +++ src/shared/core/constants.ts | 4 ++++ 6 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/filters/hooks/use-filter-config.ts b/src/filters/hooks/use-filter-config.ts index e43ca6fb7..7879d304e 100644 --- a/src/filters/hooks/use-filter-config.ts +++ b/src/filters/hooks/use-filter-config.ts @@ -1,6 +1,6 @@ import { useQuery } from '@tanstack/react-query'; -import { RouteSection } from '~/shared/core/constants'; +import { QUERY_STALETIME, RouteSection } from '~/shared/core/constants'; import { filterQueryKeys } from '~/filters/core/query-keys'; import { sanitizeFilterParams } from '~/filters/utils/sanitizeFilterParams'; @@ -13,6 +13,7 @@ export const useFilterConfig = ( const query = useQuery({ queryKey: filterQueryKeys.list(searchParams, routeSection), queryFn: () => getFilterConfig(`/${routeSection}`), + staleTime: QUERY_STALETIME.DEFAULT, // Filter show-flag and then sort by position select: (data) => Object.values(data) diff --git a/src/jobs/hooks/use-job-details.ts b/src/jobs/hooks/use-job-details.ts index cd70e95b4..542ae0cd8 100644 --- a/src/jobs/hooks/use-job-details.ts +++ b/src/jobs/hooks/use-job-details.ts @@ -1,5 +1,7 @@ import { useQuery } from '@tanstack/react-query'; +import { QUERY_STALETIME } from '~/shared/core/constants'; + import { jobQueryKeys } from '~/jobs/core/query-keys'; import { getJobDetails } from '~/jobs/api/get-job-details'; @@ -7,5 +9,6 @@ export const useJobDetails = (id: string) => { return useQuery({ queryKey: jobQueryKeys.details(id), queryFn: () => getJobDetails(id), + staleTime: QUERY_STALETIME.DEFAULT, }); }; diff --git a/src/jobs/hooks/use-job-list-query.ts b/src/jobs/hooks/use-job-list-query.ts index 31ff58c3d..b2960968d 100644 --- a/src/jobs/hooks/use-job-list-query.ts +++ b/src/jobs/hooks/use-job-list-query.ts @@ -1,5 +1,7 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'; +import { QUERY_STALETIME } from '~/shared/core/constants'; + import { JobQueryKeys, jobQueryKeys } from '~/jobs/core/query-keys'; import { JobListQueryPage } from '~/jobs/core/schemas'; import { getJobList } from '~/jobs/api/get-job-list'; @@ -21,5 +23,6 @@ export const useJobListQuery = () => { initialPageParam: 1, getNextPageParam: ({ page, data }) => page > 0 && data.length > 0 ? page + 1 : undefined, + staleTime: QUERY_STALETIME.DEFAULT, }); }; diff --git a/src/orgs/hooks/use-org-details.ts b/src/orgs/hooks/use-org-details.ts index c2080ea25..e2205c4a9 100644 --- a/src/orgs/hooks/use-org-details.ts +++ b/src/orgs/hooks/use-org-details.ts @@ -1,5 +1,7 @@ import { useQuery } from '@tanstack/react-query'; +import { QUERY_STALETIME } from '~/shared/core/constants'; + import { orgQueryKeys } from '~/orgs/core/query-keys'; import { getOrgDetails } from '~/orgs/api/get-org-details'; @@ -7,5 +9,6 @@ export const useOrgDetails = (orgId: string) => { return useQuery({ queryKey: orgQueryKeys.details(orgId), queryFn: () => getOrgDetails(orgId), + staleTime: QUERY_STALETIME.DEFAULT, }); }; diff --git a/src/orgs/hooks/use-org-list-query.ts b/src/orgs/hooks/use-org-list-query.ts index dc920e6f2..fffef77c0 100644 --- a/src/orgs/hooks/use-org-list-query.ts +++ b/src/orgs/hooks/use-org-list-query.ts @@ -1,5 +1,7 @@ import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'; +import { QUERY_STALETIME } from '~/shared/core/constants'; + import { OrgQueryKeys, orgQueryKeys } from '~/orgs/core/query-keys'; import { OrgListQueryPage } from '~/orgs/core/schemas'; import { getOrgList } from '~/orgs/api/get-org-list'; @@ -21,5 +23,6 @@ export const useOrgListQuery = () => { initialPageParam: 1, getNextPageParam: ({ page, data }) => page > 0 && data.length > 0 ? page + 1 : undefined, + staleTime: QUERY_STALETIME.DEFAULT, }); }; diff --git a/src/shared/core/constants.ts b/src/shared/core/constants.ts index c21ff7248..6238ae94b 100644 --- a/src/shared/core/constants.ts +++ b/src/shared/core/constants.ts @@ -50,3 +50,7 @@ export const METADATA = { TWITTER_CREATOR: '@jobstash_xyz', IMAGE_DIMENSION: { width: 1100, height: 576 } as const, } as const; + +export const QUERY_STALETIME = { + DEFAULT: 1000 * 60 * 60, // 1 hr +} as const;