mirror of https://github.com/grafana/grafana
Restore dashboards: Add filters and search (#106994)
* Restore dashboards: Enable search and filtering * Remove sorting * Configurable sort * Move cache to a separate file * Get tags * Reset cache on delete * Use store * Add sort * Use fuzzyFind for search * Move fuzzy search to grafana/data * Move @leeoniya/ufuzzy package * Use the new util * Improve sort * Error handlingpull/107251/head^2
parent
30e72ca774
commit
ecb93ed7f7
@ -0,0 +1,58 @@ |
||||
import { isResourceList } from 'app/features/apiserver/guards'; |
||||
import { getDashboardAPI } from 'app/features/dashboard/api/dashboard_api'; |
||||
|
||||
import { DashboardDataDTO } from '../../../types'; |
||||
|
||||
import { SearchHit } from './unified'; |
||||
import { resourceToSearchResult } from './utils'; |
||||
|
||||
/** |
||||
* Store deleted dashboards in the cache to avoid multiple calls to the API. |
||||
*/ |
||||
class DeletedDashboardsCache { |
||||
private cache: SearchHit[] | null = null; |
||||
private promise: Promise<SearchHit[]> | null = null; |
||||
|
||||
async get(): Promise<SearchHit[]> { |
||||
if (this.cache !== null) { |
||||
return this.cache; |
||||
} |
||||
|
||||
if (this.promise !== null) { |
||||
return this.promise; |
||||
} |
||||
|
||||
this.promise = this.fetch(); |
||||
|
||||
try { |
||||
this.cache = await this.promise; |
||||
return this.cache; |
||||
} catch (error) { |
||||
this.promise = null; |
||||
throw error; |
||||
} |
||||
} |
||||
|
||||
clear(): void { |
||||
this.cache = null; |
||||
this.promise = null; |
||||
} |
||||
|
||||
private async fetch(): Promise<SearchHit[]> { |
||||
try { |
||||
const api = getDashboardAPI(); |
||||
const deletedResponse = await api.listDeletedDashboards({ limit: 1000 }); |
||||
|
||||
if (isResourceList<DashboardDataDTO>(deletedResponse)) { |
||||
return resourceToSearchResult(deletedResponse); |
||||
} |
||||
|
||||
return []; |
||||
} catch (error) { |
||||
console.error('Failed to fetch deleted dashboards:', error); |
||||
return []; |
||||
} |
||||
} |
||||
} |
||||
|
||||
export const deletedDashboardsCache = new DeletedDashboardsCache(); |
Loading…
Reference in new issue