{"version":3,"sources":["../../../src/hooks/useChannelConnection.ts"],"sourcesContent":["import type { ChannelConnectionResponse, NovuError } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseChannelConnectionProps = {\n  identifier: string;\n  onSuccess?: (data: ChannelConnectionResponse | null) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseChannelConnectionResult = {\n  connection?: ChannelConnectionResponse | null;\n  error?: NovuError;\n  isLoading: boolean;\n  isFetching: boolean;\n  refetch: () => Promise<void>;\n};\n\n/**\n * Get a channel connection by identifier.\n */\nexport const useChannelConnection = (props: UseChannelConnectionProps): UseChannelConnectionResult => {\n  const novu = useNovu();\n  const propsRef = useRef<UseChannelConnectionProps>(props);\n  propsRef.current = props;\n\n  const [connection, setConnection] = useState<ChannelConnectionResponse | null>();\n  const [error, setError] = useState<NovuError>();\n  const [isLoading, setIsLoading] = useState(true);\n  const [isFetching, setIsFetching] = useState(false);\n\n  const fetchConnection = useCallback(\n    async (options?: { refetch: boolean }) => {\n      const { identifier, onSuccess, onError } = propsRef.current;\n      if (options?.refetch) {\n        setError(undefined);\n        setIsLoading(true);\n      }\n\n      setIsFetching(true);\n\n      const response = await novu.channelConnections.get({ identifier });\n\n      if (response.error) {\n        setError(response.error as NovuError);\n        onError?.(response.error as NovuError);\n      } else if (response.data !== undefined) {\n        onSuccess?.(response.data);\n        setConnection(response.data);\n      }\n      setIsLoading(false);\n      setIsFetching(false);\n    },\n    [novu]\n  );\n\n  // props.identifier triggers a re-fetch and re-registration whenever the\n  // caller switches to a different identifier; fetchConnection reads it from\n  // propsRef at call-time so it doesn't appear in the callback body directly.\n  // biome-ignore lint/correctness/useExhaustiveDependencies: props.identifier is an intentional trigger dependency\n  useEffect(() => {\n    const cleanupGetPending = novu.on('channel-connection.get.pending', ({ args }) => {\n      if (!args || args.identifier !== propsRef.current.identifier) {\n        return;\n      }\n      setIsFetching(true);\n    });\n\n    const cleanupGetResolved = novu.on('channel-connection.get.resolved', ({ args, data, error: resolvedError }) => {\n      if (!args || args.identifier !== propsRef.current.identifier) {\n        return;\n      }\n      const { onSuccess, onError } = propsRef.current;\n      if (resolvedError) {\n        setError(resolvedError as NovuError);\n        onError?.(resolvedError as NovuError);\n      } else {\n        setConnection((data as ChannelConnectionResponse) ?? null);\n        onSuccess?.((data as ChannelConnectionResponse) ?? null);\n      }\n      setIsFetching(false);\n    });\n\n    const cleanupDeleteResolved = novu.on('channel-connection.delete.resolved', ({ args }) => {\n      if (!args || args.identifier !== propsRef.current.identifier) {\n        return;\n      }\n      setConnection(null);\n      propsRef.current.onSuccess?.(null);\n      setIsFetching(false);\n    });\n\n    void fetchConnection({ refetch: true });\n\n    return () => {\n      cleanupGetPending();\n      cleanupGetResolved();\n      cleanupDeleteResolved();\n    };\n  }, [novu, fetchConnection, props.identifier]);\n\n  const refetch = useCallback(() => fetchConnection({ refetch: true }), [fetchConnection]);\n\n  return {\n    connection,\n    error,\n    isLoading,\n    isFetching,\n    refetch,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAyD;AACzD,0BAAwB;AAmBjB,IAAM,uBAAuB,CAAC,UAAiE;AACpG,QAAM,WAAO,6BAAQ;AACrB,QAAM,eAAW,qBAAkC,KAAK;AACxD,WAAS,UAAU;AAEnB,QAAM,CAAC,YAAY,aAAa,QAAI,uBAA2C;AAC/E,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,sBAAkB;AAAA,IACtB,OAAO,YAAmC;AACxC,YAAM,EAAE,YAAY,WAAW,QAAQ,IAAI,SAAS;AACpD,UAAI,mCAAS,SAAS;AACpB,iBAAS,MAAS;AAClB,qBAAa,IAAI;AAAA,MACnB;AAEA,oBAAc,IAAI;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