|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { i18n } from '@kbn/i18n'; |
10 | | -import type { DataView, DataViewsContract, DataViewSpec } from '@kbn/data-views-plugin/public'; |
| 10 | +import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public'; |
11 | 11 | import type { ISearchSource } from '@kbn/data-plugin/public'; |
12 | 12 | import type { IUiSettingsClient, SavedObject, ToastsStart } from '@kbn/core/public'; |
13 | 13 | export type IndexPatternSavedObject = SavedObject & { title: string }; |
@@ -75,27 +75,23 @@ export function getIndexPatternId( |
75 | 75 | * Function to load the given index pattern by id, providing a fallback if it doesn't exist |
76 | 76 | */ |
77 | 77 | export async function loadIndexPattern( |
78 | | - id: string | DataViewSpec, |
79 | | - dataViews: DataViewsContract, |
| 78 | + id: string, |
| 79 | + indexPatterns: DataViewsContract, |
80 | 80 | config: IUiSettingsClient |
81 | 81 | ): Promise<IndexPatternData> { |
82 | | - const indexPatternList = (await dataViews.getCache()) as unknown as IndexPatternSavedObject[]; |
83 | | - |
84 | | - if (typeof id === 'string') { |
85 | | - const actualId = getIndexPatternId(id, indexPatternList, config.get('defaultIndex')); |
86 | | - return { |
87 | | - list: indexPatternList || [], |
88 | | - loaded: await dataViews.get(actualId), |
89 | | - stateVal: id, |
90 | | - stateValFound: !!id && actualId === id, |
91 | | - }; |
| 82 | + let dataView; |
| 83 | + try { |
| 84 | + dataView = await indexPatterns.get(id); |
| 85 | + } catch (e) { |
| 86 | + dataView = await config.get('defaultIndex'); |
92 | 87 | } |
93 | | - const dataView = await dataViews.create(id); |
| 88 | + const indexPatternList = (await indexPatterns.getCache()) as unknown as IndexPatternSavedObject[]; |
| 89 | + |
94 | 90 | return { |
95 | 91 | list: indexPatternList || [], |
96 | 92 | loaded: dataView, |
97 | | - stateVal: dataView.id!, |
98 | | - stateValFound: true, |
| 93 | + stateVal: id, |
| 94 | + stateValFound: !!id && dataView.id === id, |
99 | 95 | }; |
100 | 96 | } |
101 | 97 |
|
|
0 commit comments