{"version":3,"sources":["../../../src/hooks/useChannelEndpoints.ts"],"sourcesContent":["import type { ChannelEndpointResponse, ListChannelEndpointsArgs, NovuError } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseChannelEndpointsProps = ListChannelEndpointsArgs & {\n  onSuccess?: (data: ChannelEndpointResponse[]) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseChannelEndpointsResult = {\n  endpoints: ChannelEndpointResponse[];\n  error?: NovuError;\n  isLoading: boolean;\n  isFetching: boolean;\n  refetch: () => Promise<void>;\n};\n\n/**\n * List channel endpoints for the current subscriber. Use this to discover a linked\n * user endpoint (e.g. a `slack_user` endpoint) for a given connection identifier.\n *\n * @example\n * const { endpoints } = useChannelEndpoints({ integrationIdentifier: 'my-chat', connectionIdentifier });\n * const isLinked = endpoints.some((endpoint) => endpoint.type === 'slack_user');\n */\nexport const useChannelEndpoints = (props: UseChannelEndpointsProps = {}): UseChannelEndpointsResult => {\n  const novu = useNovu();\n  const propsRef = useRef<UseChannelEndpointsProps>(props);\n  propsRef.current = props;\n\n  const [endpoints, setEndpoints] = useState<ChannelEndpointResponse[]>([]);\n  const [error, setError] = useState<NovuError>();\n  const [isLoading, setIsLoading] = useState(true);\n  const [isFetching, setIsFetching] = useState(false);\n\n  const fetchEndpoints = useCallback(\n    async (options?: { refetch: boolean }) => {\n      const { onSuccess, onError, ...listArgs } = propsRef.current;\n\n      if (options?.refetch) {\n        setError(undefined);\n        setIsLoading(true);\n      }\n\n      setIsFetching(true);\n\n      try {\n        const response = await novu.channelEndpoints.list(listArgs);\n\n        if (response.error) {\n          setError(response.error as NovuError);\n          onError?.(response.error as NovuError);\n        } else if (response.data !== undefined) {\n          setEndpoints(response.data);\n          onSuccess?.(response.data);\n        }\n      } catch (err) {\n        setError(err as NovuError);\n        onError?.(err as NovuError);\n      } finally {\n        setIsLoading(false);\n        setIsFetching(false);\n      }\n    },\n    [novu]\n  );\n\n  useEffect(() => {\n    const cleanups = (\n      [\n        'channel-endpoint.create.resolved',\n        'channel-endpoint.delete.resolved',\n        'channel-endpoint.link.resolved',\n      ] as const\n    ).map((event) =>\n      novu.on(event, () => {\n        void fetchEndpoints({ refetch: true });\n      })\n    );\n\n    void fetchEndpoints({ refetch: true });\n\n    return () => {\n      for (const cleanup of cleanups) {\n        cleanup();\n      }\n    };\n  }, [novu, fetchEndpoints]);\n\n  const refetch = useCallback(() => fetchEndpoints({ refetch: true }), [fetchEndpoints]);\n\n  return {\n    endpoints,\n    error,\n    isLoading,\n    isFetching,\n    refetch,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAyD;AACzD,0BAAwB;AAuBjB,IAAM,sBAAsB,CAAC,QAAkC,CAAC,MAAiC;AACtG,QAAM,WAAO,6BAAQ;AACrB,QAAM,eAAW,qBAAiC,KAAK;AACvD,WAAS,UAAU;AAEnB,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAoC,CAAC,CAAC;AACxE,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAoB;AAC9C,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,IAAI;AAC/C,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,KAAK;AAElD,QAAM,qBAAiB;AAAA,IACrB,OAAO,YAAmC;AACxC,YAAM,EAAE,WAAW,SAAS,GAAG,SAAS,IAAI,SAAS;AAErD,UAAI,mCAAS,SAAS;AACpB,iBAAS,MAAS;AAClB,qBAAa,IAAI;AAAA,MACnB;AAEA,oBAAc,IAAI;AAElB,UAAI;AACF,cAAM,WAAW,MAAM,KAAK,iBAAiB,KAAK,QAAQ;AAE1D,YAAI,SAAS,OAAO;AAClB,mBAAS,SAAS,KAAkB;AACpC,6CAAU,SAAS;AAAA,QACrB,WAAW,SAAS,SAAS,QAAW;AACtC,uBAAa,SAAS,IAAI;AAC1B,iDAAY,SAAS;AAAA,QACvB;AAAA,MAC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