mirror of https://github.com/grafana/grafana
Nav: Add items to saved (#89908)
* App events: Add info notification type * Revert state * Use info alert * Nav: Enable saving items * Use local state * Use RTK query * Revert go.work * Revert * User-specific queries * Add memo * Fix base URL * Switch to ids * Fix memo * Add codeowners * Generate API * Separate user prefs API * Remove tag * Update export * Use feature togglepull/90138/head^2
parent
7bf8375b02
commit
7111c52e4c
@ -0,0 +1,14 @@ |
||||
import { useMemo } from 'react'; |
||||
|
||||
import { config } from '@grafana/runtime'; |
||||
import { useGetUserPreferencesQuery } from 'app/features/preferences/api'; |
||||
|
||||
export const usePinnedItems = () => { |
||||
const preferences = useGetUserPreferencesQuery(); |
||||
const pinnedItems = useMemo(() => preferences.data?.navbar?.savedItemIds || [], [preferences]); |
||||
|
||||
if (config.featureToggles.pinNavItems) { |
||||
return pinnedItems; |
||||
} |
||||
return []; |
||||
}; |
||||
@ -0,0 +1,19 @@ |
||||
import { generatedAPI } from './user/endpoints.gen'; |
||||
|
||||
export const { useGetUserPreferencesQuery, usePatchUserPreferencesMutation, useUpdateUserPreferencesMutation } = |
||||
generatedAPI; |
||||
|
||||
export const userPreferencesAPI = generatedAPI.enhanceEndpoints({ |
||||
addTagTypes: ['UserPreferences'], |
||||
endpoints: { |
||||
getUserPreferences: { |
||||
providesTags: ['UserPreferences'], |
||||
}, |
||||
updateUserPreferences: { |
||||
invalidatesTags: ['UserPreferences'], |
||||
}, |
||||
patchUserPreferences: { |
||||
invalidatesTags: ['UserPreferences'], |
||||
}, |
||||
}, |
||||
}); |
||||
@ -0,0 +1,36 @@ |
||||
import { BaseQueryFn, createApi } from '@reduxjs/toolkit/query/react'; |
||||
import { lastValueFrom } from 'rxjs'; |
||||
|
||||
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime'; |
||||
|
||||
interface RequestOptions extends BackendSrvRequest { |
||||
manageError?: (err: unknown) => { error: unknown }; |
||||
showErrorAlert?: boolean; |
||||
body?: BackendSrvRequest['data']; |
||||
} |
||||
|
||||
function createBackendSrvBaseQuery({ baseURL }: { baseURL: string }): BaseQueryFn<RequestOptions> { |
||||
async function backendSrvBaseQuery(requestOptions: RequestOptions) { |
||||
try { |
||||
const { data: responseData, ...meta } = await lastValueFrom( |
||||
getBackendSrv().fetch({ |
||||
...requestOptions, |
||||
url: baseURL + requestOptions.url, |
||||
showErrorAlert: requestOptions.showErrorAlert, |
||||
data: requestOptions.body, |
||||
}) |
||||
); |
||||
return { data: responseData, meta }; |
||||
} catch (error) { |
||||
return requestOptions.manageError ? requestOptions.manageError(error) : { error }; |
||||
} |
||||
} |
||||
|
||||
return backendSrvBaseQuery; |
||||
} |
||||
|
||||
export const baseAPI = createApi({ |
||||
reducerPath: 'userPreferencesAPI', |
||||
baseQuery: createBackendSrvBaseQuery({ baseURL: '/api' }), |
||||
endpoints: () => ({}), |
||||
}); |
||||
@ -0,0 +1,102 @@ |
||||
import { baseAPI as api } from './baseAPI'; |
||||
const injectedRtkApi = api.injectEndpoints({ |
||||
endpoints: (build) => ({ |
||||
getUserPreferences: build.query<GetUserPreferencesApiResponse, GetUserPreferencesApiArg>({ |
||||
query: () => ({ url: `/user/preferences` }), |
||||
}), |
||||
patchUserPreferences: build.mutation<PatchUserPreferencesApiResponse, PatchUserPreferencesApiArg>({ |
||||
query: (queryArg) => ({ url: `/user/preferences`, method: 'PATCH', body: queryArg.patchPrefsCmd }), |
||||
}), |
||||
updateUserPreferences: build.mutation<UpdateUserPreferencesApiResponse, UpdateUserPreferencesApiArg>({ |
||||
query: (queryArg) => ({ url: `/user/preferences`, method: 'PUT', body: queryArg.updatePrefsCmd }), |
||||
}), |
||||
}), |
||||
overrideExisting: false, |
||||
}); |
||||
export { injectedRtkApi as generatedAPI }; |
||||
export type GetUserPreferencesApiResponse = /** status 200 (empty) */ Preferences; |
||||
export type GetUserPreferencesApiArg = void; |
||||
export type PatchUserPreferencesApiResponse = |
||||
/** status 200 An OKResponse is returned if the request was successful. */ SuccessResponseBody; |
||||
export type PatchUserPreferencesApiArg = { |
||||
patchPrefsCmd: PatchPrefsCmd; |
||||
}; |
||||
export type UpdateUserPreferencesApiResponse = |
||||
/** status 200 An OKResponse is returned if the request was successful. */ SuccessResponseBody; |
||||
export type UpdateUserPreferencesApiArg = { |
||||
updatePrefsCmd: UpdatePrefsCmd; |
||||
}; |
||||
export type CookiePreferencesDefinesModelForCookiePreferences = { |
||||
analytics?: { |
||||
[key: string]: any; |
||||
}; |
||||
functional?: { |
||||
[key: string]: any; |
||||
}; |
||||
performance?: { |
||||
[key: string]: any; |
||||
}; |
||||
}; |
||||
export type NavbarPreferenceDefinesModelForNavbarPreference = { |
||||
savedItemIds?: string[]; |
||||
}; |
||||
export type QueryHistoryPreferenceDefinesModelForQueryHistoryPreference = { |
||||
/** HomeTab one of: '' | 'query' | 'starred'; */ |
||||
homeTab?: string; |
||||
}; |
||||
export type Preferences = { |
||||
cookiePreferences?: CookiePreferencesDefinesModelForCookiePreferences; |
||||
/** UID for the home dashboard */ |
||||
homeDashboardUID?: string; |
||||
/** Selected language (beta) */ |
||||
language?: string; |
||||
navbar?: NavbarPreferenceDefinesModelForNavbarPreference; |
||||
queryHistory?: QueryHistoryPreferenceDefinesModelForQueryHistoryPreference; |
||||
/** Theme light, dark, empty is default */ |
||||
theme?: string; |
||||
/** The timezone selection |
||||
TODO: this should use the timezone defined in common */ |
||||
timezone?: string; |
||||
/** WeekStart day of the week (sunday, monday, etc) */ |
||||
weekStart?: string; |
||||
}; |
||||
export type ErrorResponseBody = { |
||||
/** Error An optional detailed description of the actual error. Only included if running in developer mode. */ |
||||
error?: string; |
||||
/** a human readable version of the error */ |
||||
message: string; |
||||
/** Status An optional status to denote the cause of the error. |
||||
|
||||
For example, a 412 Precondition Failed error may include additional information of why that error happened. */ |
||||
status?: string; |
||||
}; |
||||
export type SuccessResponseBody = { |
||||
message?: string; |
||||
}; |
||||
export type CookieType = string; |
||||
export type PatchPrefsCmd = { |
||||
cookies?: CookieType[]; |
||||
/** The numerical :id of a favorited dashboard */ |
||||
homeDashboardId?: number; |
||||
homeDashboardUID?: string; |
||||
language?: string; |
||||
navbar?: NavbarPreferenceDefinesModelForNavbarPreference; |
||||
queryHistory?: QueryHistoryPreferenceDefinesModelForQueryHistoryPreference; |
||||
theme?: 'light' | 'dark'; |
||||
timezone?: 'utc' | 'browser'; |
||||
weekStart?: string; |
||||
}; |
||||
export type UpdatePrefsCmd = { |
||||
cookies?: CookieType[]; |
||||
/** The numerical :id of a favorited dashboard */ |
||||
homeDashboardId?: number; |
||||
homeDashboardUID?: string; |
||||
language?: string; |
||||
navbar?: NavbarPreferenceDefinesModelForNavbarPreference; |
||||
queryHistory?: QueryHistoryPreferenceDefinesModelForQueryHistoryPreference; |
||||
theme?: 'light' | 'dark' | 'system'; |
||||
timezone?: 'utc' | 'browser'; |
||||
weekStart?: string; |
||||
}; |
||||
export const { useGetUserPreferencesQuery, usePatchUserPreferencesMutation, useUpdateUserPreferencesMutation } = |
||||
injectedRtkApi; |
||||
Loading…
Reference in new issue