Navigation: Backend to save navigation customization into preferences (#89783)

This commit is contained in:
Joao Silva 2024-07-03 10:40:51 +01:00 committed by GitHub
parent 68e30e2b4b
commit 9004b58851
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 106 additions and 3 deletions

View File

@ -53,7 +53,7 @@ Content-Type: application/json
"timezone": "utc",
"weekStart": "",
"navbar": {
"savedItems": null
"savedItemIds": null
},
"queryHistory": {
"homeTab": ""
@ -142,7 +142,7 @@ Content-Type: application/json
"timezone": "",
"weekStart": "",
"navbar": {
"savedItems": null
"savedItemIds": null
},
"queryHistory": {
"homeTab": ""

View File

@ -32,6 +32,9 @@ lineage: schemas: [{
// Cookie preferences
cookiePreferences?: #CookiePreferences
// Navigation preferences
navbar?: #NavbarPreference
} @cuetsy(kind="interface")
#QueryHistoryPreference: {
@ -44,5 +47,9 @@ lineage: schemas: [{
performance?: {}
functional?: {}
} @cuetsy(kind="interface")
#NavbarPreference: {
savedItemIds: [...string]
} @cuetsy(kind="interface")
}
}]

View File

@ -125,9 +125,13 @@ export type { LibraryPanel } from './veneer/librarypanel.types';
export type {
Preferences,
QueryHistoryPreference,
CookiePreferences
CookiePreferences,
NavbarPreference
} from './raw/preferences/x/preferences_types.gen';
// Raw generated enums and default consts from preferences kind.
export { defaultNavbarPreference } from './raw/preferences/x/preferences_types.gen';
// Raw generated types from PublicDashboard kind.
export type { PublicDashboard } from './raw/publicdashboard/x/publicdashboard_types.gen';

View File

@ -21,6 +21,14 @@ export interface CookiePreferences {
performance?: Record<string, unknown>;
}
export interface NavbarPreference {
savedItemIds: Array<string>;
}
export const defaultNavbarPreference: Partial<NavbarPreference> = {
savedItemIds: [],
};
/**
* Spec defines user, team or org Grafana preferences
* swagger:model Preferences
@ -38,6 +46,10 @@ export interface Preferences {
* Selected language (beta)
*/
language?: string;
/**
* Navigation preferences
*/
navbar?: NavbarPreference;
/**
* Explore query history preferences
*/

View File

@ -18,6 +18,7 @@ type UpdatePrefsCmd struct {
QueryHistory *pref.QueryHistoryPreference `json:"queryHistory,omitempty"`
Language string `json:"language"`
Cookies []pref.CookieType `json:"cookies,omitempty"`
Navbar *pref.NavbarPreference `json:"navbar,omitempty"`
}
// swagger:model
@ -34,4 +35,5 @@ type PatchPrefsCmd struct {
QueryHistory *pref.QueryHistoryPreference `json:"queryHistory,omitempty"`
HomeDashboardUID *string `json:"homeDashboardUID,omitempty"`
Cookies []pref.CookieType `json:"cookies,omitempty"`
Navbar *pref.NavbarPreference `json:"navbar,omitempty"`
}

View File

@ -155,6 +155,7 @@ func (hs *HTTPServer) patchPreferencesFor(ctx context.Context, orgID, userID, te
Language: dtoCmd.Language,
QueryHistory: dtoCmd.QueryHistory,
CookiePreferences: dtoCmd.Cookies,
Navbar: dtoCmd.Navbar,
}
if err := hs.preferenceService.Patch(ctx, &patchCmd); err != nil {

View File

@ -16,6 +16,11 @@ type CookiePreferences struct {
Performance map[string]any `json:"performance,omitempty"`
}
// NavbarPreference defines model for NavbarPreference.
type NavbarPreference struct {
SavedItemIds []string `json:"savedItemIds"`
}
// QueryHistoryPreference defines model for QueryHistoryPreference.
type QueryHistoryPreference struct {
// HomeTab one of: '' | 'query' | 'starred';
@ -32,6 +37,7 @@ type Spec struct {
// Selected language (beta)
Language *string `json:"language,omitempty"`
Navbar *NavbarPreference `json:"navbar,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
// Theme light, dark, empty is default

View File

@ -67,6 +67,7 @@ type SavePreferenceCommand struct {
Language string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
CookiePreferences []CookieType `json:"cookiePreferences,omitempty"`
Navbar *NavbarPreference `json:"navbar,omitempty"`
}
type PatchPreferenceCommand struct {
@ -82,18 +83,24 @@ type PatchPreferenceCommand struct {
Language *string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
CookiePreferences []CookieType `json:"cookiePreferences,omitempty"`
Navbar *NavbarPreference `json:"navbar,omitempty"`
}
type PreferenceJSONData struct {
Language string `json:"language"`
QueryHistory QueryHistoryPreference `json:"queryHistory"`
CookiePreferences map[string]struct{} `json:"cookiePreferences"`
Navbar NavbarPreference `json:"navbar"`
}
type QueryHistoryPreference struct {
HomeTab string `json:"homeTab"`
}
type NavbarPreference struct {
SavedItemIds []string `json:"savedItemIds"`
}
func (j *PreferenceJSONData) FromDB(data []byte) error {
dec := json.NewDecoder(bytes.NewBuffer(data))
dec.UseNumber()

View File

@ -46,6 +46,7 @@ func UpdatePreferencesFor(ctx context.Context,
HomeDashboardID: dtoCmd.HomeDashboardID,
QueryHistory: dtoCmd.QueryHistory,
CookiePreferences: dtoCmd.Cookies,
Navbar: dtoCmd.Navbar,
}
if err := preferenceService.Save(ctx, &saveCmd); err != nil {
@ -96,6 +97,13 @@ func GetPreferencesFor(ctx context.Context,
dto.Language = &preference.JSONData.Language
}
if preference.JSONData.Navbar.SavedItemIds != nil {
dto.Navbar = &preferences.NavbarPreference{
SavedItemIds: []string{},
}
dto.Navbar.SavedItemIds = preference.JSONData.Navbar.SavedItemIds
}
if preference.JSONData.QueryHistory.HomeTab != "" {
dto.QueryHistory = &preferences.QueryHistoryPreference{
HomeTab: &preference.JSONData.QueryHistory.HomeTab,

View File

@ -71,6 +71,10 @@ func (s *Service) GetWithDefaults(ctx context.Context, query *pref.GetPreference
res.JSONData.QueryHistory.HomeTab = p.JSONData.QueryHistory.HomeTab
}
if p.JSONData.Navbar.SavedItemIds != nil {
res.JSONData.Navbar.SavedItemIds = p.JSONData.Navbar.SavedItemIds
}
if p.JSONData.CookiePreferences != nil {
res.JSONData.CookiePreferences = p.JSONData.CookiePreferences
}
@ -170,6 +174,13 @@ func (s *Service) Patch(ctx context.Context, cmd *pref.PatchPreferenceCommand) e
preference.JSONData.Language = *cmd.Language
}
if cmd.Navbar != nil && cmd.Navbar.SavedItemIds != nil {
if preference.JSONData == nil {
preference.JSONData = &pref.PreferenceJSONData{}
}
preference.JSONData.Navbar.SavedItemIds = cmd.Navbar.SavedItemIds
}
if cmd.QueryHistory != nil {
if preference.JSONData == nil {
preference.JSONData = &pref.PreferenceJSONData{}
@ -257,6 +268,9 @@ func preferenceData(cmd *pref.SavePreferenceCommand) (*pref.PreferenceJSONData,
Language: cmd.Language,
}
if cmd.Navbar != nil {
jsonData.Navbar = *cmd.Navbar
}
if cmd.QueryHistory != nil {
jsonData.QueryHistory = *cmd.QueryHistory
}

View File

@ -17014,6 +17014,18 @@
}
}
},
"NavbarPreference": {
"type": "object",
"title": "NavbarPreference defines model for NavbarPreference.",
"properties": {
"savedItemIds": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"NewApiKeyResult": {
"type": "object",
"properties": {
@ -17577,6 +17589,9 @@
"language": {
"type": "string"
},
"navbar": {
"$ref": "#/definitions/NavbarPreference"
},
"queryHistory": {
"$ref": "#/definitions/QueryHistoryPreference"
},
@ -18168,6 +18183,9 @@
"description": "Selected language (beta)",
"type": "string"
},
"navbar": {
"$ref": "#/definitions/NavbarPreference"
},
"queryHistory": {
"$ref": "#/definitions/QueryHistoryPreference"
},
@ -21419,6 +21437,9 @@
"language": {
"type": "string"
},
"navbar": {
"$ref": "#/definitions/NavbarPreference"
},
"queryHistory": {
"$ref": "#/definitions/QueryHistoryPreference"
},

View File

@ -7140,6 +7140,18 @@
},
"type": "object"
},
"NavbarPreference": {
"properties": {
"savedItemIds": {
"items": {
"type": "string"
},
"type": "array"
}
},
"title": "NavbarPreference defines model for NavbarPreference.",
"type": "object"
},
"NewApiKeyResult": {
"properties": {
"id": {
@ -7702,6 +7714,9 @@
"language": {
"type": "string"
},
"navbar": {
"$ref": "#/components/schemas/NavbarPreference"
},
"queryHistory": {
"$ref": "#/components/schemas/QueryHistoryPreference"
},
@ -8293,6 +8308,9 @@
"description": "Selected language (beta)",
"type": "string"
},
"navbar": {
"$ref": "#/components/schemas/NavbarPreference"
},
"queryHistory": {
"$ref": "#/components/schemas/QueryHistoryPreference"
},
@ -11543,6 +11561,9 @@
"language": {
"type": "string"
},
"navbar": {
"$ref": "#/components/schemas/NavbarPreference"
},
"queryHistory": {
"$ref": "#/components/schemas/QueryHistoryPreference"
},