forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tech Debt] Refactor Rule Details page (elastic#160229)
Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
aec3a4b
commit ef661d1
Showing
28 changed files
with
1,018 additions
and
921 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/observability/public/hooks/use_delete_rules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useMutation } from '@tanstack/react-query'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { KueryNode } from '@kbn/es-query'; | ||
import { INTERNAL_BASE_ALERTING_API_PATH } from '@kbn/alerting-plugin/common'; | ||
import { useKibana } from '../utils/kibana_react'; | ||
|
||
export function useDeleteRules() { | ||
const { | ||
http, | ||
notifications: { toasts }, | ||
} = useKibana().services; | ||
|
||
const deleteRules = useMutation< | ||
string, | ||
string, | ||
{ ids: string[]; filter?: KueryNode | null | undefined } | ||
>( | ||
['deleteRules'], | ||
({ ids, filter }) => { | ||
try { | ||
const body = JSON.stringify({ | ||
...(ids?.length ? { ids } : {}), | ||
...(filter ? { filter: JSON.stringify(filter) } : {}), | ||
}); | ||
return http.patch(`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_bulk_delete`, { body }); | ||
} catch (e) { | ||
throw new Error(`Unable to parse bulk delete params: ${e}`); | ||
} | ||
}, | ||
{ | ||
onError: (_err, rule, context) => { | ||
toasts.addDanger( | ||
i18n.translate( | ||
'xpack.observability.rules.deleteConfirmationModal.errorNotification.descriptionText', | ||
{ | ||
defaultMessage: 'Failed to delete rule', | ||
} | ||
) | ||
); | ||
}, | ||
|
||
onSuccess: () => { | ||
toasts.addSuccess( | ||
i18n.translate( | ||
'xpack.observability.rules.deleteConfirmationModal.successNotification.descriptionText', | ||
{ | ||
defaultMessage: 'Deleted rule', | ||
} | ||
) | ||
); | ||
}, | ||
} | ||
); | ||
|
||
return deleteRules; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
x-pack/plugins/observability/public/hooks/use_fetch_rule_summary.ts
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
x-pack/plugins/observability/public/hooks/use_fetch_rule_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
QueryObserverResult, | ||
RefetchOptions, | ||
RefetchQueryFilters, | ||
useQuery, | ||
} from '@tanstack/react-query'; | ||
import { camelCase, mapKeys } from 'lodash'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { BASE_ALERTING_API_PATH } from '@kbn/alerting-plugin/common'; | ||
import type { RuleType } from '@kbn/triggers-actions-ui-plugin/public'; | ||
import type { AsApiContract } from '@kbn/actions-plugin/common'; | ||
import { useKibana } from '../utils/kibana_react'; | ||
|
||
export interface UseFetchRuleTypesResponse { | ||
isInitialLoading: boolean; | ||
isLoading: boolean; | ||
isRefetching: boolean; | ||
isSuccess: boolean; | ||
isError: boolean; | ||
ruleTypes: RuleType[] | undefined; | ||
refetch: <TPageData>( | ||
options?: (RefetchOptions & RefetchQueryFilters<TPageData>) | undefined | ||
) => Promise<QueryObserverResult<RuleType[] | undefined, unknown>>; | ||
} | ||
|
||
export function useFetchRuleTypes({ | ||
filterByRuleTypeIds, | ||
}: { | ||
filterByRuleTypeIds?: string[] | undefined; | ||
}): UseFetchRuleTypesResponse { | ||
const { | ||
http, | ||
notifications: { toasts }, | ||
} = useKibana().services; | ||
|
||
const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery( | ||
{ | ||
queryKey: ['fetchRuleTypes', filterByRuleTypeIds], | ||
queryFn: async ({ signal }) => { | ||
try { | ||
const res = await http.get<Array<AsApiContract<RuleType<string, string>>>>( | ||
`${BASE_ALERTING_API_PATH}/rule_types`, | ||
{ signal } | ||
); | ||
|
||
const response = res.map((item) => { | ||
return mapKeys(item, (_, k) => camelCase(k)); | ||
}) as unknown as Array<RuleType<string, string>>; | ||
|
||
return filterByRuleTypeIds && filterByRuleTypeIds.length > 0 | ||
? response.filter((item) => filterByRuleTypeIds.includes(item.id)) | ||
: response; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}, | ||
keepPreviousData: true, | ||
refetchOnWindowFocus: false, | ||
onError: (error: Error) => { | ||
toasts.addError(error, { | ||
title: i18n.translate('xpack.observability.ruleDetails.ruleLoadError', { | ||
defaultMessage: 'Unable to load rule. Reason: {message}', | ||
values: { | ||
message: | ||
error instanceof Error ? error.message : typeof error === 'string' ? error : '', | ||
}, | ||
}), | ||
}); | ||
}, | ||
} | ||
); | ||
|
||
return { | ||
ruleTypes: data, | ||
isLoading, | ||
isInitialLoading, | ||
isRefetching, | ||
isSuccess, | ||
isError, | ||
refetch, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.