@droppii-org/chat-sdk
Version:
Droppii React Chat SDK
70 lines (69 loc) • 4.56 kB
JavaScript
"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { SessionType } from "@openim/wasm-client-sdk";
import { Avatar } from "antd";
import { formatTimestamp, highlightSearch, sanitizeHtml } from "../../../utils/common";
import { DChatSDK } from "../../../constants/sdk";
import { useChatContext } from "../../../context/ChatContext";
import { message as antdMessage } from "antd";
import { useTranslation } from "react-i18next";
import useConversationStore from "../../../store/conversation";
import { useMemo } from "react";
import useAuthStore from "../../../store/auth";
const SearchItemAsMessage = (props) => {
var _a;
const { t } = useTranslation();
const { message, searchTerm = "", onClick } = props;
const { user } = useChatContext();
const isCrm = useAuthStore((state) => state.isCrm);
const conversationData = useConversationStore((state) => state.conversationData);
const useConversationDisplay = useMemo(() => {
if (isCrm)
return true;
return (user === null || user === void 0 ? void 0 : user.userID) === (message === null || message === void 0 ? void 0 : message.sendID);
}, [conversationData === null || conversationData === void 0 ? void 0 : conversationData.ex, message === null || message === void 0 ? void 0 : message.sendID, isCrm]);
const onPressItem = async () => {
const { data } = await DChatSDK.getOneConversation({
sourceID: message.sessionType === SessionType.Group
? message.groupID
: (user === null || user === void 0 ? void 0 : user.userID) !== message.sendID
? message.sendID
: message.recvID,
sessionType: message.sessionType,
});
if (!data) {
return antdMessage.error(t("err_get_conversation"));
}
useConversationStore
.getState()
.setConversationData(data, message.clientMsgID);
useConversationStore
.getState()
.setSelectedConversationId(data.conversationID);
onClick === null || onClick === void 0 ? void 0 : onClick();
};
let msgContent = "";
try {
msgContent = ((_a = JSON.parse((message === null || message === void 0 ? void 0 : message.content) || "{}")) === null || _a === void 0 ? void 0 : _a.content) || "";
}
catch (error) {
console.error("Failed to parse message content", error);
if (typeof (message === null || message === void 0 ? void 0 : message.content) === "string") {
msgContent = message === null || message === void 0 ? void 0 : message.content;
}
}
return (_jsxs("div", { className: "py-3 px-2 flex items-center gap-3 hover:bg-gray-100 hover:rounded-sm cursor-pointer border-b mx-1", onClick: onPressItem, children: [_jsx("div", { children: _jsx(Avatar, { size: "large", src: useConversationDisplay
? message.senderFaceUrl
: conversationData === null || conversationData === void 0 ? void 0 : conversationData.faceURL, alt: useConversationDisplay
? message.senderNickname
: conversationData === null || conversationData === void 0 ? void 0 : conversationData.showName, children: useConversationDisplay
? message.senderNickname.charAt(0).toUpperCase()
: conversationData === null || conversationData === void 0 ? void 0 : conversationData.showName.charAt(0).toUpperCase() }) }), _jsxs("div", { className: "flex flex-col flex-1 min-w-0 gap-1", children: [_jsxs("div", { className: "flex flex-1 items-center justify-between", children: [_jsx("span", { className: "text-sm flex-1 font-semibold truncate", children: useConversationDisplay
? message.senderNickname
: conversationData === null || conversationData === void 0 ? void 0 : conversationData.showName }), _jsx("span", { className: "text-xs text-gray-500", children: formatTimestamp(message.sendTime, {
hasTime: true,
}) })] }), _jsx("div", { className: "flex flex-col flex-1 min-w-0", children: _jsx("span", { className: "text-xs flex-1 text-gray-500 truncate", dangerouslySetInnerHTML: {
__html: sanitizeHtml(highlightSearch(msgContent, searchTerm)),
} }) })] })] }, message.clientMsgID));
};
export default SearchItemAsMessage;