UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

1 lines 3.75 kB
{"version":3,"file":"useContactPoints.mjs","sources":["../../../../../../src/grafana/contactPoints/hooks/v0alpha1/useContactPoints.tsx"],"sourcesContent":["import {\n type TypedUseMutationResult,\n type TypedUseQueryHookResult,\n fetchBaseQuery,\n} from '@reduxjs/toolkit/query/react';\nimport { OverrideProperties } from 'type-fest';\n\nimport {\n CreateReceiverApiArg,\n type ListReceiverApiArg,\n notificationsAPI,\n} from '../../../api/notifications/v0alpha1/notifications.api.gen';\nimport type { ContactPoint, EnhancedListReceiverApiResponse } from '../../../api/notifications/v0alpha1/types';\n\n// this is a workaround for the fact that the generated types are not narrow enough\ntype ListContactPointsHookResult = TypedUseQueryHookResult<\n EnhancedListReceiverApiResponse,\n ListReceiverApiArg,\n ReturnType<typeof fetchBaseQuery>\n>;\n\n// Type for the options that can be passed to the hook\n// Based on the pattern used for mutation options in this file\ntype ListContactPointsQueryArgs = Parameters<\n typeof notificationsAPI.endpoints.listReceiver.useQuery<ListContactPointsHookResult>\n>[0];\n\ntype ListContactPointsQueryOptions = Parameters<\n typeof notificationsAPI.endpoints.listReceiver.useQuery<ListContactPointsHookResult>\n>[1];\n\n/**\n * useListContactPoints is a hook that fetches a list of contact points\n *\n * This function wraps the notificationsAPI.useListReceiverQuery with proper typing\n * to ensure that the returned ContactPoints are correctly typed in the data.items array.\n *\n * It automatically uses the configured namespace for the query.\n *\n * @param queryOptions - Optional query options that will be passed to the underlying useListReceiverQuery hook.\n * These options can include refetchOnFocus, refetchOnMountOrArgChange, skip, etc.\n */\nexport function useListContactPoints(\n queryArgs: ListContactPointsQueryArgs = {},\n queryOptions: ListContactPointsQueryOptions = {}\n) {\n return notificationsAPI.useListReceiverQuery<ListContactPointsHookResult>(queryArgs, queryOptions);\n}\n\n// type narrowing mutations requires us to define a few helper types\ntype CreateContactPointArgs = OverrideProperties<\n CreateReceiverApiArg,\n { receiver: Omit<ContactPoint, 'status' | 'metadata'> }\n>;\n\ntype CreateContactPointMutation = TypedUseMutationResult<\n ContactPoint,\n CreateContactPointArgs,\n ReturnType<typeof fetchBaseQuery>\n>;\n\ntype UseCreateContactPointOptions = Parameters<\n typeof notificationsAPI.endpoints.createReceiver.useMutation<CreateContactPointMutation>\n>[0];\n\n/**\n * useCreateContactPoint is a hook that creates a new contact point with one or more integrations\n *\n * This function wraps the notificationsAPI.useCreateReceiverMutation with proper typing\n * to ensure that the payload supports type narrowing.\n */\nexport function useCreateContactPoint(options?: UseCreateContactPointOptions) {\n const [updateFn, result] = notificationsAPI.endpoints.createReceiver.useMutation<CreateContactPointMutation>(options);\n\n const typedUpdateFn = (args: CreateContactPointArgs) => {\n // @ts-expect-error this one is just impossible for me to figure out\n const response = updateFn(args);\n return response;\n };\n\n return [typedUpdateFn, result] as const;\n}\n"],"names":["notificationsAPI"],"mappings":";;;AA0CO,SAAS,qBACd,SAAA,GAAwC,EAAC,EACzC,YAAA,GAA8C,EAAC,EAC/C;AACA,EAAA,OAAOA,cAAA,CAAiB,oBAAA,CAAkD,SAAA,EAAW,YAAY,CAAA;AACnG;AAwBO,SAAS,sBAAsB,OAAA,EAAwC;AAC5E,EAAA,MAAM,CAAC,UAAU,MAAM,CAAA,GAAIA,eAAiB,SAAA,CAAU,cAAA,CAAe,YAAwC,OAAO,CAAA;AAEpH,EAAA,MAAM,aAAA,GAAgB,CAAC,IAAA,KAAiC;AAEtD,IAAA,MAAM,QAAA,GAAW,SAAS,IAAI,CAAA;AAC9B,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO,CAAC,eAAe,MAAM,CAAA;AAC/B;;;;"}