{"version":3,"file":"useManagePollVotesRealtime.mjs","names":[],"sources":["../../../../../src/components/Poll/hooks/useManagePollVotesRealtime.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport { isVoteAnswer } from 'stream-chat';\nimport { useChatContext } from '../../../context';\nimport type { EventPayload, PollVoteResponseData } from 'stream-chat';\n\nimport type { CursorPaginatorStateStore } from '../../InfiniteScrollPaginator/hooks/useCursorPaginator';\n\nexport function useManagePollVotesRealtime<\n  T extends PollVoteResponseData = PollVoteResponseData,\n>(\n  managedVoteType: 'answer' | 'vote',\n  cursorPaginatorState?: CursorPaginatorStateStore<T>,\n  optionId?: string,\n) {\n  const { client } = useChatContext();\n  const [votesInRealtime, setVotesInRealtime] = useState<T[]>(\n    cursorPaginatorState?.getLatestValue().items ?? [],\n  );\n\n  useEffect(\n    () =>\n      cursorPaginatorState?.subscribeWithSelector(\n        (state) => [state.latestPageItems],\n        ([latestPageItems]) =>\n          setVotesInRealtime((prev) => [...prev, ...latestPageItems]),\n      ),\n    [cursorPaginatorState],\n  );\n\n  useEffect(() => {\n    const handleVoteEvent = (\n      event: EventPayload<'poll.vote_casted' | 'poll.vote_changed' | 'poll.vote_removed'>,\n    ) => {\n      if (!event.poll_vote) return;\n      const isAnswer = isVoteAnswer(event.poll_vote);\n      if (\n        (managedVoteType === 'answer' && !isAnswer) ||\n        (managedVoteType === 'vote' &&\n          (isAnswer || event.poll_vote.option_id !== optionId))\n      )\n        return;\n\n      if (event.type === 'poll.vote_removed') {\n        setVotesInRealtime((prev) =>\n          event.poll_vote\n            ? prev.filter((vote) => vote.id !== (event.poll_vote as T).id)\n            : prev,\n        );\n      }\n      if (event.type === 'poll.vote_changed') {\n        setVotesInRealtime((prev) =>\n          event.poll_vote\n            ? prev.filter((vote) => vote.id !== (event.poll_vote as T).id)\n            : prev,\n        );\n      }\n      if (['poll.vote_casted', 'poll.vote_changed'].includes(event.type)) {\n        setVotesInRealtime((prev) =>\n          event.poll_vote ? [event.poll_vote as T, ...prev] : prev,\n        );\n      }\n    };\n\n    const voteCastedSubscription = client.on('poll.vote_casted', handleVoteEvent);\n    const voteRemovedSubscription = client.on('poll.vote_removed', handleVoteEvent);\n    const voteChangedSubscription = client.on('poll.vote_changed', handleVoteEvent);\n\n    return () => {\n      voteCastedSubscription.unsubscribe();\n      voteRemovedSubscription.unsubscribe();\n      voteChangedSubscription.unsubscribe();\n    };\n  }, [client, optionId, managedVoteType]);\n\n  return votesInRealtime;\n}\n"],"mappings":";;;;AAOA,SAAgB,2BAGd,iBACA,sBACA,UACA;CACA,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,CAAC,iBAAiB,sBAAsB,SAC5C,sBAAsB,eAAe,CAAC,CAAC,SAAS,CAAC,CACnD;CAEA,gBAEI,sBAAsB,uBACnB,UAAU,CAAC,MAAM,eAAe,IAChC,CAAC,qBACA,oBAAoB,SAAS,CAAC,GAAG,MAAM,GAAG,eAAe,CAAC,CAC9D,GACF,CAAC,oBAAoB,CACvB;CAEA,gBAAgB;EACd,MAAM,mBACJ,UACG;GACH,IAAI,CAAC,MAAM,WAAW;GACtB,MAAM,WAAW,aAAa,MAAM,SAAS;GAC7C,IACG,oBAAoB,YAAY,CAAC,YACjC,oBAAoB,WAClB,YAAY,MAAM,UAAU,cAAc,WAE7C;GAEF,IAAI,MAAM,SAAS,qBACjB,oBAAoB,SAClB,MAAM,YACF,KAAK,QAAQ,SAAS,KAAK,OAAQ,MAAM,UAAgB,EAAE,IAC3D,IACN;GAEF,IAAI,MAAM,SAAS,qBACjB,oBAAoB,SAClB,MAAM,YACF,KAAK,QAAQ,SAAS,KAAK,OAAQ,MAAM,UAAgB,EAAE,IAC3D,IACN;GAEF,IAAI,CAAC,oBAAoB,mBAAmB,CAAC,CAAC,SAAS,MAAM,IAAI,GAC/D,oBAAoB,SAClB,MAAM,YAAY,CAAC,MAAM,WAAgB,GAAG,IAAI,IAAI,IACtD;EAEJ;EAEA,MAAM,yBAAyB,OAAO,GAAG,oBAAoB,eAAe;EAC5E,MAAM,0BAA0B,OAAO,GAAG,qBAAqB,eAAe;EAC9E,MAAM,0BAA0B,OAAO,GAAG,qBAAqB,eAAe;EAE9E,aAAa;GACX,uBAAuB,YAAY;GACnC,wBAAwB,YAAY;GACpC,wBAAwB,YAAY;EACtC;CACF,GAAG;EAAC;EAAQ;EAAU;CAAe,CAAC;CAEtC,OAAO;AACT"}