From 6c0725f3b9de65f4b943e719b7823902014b4859 Mon Sep 17 00:00:00 2001 From: mufazalov Date: Tue, 21 Jan 2025 13:50:57 +0300 Subject: [PATCH] feat: pass database to whoami --- src/containers/App/Content.tsx | 4 +++- src/services/api/viewer.ts | 4 ++-- src/store/reducers/authentication/authentication.ts | 4 ++-- src/store/reducers/capabilities/hooks.ts | 10 +--------- src/utils/hooks/useDatabaseFromQuery.ts | 8 ++++++++ 5 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 src/utils/hooks/useDatabaseFromQuery.ts diff --git a/src/containers/App/Content.tsx b/src/containers/App/Content.tsx index eb39f719f2..e24803b458 100644 --- a/src/containers/App/Content.tsx +++ b/src/containers/App/Content.tsx @@ -15,6 +15,7 @@ import {authenticationApi} from '../../store/reducers/authentication/authenticat import {useCapabilitiesLoaded, useCapabilitiesQuery} from '../../store/reducers/capabilities/hooks'; import {nodesListApi} from '../../store/reducers/nodesList'; import {cn} from '../../utils/cn'; +import {useDatabaseFromQuery} from '../../utils/hooks/useDatabaseFromQuery'; import {lazyComponent} from '../../utils/lazyComponent'; import Authentication from '../Authentication/Authentication'; import {getClusterPath} from '../Cluster/utils'; @@ -184,7 +185,8 @@ function DataWrapper({children}: {children: React.ReactNode}) { } function GetUser({children}: {children: React.ReactNode}) { - const {isLoading, error} = authenticationApi.useWhoamiQuery(undefined); + const database = useDatabaseFromQuery(); + const {isLoading, error} = authenticationApi.useWhoamiQuery({database}); return ( diff --git a/src/services/api/viewer.ts b/src/services/api/viewer.ts index f073c226f8..6ae84e6815 100644 --- a/src/services/api/viewer.ts +++ b/src/services/api/viewer.ts @@ -434,8 +434,8 @@ export class ViewerAPI extends BaseYdbAPI { ); } - whoami() { - return this.get(this.getPath('/viewer/json/whoami'), {}); + whoami({database}: {database?: string}) { + return this.get(this.getPath('/viewer/json/whoami'), {database}); } autocomplete(params: {database: string; prefix?: string; limit?: number; table?: string[]}) { diff --git a/src/store/reducers/authentication/authentication.ts b/src/store/reducers/authentication/authentication.ts index 1e42abfd86..ef53095178 100644 --- a/src/store/reducers/authentication/authentication.ts +++ b/src/store/reducers/authentication/authentication.ts @@ -50,9 +50,9 @@ export const {selectIsUserAllowedToMakeChanges, selectUser} = slice.selectors; export const authenticationApi = api.injectEndpoints({ endpoints: (build) => ({ whoami: build.query({ - queryFn: async (_, {dispatch}) => { + queryFn: async ({database}: {database?: string}, {dispatch}) => { try { - const data = await window.api.viewer.whoami(); + const data = await window.api.viewer.whoami({database}); dispatch(setUser(data)); return {data}; } catch (error) { diff --git a/src/store/reducers/capabilities/hooks.ts b/src/store/reducers/capabilities/hooks.ts index cafdfdda7e..a0222071b0 100644 --- a/src/store/reducers/capabilities/hooks.ts +++ b/src/store/reducers/capabilities/hooks.ts @@ -1,17 +1,9 @@ -import {StringParam, useQueryParam} from 'use-query-params'; - import type {Capability} from '../../../types/api/capabilities'; import {useTypedSelector} from '../../../utils/hooks'; +import {useDatabaseFromQuery} from '../../../utils/hooks/useDatabaseFromQuery'; import {capabilitiesApi, selectCapabilityVersion, selectDatabaseCapabilities} from './capabilities'; -function useDatabaseFromQuery() { - const [database] = useQueryParam('database', StringParam); - - // Remove null from database type - return database ?? undefined; -} - export function useCapabilitiesQuery() { const database = useDatabaseFromQuery(); diff --git a/src/utils/hooks/useDatabaseFromQuery.ts b/src/utils/hooks/useDatabaseFromQuery.ts new file mode 100644 index 0000000000..2d3f52d3c1 --- /dev/null +++ b/src/utils/hooks/useDatabaseFromQuery.ts @@ -0,0 +1,8 @@ +import {StringParam, useQueryParam} from 'use-query-params'; + +export function useDatabaseFromQuery() { + const [database] = useQueryParam('database', StringParam); + + // Remove null from database type + return database ?? undefined; +}