UNPKG

general-assistant

Version:

General Assistant components

47 lines (46 loc) 2.27 kB
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime"; import { Bubble } from 'datastellar-chatui'; import { SYNC_loadCompFromRemote } from '../../_utils/tools'; import { useEffect, useState, useContext } from 'react'; import { useStore } from 'zustand'; import StoreContext from '../../_store/StoreContext'; import { toLower } from 'lodash'; const NotFilter = ['Text', 'Markdown', 'Empty', 'Load', 'Stytem', 'Combine', 'Custom']; const Combine = ({ data, ctx }) => { const { content } = data; const { data: msgList = [] } = content; const dataStore = useContext(StoreContext); const { myComponents, updateMyMsgComps, GlobalCtxs } = useStore(dataStore, (state) => ({ myComponents: state.myComponents, updateMyMsgComps: state.updateMyMsgComps, GlobalCtxs: state.GlobalCtxs })); const [myMsgList, setMyMsgList] = useState(msgList); useEffect(() => { myMsgList.filter((item = {}) => { const { cardId, cardFileUrl } = item; if (typeof myComponents[cardId] !== 'function') { if (cardFileUrl.search(/^http/) === 0) { SYNC_loadCompFromRemote({ cardId, cardFileUrl }) .then((res) => { updateMyMsgComps(cardId, res[cardId]); }) .catch((err) => { updateMyMsgComps(cardId, myComponents.Empty); }); } else { updateMyMsgComps(cardId, myComponents.Empty); } } }); }, []); useEffect(() => { setMyMsgList(msgList); }, [msgList]); return (_jsx(_Fragment, { children: myMsgList && GlobalCtxs && (_jsx("div", { className: 'DS-GA-flex DS-GA-flex-col DS-GA-gap-2', children: myMsgList.map((item = {}, index) => { const ItemCard = myComponents[item.cardId]; return (_jsx(Bubble, { type: toLower(item.cardId), children: ItemCard && typeof ItemCard === 'function' && (_jsx(ItemCard, { data: Object.assign({ type: item.cardId }, NotFilter.includes(item.cardId) ? item : { ...item.content.data }), ctx: GlobalCtxs })) }, index)); }) })) })); }; export default Combine;