openreplay/frontend/app/components/Client/Integrations/apiMethods.ts
Delirium c144add4bd
Backend logs UI (#2635)
* backend integrations ui start

* some more ui things

* moving around some integration code

* add dynatrace

* add datadog, useform hook and other things to update backend logging integration forms

* change api queries

* backend logging modals

* tracker: fix some types

* remove deprecated integrations, update types

* some ui fixes and improvements

* update notifications on success/error

* ui: debugging log output, autoclose fix

* ui: some stuff for logs base64str

* ui: improve log formatting,  change datadog data format

* some improvs for logging irm

* ui: fixup for sentry
2024-10-29 15:15:28 +01:00

85 lines
2.1 KiB
TypeScript

import { client } from "App/mstore";
import { useQuery, useMutation } from '@tanstack/react-query';
import { toast } from 'react-toastify';
export type ServiceName = 'datadog' | 'dynatrace' | 'elasticsearch' | 'sentry'
export const serviceNames: Record<ServiceName, string> = {
datadog: 'Datadog',
dynatrace: 'Dynatrace',
elasticsearch: 'Elastic',
sentry: 'Sentry',
};
export async function getIntegrationData<T>(name: ServiceName, projectId: string): Promise<T> {
const r = await client.get(
`/integrations/v1/integrations/${name}/${projectId}`
);
return r.json();
}
export function useIntegration<T>(name: ServiceName, projectId: string, initialValues: T) {
const { data, isPending } = useQuery({
queryKey: ['integrationData', name],
queryFn: async () => {
const resp = await getIntegrationData<T>(
name,
projectId
);
if (resp) {
return resp;
}
return initialValues;
},
initialData: initialValues,
});
const saveMutation = useMutation({
mutationFn: ({ values, siteId, exists }: {
values: T;
siteId: string;
exists?: boolean;
}) =>
saveIntegration(name, values, siteId, exists),
});
const removeMutation = useMutation({
mutationFn: ({ siteId }: {
siteId: string;
}) => removeIntegration(name, siteId),
});
return {
data,
isPending,
saveMutation,
removeMutation,
};
}
export async function saveIntegration<T>(
name: string,
data: T,
projectId: string,
exists?: boolean
) {
const method = exists ? 'patch' : 'post';
const r = await client[method](
`/integrations/v1/integrations/${name}/${projectId}`,
{ data }
);
if (r.ok) {
toast.success(`${name} integration saved`);
} else {
toast.error(`Failed to save ${name} integration`);
}
return r.ok;
}
export async function removeIntegration(name: string, projectId: string) {
const r = await client.delete(`/integrations/v1/integrations/${name}/${projectId}`);
if (r.ok) {
toast.success(`${name} integration removed`);
} else {
toast.error(`Failed to remove ${name} integration`);
}
return r.ok;
}