Skip to content

Commit

Permalink
Bookmarks: Store URLs instead of ids (grafana#91121)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoSilvaGrafana authored Jul 31, 2024
1 parent 5623800 commit 85d0e17
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/sources/developers/http_api/preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Content-Type: application/json
"timezone": "utc",
"weekStart": "",
"navbar": {
"bookmarkIds": null
"bookmarkUrls": null
},
"queryHistory": {
"homeTab": ""
Expand Down Expand Up @@ -142,7 +142,7 @@ Content-Type: application/json
"timezone": "",
"weekStart": "",
"navbar": {
"bookmarkIds": null
"bookmarkUrls": null
},
"queryHistory": {
"homeTab": ""
Expand Down
2 changes: 1 addition & 1 deletion kinds/preferences/preferences_kind.cue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lineage: schemas: [{
} @cuetsy(kind="interface")

#NavbarPreference: {
bookmarkIds: [...string]
bookmarkUrls: [...string]
} @cuetsy(kind="interface")
}
}]
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export interface CookiePreferences {
}

export interface NavbarPreference {
bookmarkIds: Array<string>;
bookmarkUrls: Array<string>;
}

export const defaultNavbarPreference: Partial<NavbarPreference> = {
bookmarkIds: [],
bookmarkUrls: [],
};

