|
5 | 5 | * 2.0. |
6 | 6 | */ |
7 | 7 |
|
| 8 | +import React, { FunctionComponent, useState, useEffect } from 'react'; |
8 | 9 | import { i18n } from '@kbn/i18n'; |
9 | 10 | import { get } from 'lodash'; |
10 | | -import React, { FunctionComponent, useState, useEffect } from 'react'; |
11 | 11 | import { FormattedMessage } from '@kbn/i18n/react'; |
12 | | -import { |
13 | | - EuiComboBoxOptionOption, |
14 | | - EuiTextColor, |
15 | | - EuiSpacer, |
16 | | - EuiCallOut, |
17 | | - EuiLink, |
18 | | -} from '@elastic/eui'; |
19 | | - |
20 | | -import { |
21 | | - ComboBoxField, |
22 | | - useKibana, |
23 | | - fieldValidators, |
24 | | - useFormData, |
25 | | -} from '../../../../../../../shared_imports'; |
| 12 | +import { EuiTextColor, EuiSpacer, EuiCallOut, EuiLink } from '@elastic/eui'; |
26 | 13 |
|
| 14 | +import { useKibana, useFormData } from '../../../../../../../shared_imports'; |
27 | 15 | import { useEditPolicyContext } from '../../../../edit_policy_context'; |
28 | | -import { useConfigurationIssues, UseField } from '../../../../form'; |
29 | | - |
30 | | -import { i18nTexts } from '../../../../i18n_texts'; |
31 | | - |
| 16 | +import { useConfiguration, UseField } from '../../../../form'; |
32 | 17 | import { FieldLoadingError, DescribedFormRow, LearnMoreLink } from '../../../'; |
33 | | - |
34 | 18 | import { SearchableSnapshotDataProvider } from './searchable_snapshot_data_provider'; |
| 19 | +import { RepositoryComboBoxField } from './repository_combobox_field'; |
35 | 20 |
|
36 | 21 | import './_searchable_snapshot_field.scss'; |
37 | 22 |
|
38 | | -const { emptyField } = fieldValidators; |
39 | | - |
40 | 23 | export interface Props { |
41 | 24 | phase: 'hot' | 'cold'; |
42 | 25 | } |
43 | 26 |
|
44 | | -/** |
45 | | - * This repository is provisioned by Elastic Cloud and will always |
46 | | - * exist as a "managed" repository. |
47 | | - */ |
48 | | -const CLOUD_DEFAULT_REPO = 'found-snapshots'; |
49 | | - |
50 | 27 | export const SearchableSnapshotField: FunctionComponent<Props> = ({ phase }) => { |
51 | 28 | const { |
52 | 29 | services: { cloud }, |
53 | 30 | } = useKibana(); |
54 | 31 | const { getUrlForApp, policy, license, isNewPolicy } = useEditPolicyContext(); |
55 | | - const { isUsingSearchableSnapshotInHotPhase } = useConfigurationIssues(); |
| 32 | + const { isUsingSearchableSnapshotInHotPhase } = useConfiguration(); |
| 33 | + |
| 34 | + const searchableSnapshotRepoPath = `phases.${phase}.actions.searchable_snapshot.snapshot_repository`; |
| 35 | + const searchableSnapshotRepoGlobalPath = `_meta.searchableSnapshot.repository`; |
56 | 36 |
|
57 | | - const searchableSnapshotPath = `phases.${phase}.actions.searchable_snapshot.snapshot_repository`; |
| 37 | + const [formData] = useFormData({ |
| 38 | + watch: [searchableSnapshotRepoGlobalPath], |
| 39 | + }); |
58 | 40 |
|
59 | | - const [formData] = useFormData({ watch: searchableSnapshotPath }); |
60 | | - const searchableSnapshotRepo = get(formData, searchableSnapshotPath); |
| 41 | + const searchableSnapshotGlobalRepo = get(formData, searchableSnapshotRepoGlobalPath); |
61 | 42 |
|
62 | 43 | const isColdPhase = phase === 'cold'; |
63 | 44 | const isDisabledDueToLicense = !license.canUseSearchableSnapshot(); |
@@ -141,7 +122,10 @@ export const SearchableSnapshotField: FunctionComponent<Props> = ({ phase }) => |
141 | 122 | /> |
142 | 123 | </EuiCallOut> |
143 | 124 | ); |
144 | | - } else if (searchableSnapshotRepo && !repos.includes(searchableSnapshotRepo)) { |
| 125 | + } else if ( |
| 126 | + searchableSnapshotGlobalRepo && |
| 127 | + !repos.includes(searchableSnapshotGlobalRepo) |
| 128 | + ) { |
145 | 129 | calloutContent = ( |
146 | 130 | <EuiCallOut |
147 | 131 | title={i18n.translate( |
@@ -178,56 +162,17 @@ export const SearchableSnapshotField: FunctionComponent<Props> = ({ phase }) => |
178 | 162 |
|
179 | 163 | return ( |
180 | 164 | <div className="ilmSearchableSnapshotField"> |
181 | | - <UseField<string> |
182 | | - config={{ |
183 | | - label: i18nTexts.editPolicy.searchableSnapshotsFieldLabel, |
184 | | - defaultValue: cloud?.isCloudEnabled ? CLOUD_DEFAULT_REPO : undefined, |
185 | | - validations: [ |
186 | | - { |
187 | | - validator: emptyField( |
188 | | - i18nTexts.editPolicy.errors.searchableSnapshotRepoRequired |
189 | | - ), |
190 | | - }, |
191 | | - ], |
| 165 | + <UseField |
| 166 | + path={searchableSnapshotRepoPath} |
| 167 | + defaultValue={[searchableSnapshotGlobalRepo]} |
| 168 | + component={RepositoryComboBoxField} |
| 169 | + componentProps={{ |
| 170 | + globalRepository: searchableSnapshotGlobalRepo, |
| 171 | + isLoading, |
| 172 | + repos, |
| 173 | + noSuggestions: !!(error || repos.length === 0), |
192 | 174 | }} |
193 | | - path={searchableSnapshotPath} |
194 | | - > |
195 | | - {(field) => { |
196 | | - const singleSelectionArray: [selectedSnapshot?: string] = field.value |
197 | | - ? [field.value] |
198 | | - : []; |
199 | | - |
200 | | - return ( |
201 | | - <ComboBoxField |
202 | | - field={ |
203 | | - { |
204 | | - ...field, |
205 | | - value: singleSelectionArray, |
206 | | - } as any |
207 | | - } |
208 | | - label={field.label} |
209 | | - fullWidth={false} |
210 | | - euiFieldProps={{ |
211 | | - 'data-test-subj': 'searchableSnapshotCombobox', |
212 | | - options: repos.map((repo) => ({ label: repo, value: repo })), |
213 | | - singleSelection: { asPlainText: true }, |
214 | | - isLoading, |
215 | | - noSuggestions: !!(error || repos.length === 0), |
216 | | - onCreateOption: (newOption: string) => { |
217 | | - field.setValue(newOption); |
218 | | - }, |
219 | | - onChange: (options: EuiComboBoxOptionOption[]) => { |
220 | | - if (options.length > 0) { |
221 | | - field.setValue(options[0].label); |
222 | | - } else { |
223 | | - field.setValue(''); |
224 | | - } |
225 | | - }, |
226 | | - }} |
227 | | - /> |
228 | | - ); |
229 | | - }} |
230 | | - </UseField> |
| 175 | + /> |
231 | 176 | {calloutContent && ( |
232 | 177 | <> |
233 | 178 | <EuiSpacer size="s" /> |
|
0 commit comments