UNPKG

@uimkit/uikit-react

Version:

<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>

148 lines (145 loc) 7.54 kB
import { __rest, __assign, __awaiter, __generator } from 'tslib'; import React__default, { useState, useCallback, useEffect, useMemo } from 'react'; import { Provider, useSelector } from 'react-redux'; import { UIChat } from '../UIChat/UIChat.js'; import { createAppStore } from '../../store/index.js'; import 'redux'; import '../../types/models.js'; import { EventType } from '../../types/events.js'; import '../../store/rootReducer.js'; import '../../store/accounts/actions.js'; import '../../store/accounts/selectors.js'; import '../../node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/invariant.js'; import 'i18next'; import { ConversationListActionType } from '../../store/conversations/actions.js'; import '../../node_modules/.pnpm/lodash.flatten@4.4.0/node_modules/lodash.flatten/index.js'; import '../../node_modules/.pnpm/lodash.some@4.6.0/node_modules/lodash.some/index.js'; import { updateConversation } from '../../store/conversations/commands.js'; import { ConversationActionType } from '../../store/messages/actions.js'; import '../../store/contacts/actions.js'; import { useDispatch } from '../../store/useDispatch.js'; import { TranslationProvider } from '../../context/TranslationContext.js'; import { UIKitProvider } from '../../context/UIKitContext.js'; import '../../context/ComponentContext.js'; import '../../context/ChatActionContext.js'; import '../../context/MessageInputContext.js'; import '../../context/UIMessageContext.js'; import '../../context/ChatStateContext.js'; import { UIConversationList } from '../UIConversationList/UIConversationList.js'; import { Toast } from '../Toast/index.js'; import { useTranslators } from './hooks/useTranslators.js'; import '../../i18n/uimi18n.js'; import { initAPIClient } from '../../store/common/actions.js'; var appThunkContext = { // 全局的错误处理 onError: function (e, title) { Toast({ text: e.message, type: 'error' }); } }; var store = createAppStore(appThunkContext); var UIKitInner = function (props) { var client = props.client, activeProfile = props.activeProfile, propActiveConversation = props.activeConversation, defaultLanguage = props.defaultLanguage, i18nInstance = props.i18nInstance, children = props.children; var dispatch = useDispatch(); var clientInited = useSelector(function (state) { return state.common.client; }); dispatch(initAPIClient(client)); var _a = useState(), activeConversation = _a[0], _setActiveConversation = _a[1]; var setActiveConversation = useCallback(function (activeConversation) { if (activeConversation) { if (activeConversation.unread > 0) { client === null || client === void 0 ? void 0 : client.setConversationRead(activeConversation.id); } dispatch(updateConversation({ account: activeConversation.account, id: activeConversation.id, unread: 0, })); } _setActiveConversation(activeConversation); }, [client]); useEffect(function () { if (clientInited) { setActiveConversation(propActiveConversation); } }, [propActiveConversation, clientInited]); useEffect(function () { var _a, _b, _c; var onMessageReceived = function (e) { dispatch({ type: ConversationActionType.MESSAGE_RECEIVED, payload: e.data, }); }; var onMessageUpdated = function (e) { dispatch({ type: ConversationActionType.MESSAGE_UPDATE, payload: e.data, }); }; var onConversationUpdated = function (e) { dispatch({ type: ConversationListActionType.CONVERSATION_RECEIVED, payload: e.data, }); }; (_a = client === null || client === void 0 ? void 0 : client.on) === null || _a === void 0 ? void 0 : _a.call(client, EventType.MESSAGE_RECEIVED, onMessageReceived); // 收到新消息 (_b = client === null || client === void 0 ? void 0 : client.on) === null || _b === void 0 ? void 0 : _b.call(client, EventType.MESSAGE_UPDATED, onMessageUpdated); // 更新消息 (_c = client === null || client === void 0 ? void 0 : client.on) === null || _c === void 0 ? void 0 : _c.call(client, EventType.CONVERSATION_UPDATED, onConversationUpdated); // 会话更新 或 新会话 return function () { // client?.off(EventType.MESSAGE_RECEIVED, onMessageReceived); // client?.off(EventType.MESSAGE_UPDATED, onMessageUpdated); // client?.off(EventType.CONVERSATION_CREATED, onConversationNew); }; }, [client, dispatch]); useEffect(function () { var _a; var onConversationUpdated = function (e) { return __awaiter(void 0, void 0, void 0, function () { var conversation; return __generator(this, function (_a) { switch (_a.label) { case 0: conversation = e.data; if (!conversation) return [2 /*return*/]; if (!(conversation.id === (activeConversation === null || activeConversation === void 0 ? void 0 : activeConversation.id))) return [3 /*break*/, 2]; if (!(conversation.unread > 0)) return [3 /*break*/, 2]; return [4 /*yield*/, client.setConversationRead(conversation.id)]; case 1: _a.sent(); conversation.unread = 0; _a.label = 2; case 2: dispatch({ type: ConversationListActionType.CONVERSATION_RECEIVED, payload: e.data, }); return [2 /*return*/]; } }); }); }; (_a = client === null || client === void 0 ? void 0 : client.on) === null || _a === void 0 ? void 0 : _a.call(client, EventType.CONVERSATION_UPDATED, onConversationUpdated); // 会话更新 return function () { // client?.off(EventType.CONVERSATION_UPDATED, onConversationUpdated); }; }, [client, dispatch, activeConversation]); var providerContextValue = useMemo(function () { return ({ client: client, activeProfile: activeProfile, activeConversation: activeConversation, setActiveConversation: setActiveConversation, }); }, [client, activeProfile, activeConversation, setActiveConversation]); var translators = useTranslators({ defaultLanguage: defaultLanguage, i18nInstance: i18nInstance, }).translators; return (React__default.createElement(UIKitProvider, { value: providerContextValue }, React__default.createElement(TranslationProvider, { value: translators }, !!clientInited && (React__default.createElement("div", { className: "uim-kit" }, children || (React__default.createElement(React__default.Fragment, null, React__default.createElement(UIConversationList, null), React__default.createElement(UIChat, null)))))))); }; function UIKit(_a) { var children = _a.children, rest = __rest(_a, ["children"]); return (React__default.createElement(Provider, { store: store }, React__default.createElement(UIKitInner, __assign({}, rest), children))); } export { UIKit }; //# sourceMappingURL=UIKit.js.map