{"version":3,"sources":["../../../src/hooks/useChannelConnections.ts"],"sourcesContent":["import type { ChannelConnectionResponse, ListChannelConnectionsArgs, NovuError } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseChannelConnectionsProps = ListChannelConnectionsArgs & {\n  onSuccess?: (data: ChannelConnectionResponse[]) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseChannelConnectionsResult = {\n  connections: ChannelConnectionResponse[];\n  error?: NovuError;\n  isLoading: boolean;\n  isFetching: boolean;\n  refetch: () => Promise<void>;\n};\n\n/**\n * List channel connections for the current subscriber. Use this to discover an active\n * connection identifier after a chat OAuth flow so it can be passed to `LinkUser`.\n *\n * @example\n * const { connections } = useChannelConnections({ integrationIdentifier: 'my-chat-integration' });\n * const connectionIdentifier = connections[0]?.identifier;\n */\nexport const useChannelConnections = (props: UseChannelConnectionsProps = {}): UseChannelConnectionsResult => {\n  const novu = useNovu();\n  const propsRef = useRef<UseChannelConnectionsProps>(props);\n  propsRef.current = props;\n\n  const [connections, setConnections] = useState<ChannelConnectionResponse[]>([]);\n  const [error, setError] = useState<NovuError>();\n  const [isLoading, setIsLoading] = useState(true);\n  const [isFetching, setIsFetching] = useState(false);\n\n  const fetchConnections = 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.channelConnections.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          setConnections(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 cleanupDeleteResolved = novu.on('channel-connection.delete.resolved', () => {\n      void fetchConnections({ refetch: true });\n    });\n\n    void fetchConnections({ refetch: true });\n\n    return () => {\n      cleanupDeleteResolved();\n    };\n  }, [novu, fetchConnections]);\n\n  const refetch = useCallback(() => fetchConnections({ refetch: true }), [fetchConnections]);\n\n  return {\n    connections,\n    error,\n    isLoading,\n    isFetching,\n    refetch,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAyD;AACzD,0BAAwB;AAuBjB,IAAM,wBAAwB,CAAC,QAAoC,CAAC,MAAmC;AAC5G,QAAM,WAAO,6BAAQ;AACrB,QAAM,eAAW,qBAAmC,KAAK;AACzD,WAAS,UAAU;AAEnB,QAAM,CAAC,aAAa,cAAc,QAAI,uBAAsC,CAAC,CAAC;AAC9E,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,uBAAmB;AAAA,IACvB,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,mBAAmB,KAAK,QAAQ;AAE5D,YAAI,SAAS,OAAO;AAClB,mBAAS,SAAS,KAAkB;AACpC,6CAAU,SAAS;AAAA,QACrB,WAAW,SAAS,SAAS,QAAW;AACtC,yBAAe,SAAS,IAAI;AAC5B,iDAAY,SAAS;AAAA,QACvB;AAAA,MACF,SAAS,KAAK;AACZ,iBAAS,GAAgB;AACzB,2CAAU;AAAA,MACZ,UAAE;AACA,qBAAa,KAAK;AAClB,sBAAc,KAAK;AAAA,MACrB;AAAA,IACF;AAAA,IACA,CAAC,IAAI;AAAA,EACP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