55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import type { components } from '../schema';
|
|
import { apiClient, useSelectedEnv } from './alertMeta';
|
|
|
|
export type AlertNotificationDto = components['schemas']['AlertNotificationDto'];
|
|
|
|
// NOTE ON TYPES: the generated OpenAPI schema for env-scoped alert-notification
|
|
// endpoints emits `path?: never` plus a `query.env: Environment` parameter
|
|
// because the server resolves the env via the `@EnvPath` argument resolver,
|
|
// which the OpenAPI scanner does not recognise as a path variable. At runtime
|
|
// the URL template `{envSlug}` is substituted from `params.path.envSlug` by
|
|
// openapi-fetch regardless of what the TS types say; we therefore cast the
|
|
// call options to `any` on each call to bypass the generated type oddity.
|
|
|
|
/** List notifications for a given alert instance. */
|
|
export function useAlertNotifications(alertId: string | undefined) {
|
|
const env = useSelectedEnv();
|
|
return useQuery({
|
|
queryKey: ['alertNotifications', env, alertId],
|
|
enabled: !!env && !!alertId,
|
|
queryFn: async () => {
|
|
if (!env || !alertId) throw new Error('no env/alertId');
|
|
const { data, error } = await apiClient.GET(
|
|
'/environments/{envSlug}/alerts/{alertId}/notifications',
|
|
{
|
|
params: { path: { envSlug: env, alertId } },
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as AlertNotificationDto[];
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Retry a failed notification. Uses the flat path — notification IDs are
|
|
* globally unique across environments, so the endpoint is not env-scoped.
|
|
*/
|
|
export function useRetryNotification() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
const { error } = await apiClient.POST(
|
|
'/alerts/notifications/{id}/retry',
|
|
{
|
|
params: { path: { id } },
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['alertNotifications'] });
|
|
},
|
|
});
|
|
}
|