-
Notifications
You must be signed in to change notification settings - Fork 8k
/
use_clone_slo.ts
84 lines (75 loc) · 2.87 KB
/
use_clone_slo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* 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 { v1 as uuidv1 } from 'uuid';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import type { CreateSLOInput, CreateSLOResponse, FindSLOResponse } from '@kbn/slo-schema';
import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';
export function useCloneSlo() {
const {
http,
notifications: { toasts },
} = useKibana().services;
const queryClient = useQueryClient();
return useMutation<
CreateSLOResponse,
string,
{ slo: CreateSLOInput; originalSloId?: string },
{ previousSloList: FindSLOResponse | undefined }
>(
['cloneSlo'],
({ slo }: { slo: CreateSLOInput; originalSloId?: string }) => {
const body = JSON.stringify(slo);
return http.post<CreateSLOResponse>(`/api/observability/slos`, { body });
},
{
onMutate: async ({ slo, originalSloId }) => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries(sloKeys.lists());
const latestQueriesData = (
queryClient.getQueriesData<FindSLOResponse>(sloKeys.lists()) ?? []
).at(0);
const [queryKey, data] = latestQueriesData ?? [];
const originalSlo = data?.results?.find((el) => el.id === originalSloId);
const optimisticUpdate = {
...data,
results: [
...(data?.results ?? []),
{ ...originalSlo, name: slo.name, id: uuidv1(), summary: undefined },
],
total: data?.total ? data.total + 1 : 1,
};
// Optimistically update to the new value
queryClient.setQueryData(queryKey ?? sloKeys.lists(), optimisticUpdate);
toasts.addSuccess(
i18n.translate('xpack.observability.slo.clone.successNotification', {
defaultMessage: 'Successfully created {name}',
values: { name: slo.name },
})
);
// Return a context object with the snapshotted value
return { previousSloList: data };
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (_err, { slo }, context) => {
if (context?.previousSloList) {
queryClient.setQueryData(sloKeys.lists(), context.previousSloList);
}
toasts.addDanger(
i18n.translate('xpack.observability.slo.clone.errorNotification', {
defaultMessage: 'Failed to clone {name}',
values: { name: slo.name },
})
);
},
onSuccess: () => {
queryClient.invalidateQueries(sloKeys.lists());
},
}
);
}