Skip to content

feat: add @tanstack/react-query package, custom QueryClientProvider, useQueryClient hook #747

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 38 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "1.14.0-pre-0",
"version": "1.14.0-beta-1",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down Expand Up @@ -106,6 +106,7 @@
"@lezer/highlight": "1.2.1",
"@replit/codemirror-indentation-markers": "6.5.3",
"@replit/codemirror-vscode-keymap": "6.0.2",
"@tanstack/react-query": "<5",
"@types/react-dates": "^21.8.6",
"@uiw/codemirror-extensions-hyper-link": "4.23.10",
"@uiw/codemirror-theme-github": "4.23.7",
Expand Down
46 changes: 46 additions & 0 deletions src/Common/API/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PropsWithChildren } from 'react'
import {
MutationCache,
QueryCache,
QueryClient,
QueryClientProvider as RQQueryClientProvider,
} from '@tanstack/react-query'

import { showError } from '@Common/Helper'

const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 0,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: false,
meta: { showToastError: true },
},
mutations: {
cacheTime: 0,
retry: false,
meta: { showToastError: true },
},
},
queryCache: new QueryCache({
onError: (error, query) => {
if (query.meta.showToastError) {
showError(error)
}
},
}),
mutationCache: new MutationCache({
onError: (error, _variables, _context, mutation) => {
if (mutation.meta.showToastError) {
showError(error)
}
},
}),
})

export const QueryClientProvider = ({ children }: PropsWithChildren<{}>) => (
<RQQueryClientProvider client={queryClient} contextSharing>
{children}
</RQQueryClientProvider>
)
2 changes: 2 additions & 0 deletions src/Common/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ const dashboardAPI = new CoreAPI({

export const { post, put, patch, get, trash } = dashboardAPI
export { default as CoreAPI } from './CoreAPI'
export { QueryClientProvider } from './QueryClientProvider'
export * from './useQueryClient'
export { abortPreviousRequests, getIsRequestAborted, handleRedirectToLicenseActivation } from './utils'
23 changes: 23 additions & 0 deletions src/Common/API/useQueryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
QueryKey,
useMutation as rqUseMutation,
UseMutationOptions,
UseMutationResult,
useQuery as rqUseQuery,
useQueryClient,
UseQueryOptions,
UseQueryResult,
} from '@tanstack/react-query'

import { ServerErrors } from '@Common/ServerError'
import { ResponseType } from '@Common/Types'

export const useQuery = <TQueryFnData = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(
options: UseQueryOptions<ResponseType<TQueryFnData>, ServerErrors, TData, TQueryKey>,
): UseQueryResult<TData, ServerErrors> => rqUseQuery(options)

export const useMutation = <TData = unknown, TVariables = void, TContext = unknown>(
options: UseMutationOptions<ResponseType<TData>, ServerErrors, TVariables, TContext>,
): UseMutationResult<ResponseType<TData>, ServerErrors, TVariables, TContext> => rqUseMutation(options)

export { useQueryClient }
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import { OverrideMergeStrategyType } from '@Pages/Applications'

import '@tanstack/react-query'

export interface customEnv {
SENTRY_ENV?: string
SENTRY_ERROR_ENABLED?: boolean
Expand Down Expand Up @@ -168,6 +170,23 @@ declare global {
}
}

declare module '@tanstack/react-query' {
export interface QueryMeta {
/**
* Optional flag indicating whether to display a toast notification for errors.
* @default true
*/
showToastError?: boolean
}
export interface MutationMeta {
/**
* Optional flag indicating whether to display a toast notification for errors.
* @default true
*/
showToastError?: boolean
}
}

export * from './Common'
export * from './Pages'
export * from './Shared'