UNPKG

general-assistant

Version:

General Assistant components

41 lines (40 loc) 1.81 kB
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime"; import { useTranslation } from 'react-i18next'; import ErrorBoundary from '../ErrorBoundary/index'; import { Suspense, useEffect, useState, useContext } from 'react'; import { useStore } from 'zustand'; import StoreContext from '../_store/StoreContext'; import { SYNC_loadCompFromRemote } from '../_utils/tools'; export default function TypeCard({ option }) { const { t } = useTranslation(); const dataStore = useContext(StoreContext); const { myComponents, updateMyMsgComps, GlobalCtxs } = useStore(dataStore, (state) => ({ myComponents: state.myComponents, updateMyMsgComps: state.updateMyMsgComps, GlobalCtxs: state.GlobalCtxs })); const { type, content } = option; const [ThisCard, setThisCard] = useState(() => myComponents['Empty']); useEffect(() => { const value = myComponents[type]; if (typeof value !== 'function' && typeof value === 'string') { if (value.search(/^http/) === 0) { SYNC_loadCompFromRemote({ cardId: type, cardFileUrl: value }) .then((res) => { updateMyMsgComps(type, res[type]); setThisCard(() => res[type]); }) .catch((err) => { updateMyMsgComps(type, myComponents.Empty); }); } else { updateMyMsgComps(type, myComponents.Empty); } } else { setThisCard(() => value); } }, [type]); return (_jsx(ErrorBoundary, { data: option, t: t, children: _jsx(Suspense, { fallback: _jsx(_Fragment, { children: "error" }), children: GlobalCtxs && _jsx(ThisCard, { data: option, ctx: GlobalCtxs }) }) })); }