{"version":3,"file":"useMessageAlsoSentInChannelNavigation.mjs","names":[],"sources":["../../../../../src/components/Message/hooks/useMessageAlsoSentInChannelNavigation.ts"],"sourcesContent":["import {\n  useChannel,\n  useChatContext,\n  useMessageContext,\n  useTranslationContext,\n  useWorkspaceNavigation,\n} from '../../../context';\nimport { useThreadContext } from '../../Threads';\n\nexport type MessageAlsoSentInChannelNavigation = {\n  /** True when rendered inside a thread, false in a channel message list. */\n  isInThread: boolean;\n  /** Whether the message is shown in the channel. The default indicator renders only then. */\n  isShownInChannel: boolean;\n  /** Jumps to the referenced message: in the channel from a thread, in its thread otherwise. */\n  viewReference: () => Promise<void>;\n  /** Jumps to the reply in the channel message list, opening the channel first if needed. */\n  viewReplyInChannel: (messageId?: string) => Promise<void>;\n  /** Opens the reply's parent thread and jumps to the reply. */\n  viewReplyInThread: (replyId?: string, parentId?: string) => Promise<void>;\n};\n\n/**\n * Navigation behind {@link MessageAlsoSentInChannelIndicator}, reusable in custom indicators.\n *\n * @returns The navigation handlers and the flags the indicator renders from.\n */\nexport const useMessageAlsoSentInChannelNavigation =\n  (): MessageAlsoSentInChannelNavigation => {\n    const { channelManager, client } = useChatContext();\n    const { t } = useTranslationContext();\n    const channel = useChannel();\n    const { isChannelActive, openChannel, openThread } = useWorkspaceNavigation();\n    const thread = useThreadContext();\n    const { message } = useMessageContext();\n\n    const addThreadNotFoundNotification = (error: Error) => {\n      client.notifications.addError({\n        message: t('notification.replySearchFailed', 'Thread has not been found'),\n        options: {\n          originalError: error,\n          type: 'api:message:search:not-found',\n        },\n        origin: {\n          context: { threadReply: message },\n          emitter: 'useMessageAlsoSentInChannelNavigation',\n        },\n      });\n    };\n\n    const viewReplyInChannel = async (messageId = message?.id) => {\n      if (!messageId) return;\n      // Open the channel first when it is not active in the workspace.\n      const needsNavigation = !isChannelActive(channel.cid);\n      if (needsNavigation) {\n        openChannel(channel);\n      }\n\n      await channel.messagePaginator.jumpToMessage(messageId);\n\n      if (needsNavigation) {\n        channelManager.ingestChannel(channel);\n      }\n    };\n\n    const viewReplyInThread = async (\n      replyId = message?.id,\n      parentId = message?.parent_id,\n    ) => {\n      if (!replyId || !parentId) return;\n      let targetThread = client.threads.get(parentId);\n\n      if (!targetThread) {\n        try {\n          const fetchedThread = await client.getThreadAndHydrate(\n            { message_id: parentId },\n            { watch: true },\n          );\n          // An instance registered while the request was in flight (e.g. by a thread list query)\n          // wins, so there is only one live instance per thread.\n          targetThread = client.threads.get(parentId) ?? fetchedThread;\n        } catch (error) {\n          addThreadNotFoundNotification(error as Error);\n          return;\n        }\n      }\n\n      openThread(targetThread);\n      await targetThread.messagePaginator.jumpToMessage(replyId);\n    };\n\n    const viewReference = async () => {\n      if (thread) {\n        await viewReplyInChannel(message?.id);\n        return;\n      }\n\n      if (!message?.parent_id) return;\n      await viewReplyInThread(message.id, message.parent_id);\n    };\n\n    return {\n      isInThread: !!thread,\n      isShownInChannel: !!message?.show_in_channel,\n      viewReference,\n      viewReplyInChannel,\n      viewReplyInThread,\n    };\n  };\n"],"mappings":";;;;;;;;;;;;AA2BA,IAAa,8CAC+B;CACxC,MAAM,EAAE,gBAAgB,WAAW,eAAe;CAClD,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,UAAU,WAAW;CAC3B,MAAM,EAAE,iBAAiB,aAAa,eAAe,uBAAuB;CAC5E,MAAM,SAAS,iBAAiB;CAChC,MAAM,EAAE,YAAY,kBAAkB;CAEtC,MAAM,iCAAiC,UAAiB;EACtD,OAAO,cAAc,SAAS;GAC5B,SAAS,EAAE,kCAAkC,2BAA2B;GACxE,SAAS;IACP,eAAe;IACf,MAAM;GACR;GACA,QAAQ;IACN,SAAS,EAAE,aAAa,QAAQ;IAChC,SAAS;GACX;EACF,CAAC;CACH;CAEA,MAAM,qBAAqB,OAAO,YAAY,SAAS,OAAO;EAC5D,IAAI,CAAC,WAAW;EAEhB,MAAM,kBAAkB,CAAC,gBAAgB,QAAQ,GAAG;EACpD,IAAI,iBACF,YAAY,OAAO;E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