/**
Expand Down
2 changes: 1 addition & 1 deletion pkg/kinds/preferences/preferences_spec_gen.go

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

8 changes: 4 additions & 4 deletions pkg/services/navtree/navtreeimpl/navtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ func (s *ServiceImpl) buildStarredItemsNavLinks(c *contextmodel.ReqContext) ([]*
func (s *ServiceImpl) buildBookmarksNavLinks(prefs *pref.Preference, treeRoot *navtree.NavTreeRoot) []*navtree.NavLink {
bookmarksChildNavs := []*navtree.NavLink{}

bookmarkIds := prefs.JSONData.Navbar.BookmarkIds
bookmarkUrls := prefs.JSONData.Navbar.BookmarkUrls

if len(bookmarkIds) > 0 {
for _, id := range bookmarkIds {
item := treeRoot.FindById(id)
if len(bookmarkUrls) > 0 {
for _, url := range bookmarkUrls {
item := treeRoot.FindByURL(url)
if item != nil {
bookmarksChildNavs = append(bookmarksChildNavs, &navtree.NavLink{
Id: item.Id,
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/preference/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type QueryHistoryPreference struct {
}

type NavbarPreference struct {
BookmarkIds []string `json:"bookmarkIds"`
BookmarkUrls []string `json:"bookmarkUrls"`
}

func (j *PreferenceJSONData) FromDB(data []byte) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/preference/prefapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func GetPreferencesFor(ctx context.Context,
dto.Language = &preference.JSONData.Language
}

if preference.JSONData.Navbar.BookmarkIds != nil {
if preference.JSONData.Navbar.BookmarkUrls != nil {
dto.Navbar = &preferences.NavbarPreference{
BookmarkIds: []string{},
BookmarkUrls: []string{},
}
dto.Navbar.BookmarkIds = preference.JSONData.Navbar.BookmarkIds
dto.Navbar.BookmarkUrls = preference.JSONData.Navbar.BookmarkUrls
}

if preference.JSONData.QueryHistory.HomeTab != "" {
Expand Down
8 changes: 4 additions & 4 deletions pkg/services/preference/prefimpl/pref.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (s *Service) GetWithDefaults(ctx context.Context, query *pref.GetPreference
res.JSONData.QueryHistory.HomeTab = p.JSONData.QueryHistory.HomeTab
}

if p.JSONData.Navbar.BookmarkIds != nil {
res.JSONData.Navbar.BookmarkIds = p.JSONData.Navbar.BookmarkIds
if p.JSONData.Navbar.BookmarkUrls != nil {
res.JSONData.Navbar.BookmarkUrls = p.JSONData.Navbar.BookmarkUrls
}

if p.JSONData.CookiePreferences != nil {
Expand Down Expand Up @@ -174,11 +174,11 @@ func (s *Service) Patch(ctx context.Context, cmd *pref.PatchPreferenceCommand) e
preference.JSONData.Language = *cmd.Language
}

if cmd.Navbar != nil && cmd.Navbar.BookmarkIds != nil {
if cmd.Navbar != nil && cmd.Navbar.BookmarkUrls != nil {
if preference.JSONData == nil {
preference.JSONData = &pref.PreferenceJSONData{}
}
preference.JSONData.Navbar.BookmarkIds = cmd.Navbar.BookmarkIds
preference.JSONData.Navbar.BookmarkUrls = cmd.Navbar.BookmarkUrls
}

if cmd.QueryHistory != nil {
Expand Down
2 changes: 1 addition & 1 deletion public/api-merged.json
Original file line number Diff line number Diff line change
Expand Up @@ -17112,7 +17112,7 @@
"type": "object",
"title": "NavbarPreference defines model for NavbarPreference.",
"properties": {
"bookmarkIds": {
"bookmarkUrls": {
"type": "array",
"items": {
"type": "string"
Expand Down
19 changes: 9 additions & 10 deletions public/app/core/components/AppChrome/MegaMenu/MegaMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,28 @@ export const MegaMenu = memo(
};

const isPinned = useCallback(
(id?: string) => {
if (!id || !pinnedItems?.length) {
(url?: string) => {
if (!url || !pinnedItems?.length) {
return false;
}
return pinnedItems?.includes(id);
return pinnedItems?.includes(url);
},
[pinnedItems]
);

const onPinItem = (item: NavModelItem) => {
const id = item.id;
if (id && config.featureToggles.pinNavItems) {
const navItem = navTree.find((item) => item.id === id);
const isSaved = isPinned(id);
const newItems = isSaved ? pinnedItems.filter((i) => id !== i) : [...pinnedItems, id];
const url = item.url;
if (url && config.featureToggles.pinNavItems) {
const isSaved = isPinned(url);
const newItems = isSaved ? pinnedItems.filter((i) => url !== i) : [...pinnedItems, url];
const interactionName = isSaved ? 'grafana_nav_item_unpinned' : 'grafana_nav_item_pinned';
reportInteraction(interactionName, {
path: navItem?.url ?? id,
path: url,
});
patchPreferences({
patchPrefsCmd: {
navbar: {
bookmarkIds: newItems,
bookmarkUrls: newItems,
},
},
}).then((data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ export function MegaMenuItem({ link, activeItem, level = 0, onClick, onPin, isPi
}}
target={link.target}
url={link.url}
id={link.id}
onPin={() => onPin(link)}
isPinned={isPinned(link.id)}
isPinned={isPinned(link.url)}
>
<div
className={cx(styles.labelWrapper, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ export interface Props {
onClick?: () => void;
target?: HTMLAnchorElement['target'];
url: string;
id?: string;
onPin: (id?: string) => void;
isPinned?: boolean;
}

export function MegaMenuItemText({ children, isActive, onClick, target, url, id, onPin, isPinned }: Props) {
export function MegaMenuItemText({ children, isActive, onClick, target, url, onPin, isPinned }: Props) {
const theme = useTheme2();
const styles = getStyles(theme, isActive);
const LinkComponent = !target && url.startsWith('/') ? Link : 'a';
Expand Down Expand Up @@ -51,12 +50,12 @@ export function MegaMenuItemText({ children, isActive, onClick, target, url, id,
>
{linkContent}
</LinkComponent>
{config.featureToggles.pinNavItems && id && id !== 'bookmarks' && (
{config.featureToggles.pinNavItems && url && url !== '/bookmarks' && (
<IconButton
name="bookmark"
className={'pin-icon'}
iconType={isPinned ? 'solid' : 'default'}
onClick={() => onPin(id)}
onClick={() => onPin(url)}
aria-label={
isPinned
? t('navigation.item.remove-bookmark', 'Remove from Bookmarks')
Expand Down
2 changes: 1 addition & 1 deletion public/app/core/components/AppChrome/MegaMenu/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useGetUserPreferencesQuery } from 'app/features/preferences/api';

export const usePinnedItems = () => {
const preferences = useGetUserPreferencesQuery();
const pinnedItems = useMemo(() => preferences.data?.navbar?.bookmarkIds || [], [preferences]);
const pinnedItems = useMemo(() => preferences.data?.navbar?.bookmarkUrls || [], [preferences]);

if (config.featureToggles.pinNavItems) {
return pinnedItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class SharedPreferences extends PureComponent<Props, State> {
weekStart: '',
language: '',
queryHistory: { homeTab: '' },
navbar: { bookmarkIds: [] },
navbar: { bookmarkUrls: [] },
};

this.themeOptions = getBuiltInThemes(config.featureToggles.extraThemes).map((theme) => ({
Expand Down
2 changes: 1 addition & 1 deletion public/app/core/reducers/navBarTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const navTreeSlice = createSlice({
};
bookmarks.children.push(newBookmark);
} else {
bookmarks.children = bookmarks.children?.filter((i) => i.id !== item.id) ?? [];
bookmarks.children = bookmarks.children?.filter((i) => i.url !== item.url) ?? [];
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion public/app/features/preferences/api/user/endpoints.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type CookiePreferencesDefinesModelForCookiePreferences = {
};
};
export type NavbarPreferenceDefinesModelForNavbarPreference = {
bookmarkIds?: string[];
bookmarkUrls?: string[];
};
export type QueryHistoryPreferenceDefinesModelForQueryHistoryPreference = {
/** HomeTab one of: '' | 'query' | 'starred'; */
Expand Down
2 changes: 1 addition & 1 deletion public/openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -7188,7 +7188,7 @@
},
"NavbarPreference": {
"properties": {
"bookmarkIds": {
"bookmarkUrls": {
"items": {
"type": "string"
},
Expand Down

0 comments on commit 85d0e17

Please sign in to comment.