2025-06-12 17:03:52 +08:00
|
|
|
import { t } from '@grafana/i18n';
|
2025-04-17 23:00:10 +08:00
|
|
|
import { config } from '@grafana/runtime';
|
2025-05-05 20:00:20 +08:00
|
|
|
import { useScopesServices } from 'app/features/scopes/ScopesContextProvider';
|
2025-04-17 23:00:10 +08:00
|
|
|
|
|
|
|
import { CommandPaletteAction } from '../types';
|
|
|
|
import { RECENT_SCOPES_PRIORITY } from '../values';
|
|
|
|
|
2025-10-07 19:14:36 +08:00
|
|
|
export function useRecentScopesActions(): CommandPaletteAction[] {
|
2025-05-05 20:00:20 +08:00
|
|
|
const services = useScopesServices();
|
2025-06-12 17:03:52 +08:00
|
|
|
|
2025-05-05 20:00:20 +08:00
|
|
|
if (!(config.featureToggles.scopeFilters && services)) {
|
2025-04-17 23:00:10 +08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2025-05-05 20:00:20 +08:00
|
|
|
const { scopesSelectorService } = services;
|
2025-04-17 23:00:10 +08:00
|
|
|
const recentScopes = scopesSelectorService.getRecentScopes();
|
|
|
|
|
|
|
|
return recentScopes.map((recentScope) => {
|
2025-09-30 23:12:42 +08:00
|
|
|
const names = recentScope.map((scope) => scope.spec.title).join(', ');
|
|
|
|
const keywords = recentScope
|
|
|
|
.map((scope) => `${scope.spec.title} ${scope.metadata.name}`)
|
|
|
|
.concat(names)
|
|
|
|
.join(' ');
|
2025-04-17 23:00:10 +08:00
|
|
|
return {
|
2025-09-30 23:12:42 +08:00
|
|
|
id: names,
|
|
|
|
name: names,
|
|
|
|
section: {
|
|
|
|
name: t('command-palette.section.recent-scopes', 'Recent scopes'),
|
|
|
|
priority: RECENT_SCOPES_PRIORITY,
|
|
|
|
},
|
2025-08-05 20:09:58 +08:00
|
|
|
subtitle: recentScope[0]?.parentNode?.spec.title,
|
2025-09-30 23:12:42 +08:00
|
|
|
keywords: keywords,
|
2025-04-17 23:00:10 +08:00
|
|
|
priority: RECENT_SCOPES_PRIORITY,
|
|
|
|
perform: () => {
|
2025-06-10 16:21:43 +08:00
|
|
|
scopesSelectorService.changeScopes(recentScope.map((scope) => scope.metadata.name));
|
2025-04-17 23:00:10 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|