{"version":3,"file":"useFetchReactions.mjs","names":[],"sources":["../../../../../src/components/Reactions/hooks/useFetchReactions.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from 'react';\nimport type { ReactionResponse, SortParamRequest } from 'stream-chat';\nimport type { MessageContextValue } from '../../../context';\nimport { useMessageContext, useTranslationContext } from '../../../context';\nimport { useNotificationApi } from '../../Notifications';\n\nimport type { ReactionType } from '../types';\n\nexport interface FetchReactionsOptions {\n  reactionType: ReactionType | null;\n  shouldFetch: boolean;\n  handleFetchReactions?: MessageContextValue['handleFetchReactions'];\n  sort?: SortParamRequest[];\n}\n\nexport function useFetchReactions(options: FetchReactionsOptions) {\n  const { addNotification } = useNotificationApi();\n  const { handleFetchReactions: contextHandleFetchReactions } = useMessageContext();\n  const { t } = useTranslationContext();\n  const [reactions, setReactions] = useState<ReactionResponse[]>([]);\n  const {\n    handleFetchReactions: propHandleFetchReactions,\n    reactionType,\n    shouldFetch,\n    sort,\n  } = options;\n  const [isLoading, setIsLoading] = useState(shouldFetch);\n  const handleFetchReactions = propHandleFetchReactions ?? contextHandleFetchReactions;\n  const [refetchNonce, setRefetchNonce] = useState<number | null>(null);\n\n  useEffect(() => {\n    if (!shouldFetch) {\n      return;\n    }\n\n    let cancel = false;\n\n    (async () => {\n      try {\n        setIsLoading(true);\n        const reactions = await handleFetchReactions(reactionType ?? undefined, sort);\n\n        if (!cancel) {\n          setReactions(reactions);\n        }\n      } catch (e) {\n        if (!cancel) {\n          setReactions([]);\n          addNotification({\n            emitter: 'Reactions',\n            error: e instanceof Error ? e : undefined,\n            message: t(\n              'reactions.fetchReactions.errorFetchingReactions.text',\n              'Error loading reactions',\n            ),\n            severity: 'error',\n            type: 'api:message:reactions:fetch:failed',\n          });\n        }\n      } finally {\n        if (!cancel) {\n          setIsLoading(false);\n        }\n      }\n    })();\n\n    return () => {\n      cancel = true;\n    };\n  }, [\n    addNotification,\n    handleFetchReactions,\n    reactionType,\n    refetchNonce,\n    shouldFetch,\n    sort,\n    t,\n  ]);\n\n  const refetch = useCallback(() => {\n    setRefetchNonce(Math.random());\n  }, []);\n\n  return { isLoading, reactions, refetch };\n}\n"],"mappings":";;;;;AAeA,SAAgB,kBAAkB,SAAgC;CAChE,MAAM,EAAE,oBAAoB,mBAAmB;CAC/C,MAAM,EAAE,sBAAsB,gCAAgC,kBAAkB;CAChF,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,CAAC,WAAW,gBAAgB,SAA6B,CAAC,CAAC;CACjE,MAAM,EACJ,sBAAsB,0BACtB,cACA,aACA,SACE;CACJ,MAAM,CAAC,WAAW,gBAAgB,SAAS,WAAW;CACtD,MAAM,uBAAuB,4BAA4B;CACzD,MAAM,CAAC,cAAc,mBAAmB,SAAwB,IAAI;CAEpE,gBAAgB;EACd,IAAI,CAAC,aACH;EAGF,IAAI,SAAS;EAEb,CAAC,YAAY;GACX,IAAI;IACF,aAAa,IAAI;IACjB,MAAM,YAAY,MAAM,qBAAqB,gBAAgB,KAAA,GAAW,IAAI;IAE5E,IAAI,CAAC,QACH,aAAa,SAAS;GAE1B,SAAS,GAAG;IACV,IAAI,CAAC,QAAQ;KACX,aAAa,CAAC,CAAC;KACf,gBAAgB;MACd,SAAS;MACT,OAAO,aAAa,QAAQ,IAAI,KAAA;MAChC,SAAS,EACP,wDACA,yBACF;MACA,UAAU;MACV,MAAM;KACR,CAAC;IACH;GACF,UAAU;IACR,IAAI,CAAC,QACH,aAAa,KAAK;GAEtB;EACF,EAAA,CAAG;EAEH,aAAa;GACX,SAAS;EACX;CACF,GAAG;EACD;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAMD,OAAO;EAAE;EAAW;EAAW,SAJf,kBAAkB;GAChC,gBAAgB,KAAK,OAAO,CAAC;EAC/B,GAAG,CAAC,CAE2B;CAAQ;AACzC"}