{"version":3,"file":"use-counters-helper.22e2509c.js","sources":["../../../../composables/use-clipboard-copy.ts","../../../../components/ShareModal.vue","../../../../stores/counters.ts","../../../../composables/use-counters-helper.ts"],"sourcesContent":["import { useI18n } from 'vue-i18n'\nimport { useNotifications } from '~/stores/notifications'\n\nexport const useClipboardCopy = () => {\n const { t } = useI18n()\n const { pushNotification } = useNotifications()\n\n const copyLink = async (link: string) => {\n try {\n await navigator.clipboard.writeText(link)\n pushNotification({\n title: t('notifications.share.copy_link_success'),\n })\n } catch (error) {\n pushNotification({\n title: t(`notifications.share.copy_link_error`),\n theme: 'destructive',\n })\n useLogError(new Error('error on clipboard.writeText'))\n }\n }\n\n return {\n copyLink,\n }\n}\n","\n\n\n","import { defineStore } from 'pinia'\nimport { EntityType } from '~/models/common'\nimport {\n CounterCommentsResponseType,\n CounterReactionsResponseType,\n CounterRequestType,\n ReactionType,\n} from '~/models/counters'\n\nexport const useCounters = defineStore('counters', () => {\n const baseURL = useRuntimeConfig().public.gatewayApi\n\n const getParams = (entity: CounterRequestType): Record => ({\n [`entities[0][entity_id]`]: entity.entity_id.toString(),\n [`entities[0][entity_type]`]: entity.entity_type,\n })\n\n const fetchCommentsCounters = async (\n entity: CounterRequestType\n ): Promise => {\n const queryParams = getParams(entity)\n\n const { data } = await useKrakenFetch<{\n data: CounterCommentsResponseType[]\n }>(`/comments/counter`, {\n baseURL,\n params: queryParams,\n })\n\n return data[0]\n }\n\n const fetchReactionsCounters = async (\n entity: CounterRequestType,\n isAuth: boolean\n ): Promise => {\n const queryParams = getParams(entity)\n\n if (isAuth) {\n const { data } = await useAuthFetch<{\n data: CounterReactionsResponseType[]\n }>('/reactions/me', {\n baseURL,\n params: queryParams,\n })\n\n return data\n } else {\n const { data } = await useKrakenFetch<{\n data: CounterReactionsResponseType[]\n }>('/reactions', {\n baseURL,\n params: queryParams,\n })\n\n return data\n }\n }\n\n const fetchAllCounters = async (\n entity: CounterRequestType,\n isAuth: boolean\n ) => {\n const [comments, reactions] = await Promise.all([\n fetchCommentsCounters(entity),\n fetchReactionsCounters(entity, isAuth),\n ])\n\n return {\n comments,\n reactions,\n }\n }\n\n const sendReaction = async (\n entityId: number,\n entityType: EntityType,\n reactionType: ReactionType\n ) => {\n await useAuthFetch<{\n data: CounterReactionsResponseType[]\n }>('/reactions', {\n baseURL,\n method: 'POST',\n body: {\n entity_id: entityId,\n entity_type: entityType,\n reaction: reactionType,\n },\n })\n }\n\n const deleteReaction = async (entityId: number, entityType: EntityType) => {\n await useAuthFetch<{\n data: CounterReactionsResponseType[]\n }>('/reactions', {\n baseURL,\n method: 'DELETE',\n body: {\n entity_id: entityId,\n entity_type: entityType,\n },\n })\n }\n\n return {\n fetchCommentsCounters,\n fetchReactionsCounters,\n fetchAllCounters,\n sendReaction,\n deleteReaction,\n }\n})\n","import { useI18n } from 'vue-i18n'\nimport { Ref } from 'vue'\nimport { useAuth } from '~/stores/auth'\nimport { useCounters } from '~/stores/counters'\nimport {\n CounterReactionsResponseType,\n ReactionType,\n CounterRequestType,\n ReactionsCounters,\n} from '~/models/counters'\nimport { useNotifications } from '~/stores/notifications'\n\nexport interface CountersCallback {\n onDeleteReaction?: () => Promise\n onSendReaction?: () => Promise\n}\n\nexport const useCountersHelper = () => {\n const {\n fetchAllCounters,\n fetchCommentsCounters,\n fetchReactionsCounters,\n sendReaction: sendReactionAction,\n deleteReaction: deleteReactionAction,\n } = useCounters()\n const { pushNotification } = useNotifications()\n const authStore = useAuth()\n const { t } = useI18n()\n const commentsCounter = ref(0)\n const reactionsCounters: Ref = ref({})\n const isObserverDone = ref(false)\n const isCountersProcessing = ref(false)\n const entity = ref(null)\n const observedTarget = ref>()\n const callbacks = ref(null)\n\n const getAllCounters = async () => {\n if (!entity.value) {\n return\n }\n\n isCountersProcessing.value = true\n\n try {\n const allCounters = await fetchAllCounters(entity.value, !!authStore.auth)\n\n if (allCounters.comments) {\n updateCommentsCounter(allCounters.comments.comments_count)\n }\n\n updateReactiosCounters(allCounters.reactions)\n } finally {\n isCountersProcessing.value = false\n }\n }\n\n const getCommentsCounters = async () => {\n if (!entity.value) {\n return\n }\n\n const counter = await fetchCommentsCounters(entity.value)\n updateCommentsCounter(counter.comments_count)\n }\n\n const updateCommentsCounter = (value: number) => {\n commentsCounter.value = value\n }\n\n const resetReactions = () => {\n reactionsCounters.value = {}\n }\n\n const updateReactiosCounters = (\n reactionList: CounterReactionsResponseType[]\n ) => {\n if (reactionList && reactionList.length) {\n reactionList.forEach((reactionItem) => {\n reactionsCounters.value[reactionItem.reaction] = reactionItem\n })\n }\n }\n\n const getReactionsCounters = async () => {\n if (!entity.value) {\n return\n }\n\n const counters = await fetchReactionsCounters(\n entity.value,\n !!authStore.auth\n )\n\n resetReactions()\n updateReactiosCounters(counters)\n }\n\n const sendReaction = async (reactionType: ReactionType) => {\n if (!entity.value) {\n return\n }\n\n if (!authStore.auth) {\n authStore.openLoginModal()\n return\n }\n\n try {\n await sendReactionAction(\n entity.value.entity_id,\n entity.value.entity_type,\n reactionType\n )\n await getReactionsCounters()\n\n if (callbacks.value && callbacks.value.onSendReaction) {\n await callbacks.value.onSendReaction()\n }\n } catch (error) {\n pushNotification({\n title: t('notifications.reactions.sent_error'),\n theme: 'destructive',\n })\n useLogError(error)\n }\n }\n\n const deleteReaction = async () => {\n if (!entity.value) {\n return\n }\n\n try {\n await deleteReactionAction(\n entity.value.entity_id,\n entity.value.entity_type\n )\n await getReactionsCounters()\n\n if (callbacks.value && callbacks.value.onDeleteReaction) {\n await callbacks.value.onDeleteReaction()\n }\n } catch (error) {\n pushNotification({\n title: t('notifications.reactions.delete_error'),\n theme: 'destructive',\n })\n useLogError(error)\n }\n }\n\n const onChangeReaction = async (reactionType: ReactionType) => {\n isCountersProcessing.value = true\n\n if (reactionsCounters.value[reactionType]?.is_set_by_user) {\n await deleteReaction()\n } else {\n await sendReaction(reactionType)\n }\n\n isCountersProcessing.value = false\n }\n\n const createObserver = (loadComments = true, loadReactions = true) => {\n const targetIsVisible = ref(false)\n\n // let timeout: number\n\n const { stop } = useIntersectionObserver(\n observedTarget.value,\n ([{ isIntersecting }], _observerElement) => {\n targetIsVisible.value = isIntersecting\n\n if (targetIsVisible.value) {\n // timeout =\n window.setTimeout(async () => {\n if (targetIsVisible.value) {\n if (loadReactions && loadComments) {\n await getAllCounters()\n } else if (loadReactions) {\n await getReactionsCounters()\n } else if (loadComments) {\n await getCommentsCounters()\n }\n stop()\n isObserverDone.value = true\n }\n }, 200)\n }\n },\n {\n rootMargin: '50px',\n }\n )\n\n // onUnmounted is temporaly commented as we getting vue warnings about no active instance could be bounded to unmounted hook\n // moreover if component will be unmounted so it should be not visible and callback should be stopped\n\n // onUnmounted(() => {\n // clearTimeout(timeout)\n // })\n }\n\n const initCountersHelper = async (\n entityValue: CounterRequestType,\n observedTargetValue?: Ref,\n countersCallbacks?: CountersCallback,\n isLoadComments = true,\n isLoadReactions = true\n ) => {\n entity.value = entityValue\n\n if (observedTargetValue) {\n observedTarget.value = observedTargetValue\n }\n\n if (countersCallbacks) {\n callbacks.value = countersCallbacks\n }\n\n if (!observedTargetValue) {\n if (isLoadComments && isLoadReactions) {\n await getAllCounters()\n } else if (isLoadComments) {\n await getCommentsCounters()\n } else if (isLoadReactions) {\n await getReactionsCounters()\n }\n } else {\n await createObserver(isLoadComments, isLoadReactions)\n }\n\n if (isLoadReactions) {\n watch(\n () => authStore.auth,\n () => {\n if (!observedTarget) {\n getReactionsCounters()\n } else if (isObserverDone.value) {\n createObserver(false)\n }\n }\n )\n }\n }\n\n return {\n initCountersHelper,\n reactionsCounters,\n commentsCounter,\n updateCommentsCounter,\n onChangeReaction,\n isCountersProcessing,\n }\n}\n"],"names":["useClipboardCopy","t","useI18n","pushNotification","useNotifications","link","useLogError","close","emit","copyLink","useCounters","defineStore","baseURL","useRuntimeConfig","getParams","entity","fetchCommentsCounters","queryParams","data","useKrakenFetch","fetchReactionsCounters","isAuth","useAuthFetch","comments","reactions","entityId","entityType","reactionType","_optionalChain","ops","lastAccessLHS","value","i","op","fn","args","useCountersHelper","fetchAllCounters","sendReactionAction","deleteReactionAction","authStore","useAuth","commentsCounter","ref","reactionsCounters","isObserverDone","isCountersProcessing","observedTarget","callbacks","getAllCounters","allCounters","updateCommentsCounter","updateReactiosCounters","getCommentsCounters","counter","resetReactions","reactionList","reactionItem","getReactionsCounters","counters","sendReaction","error","deleteReaction","onChangeReaction","_","_2","_3","createObserver","loadComments","loadReactions","targetIsVisible","stop","useIntersectionObserver","isIntersecting","_observerElement","entityValue","observedTargetValue","countersCallbacks","isLoadComments","isLoadReactions","watch"],"mappings":"gTAGO,MAAMA,GAAmB,IAAM,CAC9B,KAAA,CAAE,EAAAC,GAAMC,IACR,CAAE,iBAAAC,GAAqBC,IAiBtB,MAAA,CACL,SAhBe,MAAOC,GAAiB,CACnC,GAAA,CACI,MAAA,UAAU,UAAU,UAAUA,CAAI,EACvBF,EAAA,CACf,MAAOF,EAAE,uCAAuC,CAAA,CACjD,QAEgBE,EAAA,CACf,MAAOF,EAAE,qCAAqC,EAC9C,MAAO,aAAA,CACR,EACWK,EAAA,IAAI,MAAM,8BAA8B,CAAC,CACvD,CAAA,CAIA,CAEJ,yZC6BA,MAAMC,EAAQ,IAAM,CAClBC,EAAK,OAAO,CAAA,EAGR,CAAE,SAAAC,GAAaT,qnBCjDRU,GAAcC,EAAY,WAAY,IAAM,CACjD,MAAAC,EAAUC,IAAmB,OAAO,WAEpCC,EAAaC,IAAqD,CACtE,CAAC,wBAAwB,EAAGA,EAAO,UAAU,SAAS,EACtD,CAAC,0BAA0B,EAAGA,EAAO,WAAA,GAGjCC,EAAwB,MAC5BD,GACyC,CACnC,MAAAE,EAAcH,EAAUC,CAAM,EAE9B,CAAE,KAAAG,CAAS,EAAA,MAAMC,EAEpB,oBAAqB,CACtB,QAAAP,EACA,OAAQK,CAAA,CACT,EAED,OAAOC,EAAK,CAAC,CAAA,EAGTE,EAAyB,MAC7BL,EACAM,IAC4C,CACtC,MAAAJ,EAAcH,EAAUC,CA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