{"version":3,"file":"usePinHandler.mjs","names":[],"sources":["../../../../../src/components/Message/hooks/usePinHandler.ts"],"sourcesContent":["import { validateAndGetMessage } from '../utils';\n\nimport { useChatContext } from '../../../context';\nimport { useMessagePaginator } from '../../../hooks';\nimport { useTranslationContext } from '../../../context/TranslationContext';\n\nimport type { LocalMessage, UserResponse } from 'stream-chat';\nimport type { ReactEventHandler } from '../types';\nimport { nowNs } from 'stream-chat';\n\nexport type PinMessageNotifications = {\n  getErrorNotification?: (message: LocalMessage) => string;\n  notify?: (notificationText: string, type: 'success' | 'error') => void;\n};\n\nexport const usePinHandler = (\n  message: LocalMessage,\n  notifications: PinMessageNotifications = {},\n) => {\n  const { getErrorNotification, notify } = notifications;\n\n  const messagePaginator = useMessagePaginator();\n  const { client } = useChatContext();\n  const { t } = useTranslationContext();\n\n  // The stored message, which may be newer than the `message` this hook rendered with.\n  const getLatestMessage = () => messagePaginator.getItem(message.id) ?? message;\n\n  // Restores only the pin fields from before the click, so updates that arrived while the request\n  // was in flight (an edit, a reaction) are kept. Skipped unless the stored pin is still the\n  // optimistic one: a pin or unpin that arrived meanwhile is newer than the snapshot, even when\n  // `pinned` has the same value.\n  const rollBackPin = (optimisticMessage: LocalMessage) => {\n    const latestMessage = messagePaginator.getItem(message.id);\n    const isOptimisticPinStored =\n      latestMessage?.pinned === optimisticMessage.pinned &&\n      latestMessage.pinned_at === optimisticMessage.pinned_at &&\n      latestMessage.pinned_by?.id === optimisticMessage.pinned_by?.id;\n    if (!latestMessage || !isOptimisticPinStored) return;\n\n    messagePaginator.ingestItem({\n      ...latestMessage,\n      pin_expires: message.pin_expires,\n      pinned: message.pinned,\n      pinned_at: message.pinned_at,\n      pinned_by: message.pinned_by,\n    });\n  };\n\n  const handlePin: ReactEventHandler = async (event) => {\n    event.preventDefault();\n\n    if (!message) {\n      throw new Error('usePinHandler was called but it is missing the message.');\n    }\n\n    if (!message.pinned) {\n      const optimisticMessage: LocalMessage = {\n        ...getLatestMessage(),\n        pinned: true,\n        pinned_at: nowNs(),\n        pinned_by: client.user as UserResponse | undefined,\n      };\n\n      try {\n        messagePaginator.ingestItem(optimisticMessage);\n\n        await client.pinMessage(message);\n      } catch (e) {\n        // Roll back first, so a throwing notification callback cannot leave the optimistic state.\n        rollBackPin(optimisticMessage);\n\n        const errorMessage =\n          getErrorNotification && validateAndGetMessage(getErrorNotification, [message]);\n\n        if (notify)\n          notify(\n            errorMessage ||\n              t('common.errorPinningMessage.label', 'Error pinning message'),\n            'error',\n          );\n        throw e;\n      }\n    } else {\n      const optimisticMessage: LocalMessage = {\n        ...getLatestMessage(),\n        pin_expires: undefined,\n        pinned: false,\n        pinned_at: undefined,\n        pinned_by: undefined,\n      };\n\n      try {\n        messagePaginator.ingestItem(optimisticMessage);\n\n        await client.unpinMessage(message);\n      } catch (e) {\n        // Roll back first, so a throwing notification callback cannot leave the optimistic state.\n        rollBackPin(optimisticMessage);\n\n        const errorMessage =\n          getErrorNotification && validateAndGetMessage(getErrorNotification, [message]);\n\n        if (notify)\n          notify(\n            errorMessage ||\n              t('common.errorRemovingMessagePin.label', 'Error removing message pin'),\n            'error',\n          );\n        throw e;\n      }\n    }\n  };\n\n  return { handlePin };\n};\n"],"mappings":";;;;;;AAeA,IAAa,iBACX,SACA,gBAAyC,CAAC,MACvC;CACH,MAAM,EAAE,sBAAsB,WAAW;CAEzC,MAAM,mBAAmB,oBAAoB;CAC7C,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,EAAE,MAAM,sBAAsB;CAGpC,MAAM,yBAAyB,iBAAiB,QAAQ,QAAQ,EAAE,KAAK;CAMvE,MAAM,eAAe,sBAAoC;EACvD,MAAM,gBAAgB,iBAAiB,QAAQ,QAAQ,EAAE;EACzD,MAAM,wBACJ,eAAe,WAAW,kBAAkB,UAC5C,cAAc,cAAc,kBAAkB,aAC9C,cAAc,WAAW,OAAO,kBAAkB,WAAW;EAC/D,IAAI,CAAC,iBAAiB,CAAC,uBAAuB;EAE9C,iBAAiB,WAAW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