|
| 1 | +import { |
| 2 | + useChannel, |
| 3 | + useChatContext, |
| 4 | + useMessageContext, |
| 5 | + useTranslationContext, |
| 6 | +} from '../../../context'; |
| 7 | +import { useStateStore } from '../../../store'; |
| 8 | +import { useChatViewContext, useChatViewNavigation } from '../../ChatView'; |
| 9 | +import { useThreadContext } from '../../Threads'; |
| 10 | + |
| 11 | +const activeViewSelector = ({ activeView }: { activeView: string }) => ({ activeView }); |
| 12 | + |
| 13 | +export type MessageAlsoSentInChannelNavigation = { |
| 14 | + /** True when rendered inside a thread (the reply was "also sent in channel"); false in a |
| 15 | + * channel message list (the message is a thread reply, "replied to a thread"). */ |
| 16 | + isInThread: boolean; |
| 17 | + /** Whether the current message is shown in the channel (i.e. the indicator should render). */ |
| 18 | + isShownInChannel: boolean; |
| 19 | + /** Composed handler used by the default indicator: jumps to the referenced message, picking the |
| 20 | + * channel or the thread depending on where the indicator is rendered. */ |
| 21 | + viewReference: () => Promise<void>; |
| 22 | + /** Jump to the reply in the channel message list (used from inside a thread). */ |
| 23 | + viewReplyInChannel: (messageId?: string) => Promise<void>; |
| 24 | + /** Open the reply's parent thread and jump to the reply (used from a channel message list). */ |
| 25 | + viewReplyInThread: (replyId?: string, parentId?: string) => Promise<void>; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Encapsulates the navigation behind {@link MessageAlsoSentInChannelIndicator} so integrators can |
| 30 | + * reuse it (or compose extra behavior around it) without re-implementing the component. Returns the |
| 31 | + * composed `viewReference` handler plus the granular `viewReplyInChannel` / `viewReplyInThread` |
| 32 | + * actions and the `isInThread` / `isShownInChannel` flags used for rendering. |
| 33 | + */ |
| 34 | +export const useMessageAlsoSentInChannelNavigation = |
| 35 | + (): MessageAlsoSentInChannelNavigation => { |
| 36 | + const { channelPaginatorsOrchestrator, client } = useChatContext(); |
| 37 | + const { t } = useTranslationContext(); |
| 38 | + const channel = useChannel(); |
| 39 | + const { open } = useChatViewNavigation(); |
| 40 | + const { layoutController } = useChatViewContext(); |
| 41 | + const thread = useThreadContext(); |
| 42 | + const { message } = useMessageContext('useMessageAlsoSentInChannelNavigation'); |
| 43 | + const { activeView } = useStateStore(layoutController.state, activeViewSelector) ?? { |
| 44 | + activeView: layoutController.state.getLatestValue().activeView, |
| 45 | + }; |
| 46 | + |
| 47 | + const addThreadNotFoundNotification = (error: Error) => { |
| 48 | + client.notifications.addError({ |
| 49 | + message: t('Thread has not been found'), |
| 50 | + options: { |
| 51 | + originalError: error, |
| 52 | + type: 'api:message:search:not-found', |
| 53 | + }, |
| 54 | + origin: { |
| 55 | + context: { threadReply: message }, |
| 56 | + emitter: 'useMessageAlsoSentInChannelNavigation', |
| 57 | + }, |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + const viewReplyInChannel = async (messageId = message?.id) => { |
| 62 | + if (!messageId) return; |
| 63 | + if (activeView === 'threads') { |
| 64 | + // Switch to the channels view and bind this specific channel. Backed by the |
| 65 | + // ChannelPaginatorsOrchestrator, `ingestChannel` surfaces it in the channel list(s) |
| 66 | + // incrementally (deduped, in sort order) instead of forcing a full list re-query. |
| 67 | + open({ key: channel.cid ?? undefined, kind: 'channel', source: channel }); |
| 68 | + // Only initialize the channel if it hasn't been loaded yet — a channel already held by the |
| 69 | + // orchestrator / active session is initialized, so we skip the redundant query. |
| 70 | + if (!channel.initialized) { |
| 71 | + await channel.query({ messages: { limit: 0 } }); |
| 72 | + } |
| 73 | + // Surface the (now initialized) channel in the list(s) so its data is available for filter |
| 74 | + // matching; `ingestChannel` dedupes by cid and inserts in sort order — safe to call always. |
| 75 | + channelPaginatorsOrchestrator.ingestChannel(channel); |
| 76 | + } |
| 77 | + |
| 78 | + await channel.messagePaginator.jumpToMessage(messageId); |
| 79 | + }; |
| 80 | + |
| 81 | + const viewReplyInThread = async ( |
| 82 | + replyId = message?.id, |
| 83 | + parentId = message?.parent_id, |
| 84 | + ) => { |
| 85 | + if (!replyId || !parentId) return; |
| 86 | + let targetThread = client.threads.threadsById[parentId]; |
| 87 | + |
| 88 | + if (!targetThread) { |
| 89 | + try { |
| 90 | + targetThread = await client.getThread(parentId, { watch: true }); |
| 91 | + } catch (error) { |
| 92 | + addThreadNotFoundNotification(error as Error); |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + open({ key: targetThread.id ?? undefined, kind: 'thread', source: targetThread }); |
| 98 | + await targetThread.messagePaginator.jumpToMessage(replyId); |
| 99 | + }; |
| 100 | + |
| 101 | + const viewReference = async () => { |
| 102 | + if (thread) { |
| 103 | + await viewReplyInChannel(message?.id); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (!message?.parent_id) return; |
| 108 | + await viewReplyInThread(message.id, message.parent_id); |
| 109 | + }; |
| 110 | + |
| 111 | + return { |
| 112 | + isInThread: !!thread, |
| 113 | + isShownInChannel: !!message?.show_in_channel, |
| 114 | + viewReference, |
| 115 | + viewReplyInChannel, |
| 116 | + viewReplyInThread, |
| 117 | + }; |
| 118 | + }; |
0 commit comments