@uimkit/uikit-react
Version:
<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>
156 lines (149 loc) • 8.03 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var React = require('react');
var reactRedux = require('react-redux');
var UIChat = require('../UIChat/UIChat.js');
var index = require('../../store/index.js');
require('redux');
require('../../types/models.js');
var events = require('../../types/events.js');
require('../../store/rootReducer.js');
require('../../store/accounts/actions.js');
require('../../store/accounts/selectors.js');
require('../../node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/invariant.js');
require('i18next');
var actions$2 = require('../../store/conversations/actions.js');
require('../../node_modules/.pnpm/lodash.flatten@4.4.0/node_modules/lodash.flatten/index.js');
require('../../node_modules/.pnpm/lodash.some@4.6.0/node_modules/lodash.some/index.js');
var commands = require('../../store/conversations/commands.js');
var actions$1 = require('../../store/messages/actions.js');
require('../../store/contacts/actions.js');
var useDispatch = require('../../store/useDispatch.js');
var TranslationContext = require('../../context/TranslationContext.js');
var UIKitContext = require('../../context/UIKitContext.js');
require('../../context/ComponentContext.js');
require('../../context/ChatActionContext.js');
require('../../context/MessageInputContext.js');
require('../../context/UIMessageContext.js');
require('../../context/ChatStateContext.js');
var UIConversationList = require('../UIConversationList/UIConversationList.js');
var index$1 = require('../Toast/index.js');
var useTranslators = require('./hooks/useTranslators.js');
require('../../i18n/uimi18n.js');
var actions = require('../../store/common/actions.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var appThunkContext = {
// 全局的错误处理
onError: function (e, title) {
index$1.Toast({ text: e.message, type: 'error' });
}
};
var store = index.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.useDispatch();
var clientInited = reactRedux.useSelector(function (state) { return state.common.client; });
dispatch(actions.initAPIClient(client));
var _a = React.useState(), activeConversation = _a[0], _setActiveConversation = _a[1];
var setActiveConversation = React.useCallback(function (activeConversation) {
if (activeConversation) {
if (activeConversation.unread > 0) {
client === null || client === void 0 ? void 0 : client.setConversationRead(activeConversation.id);
}
dispatch(commands.updateConversation({
account: activeConversation.account,
id: activeConversation.id,
unread: 0,
}));
}
_setActiveConversation(activeConversation);
}, [client]);
React.useEffect(function () {
if (clientInited) {
setActiveConversation(propActiveConversation);
}
}, [propActiveConversation, clientInited]);
React.useEffect(function () {
var _a, _b, _c;
var onMessageReceived = function (e) {
dispatch({
type: actions$1.ConversationActionType.MESSAGE_RECEIVED,
payload: e.data,
});
};
var onMessageUpdated = function (e) {
dispatch({
type: actions$1.ConversationActionType.MESSAGE_UPDATE,
payload: e.data,
});
};
var onConversationUpdated = function (e) {
dispatch({
type: actions$2.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, events.EventType.MESSAGE_RECEIVED, onMessageReceived); // 收到新消息
(_b = client === null || client === void 0 ? void 0 : client.on) === null || _b === void 0 ? void 0 : _b.call(client, events.EventType.MESSAGE_UPDATED, onMessageUpdated); // 更新消息
(_c = client === null || client === void 0 ? void 0 : client.on) === null || _c === void 0 ? void 0 : _c.call(client, events.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]);
React.useEffect(function () {
var _a;
var onConversationUpdated = function (e) { return tslib.__awaiter(void 0, void 0, void 0, function () {
var conversation;
return tslib.__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: actions$2.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, events.EventType.CONVERSATION_UPDATED, onConversationUpdated); // 会话更新
return function () {
// client?.off(EventType.CONVERSATION_UPDATED, onConversationUpdated);
};
}, [client, dispatch, activeConversation]);
var providerContextValue = React.useMemo(function () { return ({
client: client,
activeProfile: activeProfile,
activeConversation: activeConversation,
setActiveConversation: setActiveConversation,
}); }, [client, activeProfile, activeConversation, setActiveConversation]);
var translators = useTranslators.useTranslators({
defaultLanguage: defaultLanguage,
i18nInstance: i18nInstance,
}).translators;
return (React__default["default"].createElement(UIKitContext.UIKitProvider, { value: providerContextValue },
React__default["default"].createElement(TranslationContext.TranslationProvider, { value: translators }, !!clientInited && (React__default["default"].createElement("div", { className: "uim-kit" }, children || (React__default["default"].createElement(React__default["default"].Fragment, null,
React__default["default"].createElement(UIConversationList.UIConversationList, null),
React__default["default"].createElement(UIChat.UIChat, null))))))));
};
function UIKit(_a) {
var children = _a.children, rest = tslib.__rest(_a, ["children"]);
return (React__default["default"].createElement(reactRedux.Provider, { store: store },
React__default["default"].createElement(UIKitInner, tslib.__assign({}, rest), children)));
}
exports.UIKit = UIKit;
//# sourceMappingURL=UIKit.js.map