import type React from 'react'; import { useState } from 'react'; import { Toast, useUIKit } from '@tencentcloud/uikit-base-component-react'; import cs from 'classnames'; import { ConversationType, useChatContext, useGroupStore, } from 'tuikit-atomicx-react/chat'; import { MiniSearch } from '../../SearchNew'; import styles from './ConversationSearch.module.scss'; import type { MiniSearchResult } from '../../SearchNew'; export interface ConversationSearchProps { visible?: boolean; className?: string; style?: React.CSSProperties; /** Called on every keyword change. */ onKeywordChange?: (keyword: string) => void; /** * Called when a result is clicked. When provided, overrides the default * "navigate to conversation" behavior. */ onResultItemClick?: (result: MiniSearchResult) => void; } export const ConversationSearch = ({ visible = true, className, style, onKeywordChange, onResultItemClick, }: ConversationSearchProps = {}) => { const { t } = useUIKit(); const { setActiveConversation, getConversationInfo } = useChatContext(); const { joinedGroupList, getGroupInfo, loadJoinedGroups } = useGroupStore(); const activateConversationByID = async (conversationID: string): Promise => { try { const info = await getConversationInfo(conversationID); if (info.type === ConversationType.Group) { const groupID = conversationID.replace(/^GROUP/, ''); try { const groupInfo = await getGroupInfo(groupID); if (!groupInfo?.groupID) { Toast.error({ message: t('TUIConversation.the_group_chat_has_been_disbanded') }); return; } await loadJoinedGroups(); const isInGroup = !!joinedGroupList.find(g => g.groupID === groupID); if (!isInGroup) { Toast.error({ message: t('TUIConversation.You are not in the group, please join the group first'), }); } } catch { Toast.error({ message: t('TUIConversation.the_group_chat_has_been_disbanded') }); return; } } setActiveConversation(info.conversationID); } catch (err) { Toast.error({ message: t('TUIConversation.conversation_not_found') }); console.error('[ConversationSearch.activateConversationByID] failed', err); } }; // Default click handling: user / group navigate to their conversation. // MESSAGE results require STANDARD mode (not implemented yet) → no-op. const handleResultItemClick = (result: MiniSearchResult) => { if (onResultItemClick) { onResultItemClick(result); return; } switch (result.type) { case 'user': activateConversationByID(`C2C${result.data.userID}`); break; case 'group': if (result.data.groupID) { activateConversationByID(`GROUP${result.data.groupID}`); } else { Toast.error({ message: t('TUIConversation.You are not in the group, please join the group first'), }); } break; case 'message': // TODO: open STANDARD mode once implemented. break; default: break; } }; const handleKeywordChange = (keyword: string) => { onKeywordChange?.(keyword); }; if (!visible) { return null; } return (
); }; export default ConversationSearch;