Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.1.x] BrowseDashboards: Only remember the most recent expanded folder #74809

Merged
merged 1 commit into from Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions public/app/app.ts
Expand Up @@ -83,6 +83,7 @@ import { preloadPlugins } from './features/plugins/pluginPreloader';
import { QueryRunner } from './features/query/state/QueryRunner';
import { runRequest } from './features/query/state/runRequest';
import { initWindowRuntime } from './features/runtime/init';
import { cleanupOldExpandedFolders } from './features/search/utils';
import { variableAdapters } from './features/variables/adapters';
import { createAdHocVariableAdapter } from './features/variables/adhoc/adapter';
import { createConstantVariableAdapter } from './features/variables/constant/adapter';
Expand Down Expand Up @@ -216,6 +217,13 @@ export class GrafanaApp {
// Read initial kiosk mode from url at app startup
chromeService.setKioskModeFromUrl(queryParams.kiosk);

// Clean up old search local storage values
try {
cleanupOldExpandedFolders();
} catch (err) {
console.warn('Failed to clean up old expanded folders', err);
}

this.context = {
backend: backendSrv,
location: locationService,
Expand Down
1 change: 1 addition & 0 deletions public/app/features/search/constants.ts
Expand Up @@ -6,6 +6,7 @@ export const SEARCH_ITEM_HEIGHT = 58;
export const SEARCH_ITEM_MARGIN = 8;
export const DEFAULT_SORT = { label: 'A\u2013Z', value: 'alpha-asc' };
export const SECTION_STORAGE_KEY = 'search.sections';
export const SEARCH_EXPANDED_FOLDER_STORAGE_KEY = 'grafana.search.expanded-folder';
export const GENERAL_FOLDER_ID = 0;
export const GENERAL_FOLDER_UID = 'general';
export const GENERAL_FOLDER_TITLE = 'General';
Expand Down
29 changes: 23 additions & 6 deletions public/app/features/search/page/components/FolderSection.tsx
@@ -1,16 +1,15 @@
import { css } from '@emotion/css';
import React, { useCallback, useId } from 'react';
import { useAsync, useLocalStorage } from 'react-use';
import React, { useCallback, useId, useState } from 'react';
import { useAsync } from 'react-use';

import { GrafanaTheme2, toIconName } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Card, Checkbox, CollapsableSection, Icon, Spinner, useStyles2 } from '@grafana/ui';
import { config } from 'app/core/config';
import { t } from 'app/core/internationalization';
import { getSectionStorageKey } from 'app/features/search/utils';

import { SearchItem } from '../..';
import { GENERAL_FOLDER_UID } from '../../constants';
import { GENERAL_FOLDER_UID, SEARCH_EXPANDED_FOLDER_STORAGE_KEY } from '../../constants';
import { getGrafanaSearcher } from '../../service';
import { getFolderChildren } from '../../service/folders';
import { queryResultToViewItem } from '../../service/utils';
Expand Down Expand Up @@ -57,9 +56,14 @@ export const FolderSection = ({
renderStandaloneBody,
tags,
}: SectionHeaderProps) => {
const uid = section.uid;
const editable = selectionToggle != null;

const styles = useStyles2(useCallback((theme: GrafanaTheme2) => getSectionHeaderStyles(theme, editable), [editable]));
const [sectionExpanded, setSectionExpanded] = useLocalStorage(getSectionStorageKey(section.title), false);
const [sectionExpanded, setSectionExpanded] = useState(() => {
const lastExpandedFolder = window.localStorage.getItem(SEARCH_EXPANDED_FOLDER_STORAGE_KEY);
return lastExpandedFolder === uid;
});

const results = useAsync(async () => {
if (!sectionExpanded && !renderStandaloneBody) {
Expand All @@ -72,7 +76,20 @@ export const FolderSection = ({
}, [sectionExpanded, tags]);

const onSectionExpand = () => {
setSectionExpanded(!sectionExpanded);
const newExpandedValue = !sectionExpanded;

if (newExpandedValue) {
// If we've just expanded the section, remember it to local storage
window.localStorage.setItem(SEARCH_EXPANDED_FOLDER_STORAGE_KEY, uid);
} else {
// Else, when closing a section, remove it from local storage only if this folder was the most recently opened
const lastExpandedFolder = window.localStorage.getItem(SEARCH_EXPANDED_FOLDER_STORAGE_KEY);
if (lastExpandedFolder === uid) {
window.localStorage.removeItem(SEARCH_EXPANDED_FOLDER_STORAGE_KEY);
}
}

setSectionExpanded(newExpandedValue);
};

const onToggleFolder = (evt: React.FormEvent) => {
Expand Down
12 changes: 12 additions & 0 deletions public/app/features/search/utils.ts
Expand Up @@ -14,6 +14,18 @@ export const hasFilters = (query: SearchState) => {
return Boolean(query.query || query.tag?.length > 0 || query.starred || query.sort);
};

/** Cleans up old local storage values that remembered many open folders */
export const cleanupOldExpandedFolders = () => {
const keyPrefix = SECTION_STORAGE_KEY + '.';

for (let index = 0; index < window.localStorage.length; index++) {
const lsKey = window.localStorage.key(index);
if (lsKey?.startsWith(keyPrefix)) {
window.localStorage.removeItem(lsKey);
}
}
};

/**
* Get storage key for a dashboard folder by its title
* @param title
Expand Down