feat: refactor version api (#2534)
* feat: create useGetVersionQuery function * [autofix.ci] apply automated fixes * fix: extend useQuery type to accpet functions with no parameters * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: anovazzi1 <otavio2204@gmail.com>
This commit is contained in:
parent
f0725ce874
commit
911035cdd3
8 changed files with 92 additions and 53 deletions
|
|
@ -14,6 +14,7 @@ import {
|
||||||
} from "./constants/constants";
|
} from "./constants/constants";
|
||||||
import { AuthContext } from "./contexts/authContext";
|
import { AuthContext } from "./contexts/authContext";
|
||||||
import { autoLogin, getGlobalVariables, getHealth } from "./controllers/API";
|
import { autoLogin, getGlobalVariables, getHealth } from "./controllers/API";
|
||||||
|
import { useGetVersionQuery } from "./controllers/API/queries/version";
|
||||||
import { setupAxiosDefaults } from "./controllers/API/utils";
|
import { setupAxiosDefaults } from "./controllers/API/utils";
|
||||||
import useTrackLastVisitedPath from "./hooks/use-track-last-visited-path";
|
import useTrackLastVisitedPath from "./hooks/use-track-last-visited-path";
|
||||||
import Router from "./routes";
|
import Router from "./routes";
|
||||||
|
|
@ -27,22 +28,19 @@ export default function App() {
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
useTrackLastVisitedPath();
|
useTrackLastVisitedPath();
|
||||||
|
|
||||||
const [fetchError, setFetchError] = useState(false);
|
const [fetchError, setFetchError] = useState(false);
|
||||||
const isLoading = useFlowsManagerStore((state) => state.isLoading);
|
const isLoading = useFlowsManagerStore((state) => state.isLoading);
|
||||||
|
|
||||||
const { isAuthenticated, login, setUserData, setAutoLogin, getUser } =
|
const { isAuthenticated, login, setUserData, setAutoLogin, getUser } =
|
||||||
useContext(AuthContext);
|
useContext(AuthContext);
|
||||||
const setLoading = useAlertStore((state) => state.setLoading);
|
const setLoading = useAlertStore((state) => state.setLoading);
|
||||||
const refreshVersion = useDarkStore((state) => state.refreshVersion);
|
|
||||||
const refreshStars = useDarkStore((state) => state.refreshStars);
|
const refreshStars = useDarkStore((state) => state.refreshStars);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const dark = useDarkStore((state) => state.dark);
|
const dark = useDarkStore((state) => state.dark);
|
||||||
|
|
||||||
const isLoadingFolders = useFolderStore((state) => state.isLoadingFolders);
|
const isLoadingFolders = useFolderStore((state) => state.isLoadingFolders);
|
||||||
|
useGetVersionQuery(undefined, "updateState");
|
||||||
|
|
||||||
const [isLoadingHealth, setIsLoadingHealth] = useState(false);
|
const [isLoadingHealth, setIsLoadingHealth] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!dark) {
|
if (!dark) {
|
||||||
document.getElementById("body")!.classList.remove("dark");
|
document.getElementById("body")!.classList.remove("dark");
|
||||||
|
|
@ -85,10 +83,9 @@ export default function App() {
|
||||||
*/
|
*/
|
||||||
return () => abortController.abort();
|
return () => abortController.abort();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const fetchAllData = async () => {
|
const fetchAllData = async () => {
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
await Promise.all([refreshStars(), refreshVersion(), fetchData()]);
|
await Promise.all([refreshStars(), fetchData()]);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -154,38 +151,36 @@ export default function App() {
|
||||||
return (
|
return (
|
||||||
//need parent component with width and height
|
//need parent component with width and height
|
||||||
<div className="flex h-full flex-col">
|
<div className="flex h-full flex-col">
|
||||||
<QueryClientProvider client={queryClient}>
|
<ErrorBoundary
|
||||||
<ErrorBoundary
|
onReset={() => {
|
||||||
onReset={() => {
|
// any reset function
|
||||||
// any reset function
|
}}
|
||||||
}}
|
FallbackComponent={CrashErrorComponent}
|
||||||
FallbackComponent={CrashErrorComponent}
|
>
|
||||||
>
|
<>
|
||||||
<>
|
{
|
||||||
{
|
<FetchErrorComponent
|
||||||
<FetchErrorComponent
|
description={FETCH_ERROR_DESCRIPION}
|
||||||
description={FETCH_ERROR_DESCRIPION}
|
message={FETCH_ERROR_MESSAGE}
|
||||||
message={FETCH_ERROR_MESSAGE}
|
openModal={fetchError}
|
||||||
openModal={fetchError}
|
setRetry={() => {
|
||||||
setRetry={() => {
|
checkApplicationHealth();
|
||||||
checkApplicationHealth();
|
}}
|
||||||
}}
|
isLoadingHealth={isLoadingHealth}
|
||||||
isLoadingHealth={isLoadingHealth}
|
></FetchErrorComponent>
|
||||||
></FetchErrorComponent>
|
}
|
||||||
}
|
|
||||||
|
|
||||||
<Case condition={isLoadingApplication}>
|
<Case condition={isLoadingApplication}>
|
||||||
<div className="loading-page-panel">
|
<div className="loading-page-panel">
|
||||||
<LoadingComponent remSize={50} />
|
<LoadingComponent remSize={50} />
|
||||||
</div>
|
</div>
|
||||||
</Case>
|
</Case>
|
||||||
|
|
||||||
<Case condition={!isLoadingApplication}>
|
<Case condition={!isLoadingApplication}>
|
||||||
<Router />
|
<Router />
|
||||||
</Case>
|
</Case>
|
||||||
</>
|
</>
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
</QueryClientProvider>
|
|
||||||
<div></div>
|
<div></div>
|
||||||
<div className="app-div">
|
<div className="app-div">
|
||||||
<AlertDisplayArea />
|
<AlertDisplayArea />
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
import { ReactNode } from "react";
|
import { ReactNode } from "react";
|
||||||
import { BrowserRouter } from "react-router-dom";
|
import { BrowserRouter } from "react-router-dom";
|
||||||
import { ReactFlowProvider } from "reactflow";
|
import { ReactFlowProvider } from "reactflow";
|
||||||
|
|
@ -6,18 +7,21 @@ import { ApiInterceptor } from "../controllers/API/api";
|
||||||
import { AuthProvider } from "./authContext";
|
import { AuthProvider } from "./authContext";
|
||||||
|
|
||||||
export default function ContextWrapper({ children }: { children: ReactNode }) {
|
export default function ContextWrapper({ children }: { children: ReactNode }) {
|
||||||
|
const queryClient = new QueryClient();
|
||||||
//element to wrap all context
|
//element to wrap all context
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<AuthProvider>
|
<QueryClientProvider client={queryClient}>
|
||||||
<TooltipProvider>
|
<AuthProvider>
|
||||||
<ReactFlowProvider>
|
<TooltipProvider>
|
||||||
<ApiInterceptor />
|
<ReactFlowProvider>
|
||||||
{children}
|
<ApiInterceptor />
|
||||||
</ReactFlowProvider>
|
{children}
|
||||||
</TooltipProvider>
|
</ReactFlowProvider>
|
||||||
</AuthProvider>
|
</TooltipProvider>
|
||||||
|
</AuthProvider>
|
||||||
|
</QueryClientProvider>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import { BASE_URL_API } from "../../../constants/constants";
|
||||||
export const URLs = {
|
export const URLs = {
|
||||||
TRANSACTIONS: `monitor/transactions`,
|
TRANSACTIONS: `monitor/transactions`,
|
||||||
API_KEY: `api_key`,
|
API_KEY: `api_key`,
|
||||||
|
VERSION: `version`,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export function getURL(key: keyof typeof URLs, params: any = {}) {
|
export function getURL(key: keyof typeof URLs, params: any = {}) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export * from "./use-get-version";
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { useDarkStore } from "@/stores/darkStore";
|
||||||
|
import { useQueryFunctionType } from "@/types/api";
|
||||||
|
import { api } from "../../api";
|
||||||
|
import { getURL } from "../../helpers/constants";
|
||||||
|
import { UseRequestProcessor } from "../../services/request-processor";
|
||||||
|
|
||||||
|
interface versionQueryResponse {
|
||||||
|
version: string;
|
||||||
|
package: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useGetVersionQuery: useQueryFunctionType<
|
||||||
|
undefined,
|
||||||
|
versionQueryResponse
|
||||||
|
> = (_, onFetch) => {
|
||||||
|
const { query } = UseRequestProcessor();
|
||||||
|
|
||||||
|
const responseFn = (data: versionQueryResponse) => {
|
||||||
|
if (!onFetch) return data;
|
||||||
|
if (typeof onFetch === "function") return onFetch(data);
|
||||||
|
const refreshVersion = useDarkStore.getState().refreshVersion;
|
||||||
|
switch (onFetch) {
|
||||||
|
case "updateState": {
|
||||||
|
return refreshVersion(data.version);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getVersionFn = async () => {
|
||||||
|
return await api.get<versionQueryResponse>(`${getURL("VERSION")}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const queryResult = query(["useGetVersionQuery"], async () => {
|
||||||
|
const { data } = await getVersionFn();
|
||||||
|
return responseFn(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
return queryResult;
|
||||||
|
};
|
||||||
|
|
@ -12,10 +12,8 @@ export const useDarkStore = create<DarkStoreType>((set, get) => ({
|
||||||
set(() => ({ dark: dark }));
|
set(() => ({ dark: dark }));
|
||||||
window.localStorage.setItem("isDark", dark.toString());
|
window.localStorage.setItem("isDark", dark.toString());
|
||||||
},
|
},
|
||||||
refreshVersion: () => {
|
refreshVersion: (v) => {
|
||||||
getVersion().then((data) => {
|
set(() => ({ version: v }));
|
||||||
set(() => ({ version: data.version }));
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
refreshStars: () => {
|
refreshStars: () => {
|
||||||
if (import.meta.env.CI) {
|
if (import.meta.env.CI) {
|
||||||
|
|
|
||||||
|
|
@ -233,10 +233,9 @@ export type ResponseErrorDetailAPI = {
|
||||||
response: { data: { detail: string } };
|
response: { data: { detail: string } };
|
||||||
};
|
};
|
||||||
|
|
||||||
export type useQueryFunctionType<T, R> = (
|
export type useQueryFunctionType<T = undefined, R = any> = T extends undefined
|
||||||
props: T,
|
? (props?: T, onFetch?: ((data: R) => void) | string) => UseQueryResult<R>
|
||||||
onFetch?: ((data: R) => void) | string,
|
: (props: T, onFetch?: ((data: R) => void) | string) => UseQueryResult<R>;
|
||||||
) => UseQueryResult<R>;
|
|
||||||
|
|
||||||
export type QueryFunctionType = (
|
export type QueryFunctionType = (
|
||||||
queryKey: UndefinedInitialDataOptions["queryKey"],
|
queryKey: UndefinedInitialDataOptions["queryKey"],
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ export type DarkStoreType = {
|
||||||
stars: number;
|
stars: number;
|
||||||
version: string;
|
version: string;
|
||||||
setDark: (dark: boolean) => void;
|
setDark: (dark: boolean) => void;
|
||||||
refreshVersion: () => void;
|
refreshVersion: (v: string) => void;
|
||||||
refreshStars: () => void;
|
refreshStars: () => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue