Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Add FormEmbodimentModal support to code snippet handling

Muhammad Hamza Khan7d7d44ed4730
diff --git a/packages/app/src/react/features/embodiments/embodiment-configs.tsx b/packages/app/src/react/features/embodiments/embodiment-configs.tsx
index 26738a07..6ce51f6a 100644
--- a/packages/app/src/react/features/embodiments/embodiment-configs.tsx
+++ b/packages/app/src/react/features/embodiments/embodiment-configs.tsx
@@ -6,6 +6,7 @@ import ChatBotDialog from '../agent-settings/dialogs/ChatBot';
import ChatGptDialog from '../agent-settings/dialogs/ChatGpt';
import FormPreviewDialog from '../agent-settings/dialogs/FormPreview';
import ChatbotCodeSnippetModal from '../agent-settings/modals/chatbotCode.modal';
+import FormEmbodimentModal from './form-embodiment-modal';
export const AlwaysAvailableEmbodiments = [
'API',
@@ -172,6 +173,7 @@ export const getFormPreviewDialog = (
};
export const getCodeSnippetModal = (
+ embodimentType: EMBODIMENT_TYPE.FORM | EMBODIMENT_TYPE.CHAT_BOT,
isOpen: boolean,
closeModal: () => void,
agent,
@@ -184,14 +186,27 @@ export const getCodeSnippetModal = (
return null; // Return null to not render when closed, allowing iframe to reload
}
- return createPortal(
- <ChatbotCodeSnippetModal
- onClose={closeModal}
- domain={agent?.domain?.[0]?.name}
- embodimentData={embodimentsData?.find(
- (e) => e?.aiAgentId === agentId && e?.type === EMBODIMENT_TYPE.CHAT_BOT,
- )}
- />,
- document.body,
- );
+ if (embodimentType === EMBODIMENT_TYPE.CHAT_BOT) {
+ return createPortal(
+ <ChatbotCodeSnippetModal
+ onClose={closeModal}
+ domain={agent?.domain?.[0]?.name}
+ embodimentData={embodimentsData?.find(
+ (e) => e?.aiAgentId === agentId && e?.type === embodimentType,
+ )}
+ />,
+ document.body,
+ );
+ }
+
+ if (embodimentType === EMBODIMENT_TYPE.FORM) {
+ return createPortal(
+ <FormEmbodimentModal
+ onClose={closeModal}
+ domain={agent?.domain?.[0]?.name}
+ showBackButton={false}
+ />,
+ document.body,
+ );
+ }
};
diff --git a/packages/app/src/react/features/embodiments/embodiment-helper.ts b/packages/app/src/react/features/embodiments/embodiment-helper.ts
index ae2eccf6..58a24173 100644
--- a/packages/app/src/react/features/embodiments/embodiment-helper.ts
+++ b/packages/app/src/react/features/embodiments/embodiment-helper.ts
@@ -48,7 +48,8 @@ export function structureAgentSetting(
openModal: (modalType: string) => void;
closeModal: () => void;
showCodeSnippet: boolean;
- openCodeSnippet: () => void;
+ activeCodeSnippetKey: string | null;
+ openCodeSnippet: (key: string) => void;
closeCodeSnippet: () => void;
agent?: Agent;
embodimentsData?: Embodiment[];
@@ -107,7 +108,7 @@ export function structureAgentSetting(
// Add modal handlers and components if provided
if (modalHandlers) {
result.openModal = () => modalHandlers.openModal(key);
- result.openCodeSnippet = modalHandlers.openCodeSnippet;
+ result.openCodeSnippet = () => modalHandlers.openCodeSnippet(key);
// PRELOADING STRATEGY:
// Create modal components for preloading if this is the active instance
@@ -159,9 +160,13 @@ export function structureAgentSetting(
// Add code snippet modal for chat bot (preload when it should be visible)
// Note: iframe-based components are conditionally rendered to allow reload as requested
- if (key === EMBODIMENT_TYPE.CHAT_BOT && shouldCodeSnippetVisible(key, agentData)) {
+ if (
+ (key === EMBODIMENT_TYPE.CHAT_BOT || key === EMBODIMENT_TYPE.FORM) &&
+ shouldCodeSnippetVisible(key, agentData)
+ ) {
result.codeSnippetComponent = getCodeSnippetModal(
- modalHandlers.showCodeSnippet, // Pass visibility state
+ key,
+ modalHandlers.showCodeSnippet && modalHandlers.activeCodeSnippetKey === key, // Pass visibility state
modalHandlers.closeCodeSnippet,
modalHandlers.agent,
agentData.agentId,
@@ -183,7 +188,7 @@ const shouldCodeSnippetVisible = (
},
) => {
return (
- embodimentType === EMBODIMENT_TYPE.CHAT_BOT &&
+ (embodimentType === EMBODIMENT_TYPE.CHAT_BOT || embodimentType === EMBODIMENT_TYPE.FORM) &&
agentData?.agentDeployed &&
agentData.canUseEmbodiments
);
@@ -228,7 +233,8 @@ export const useAgentSettings = (
openModal: (modalType: string) => void;
closeModal: () => void;
showCodeSnippet: boolean;
- openCodeSnippet: () => void;
+ activeCodeSnippetKey: string | null;
+ openCodeSnippet: (key: string) => void;
closeCodeSnippet: () => void;
agent?: Agent;
embodimentsData?: Embodiment[];
diff --git a/packages/app/src/react/features/embodiments/embodiment-settings.ts b/packages/app/src/react/features/embodiments/embodiment-settings.ts
index 5404b157..fd493aeb 100644
--- a/packages/app/src/react/features/embodiments/embodiment-settings.ts
+++ b/packages/app/src/react/features/embodiments/embodiment-settings.ts
@@ -45,6 +45,7 @@ let globalModalManager = {
activeInstanceId: null as string | null,
activeModal: null as string | null,
showCodeSnippet: false,
+ activeCodeSnippetKey: null as string | null,
subscribers: new Set<string>(),
registerInstance: (instanceId: string, agentId: string) => {
@@ -64,6 +65,7 @@ let globalModalManager = {
globalModalManager.activeAgentId = null;
globalModalManager.activeModal = null;
globalModalManager.showCodeSnippet = false;
+ globalModalManager.activeCodeSnippetKey = null;
}
},
@@ -81,13 +83,15 @@ let globalModalManager = {
}
},
- setCodeSnippet: (instanceId: string, agentId: string, show: boolean) => {
+ setCodeSnippet: (instanceId: string, agentId: string, show: boolean, key: string) => {
if (show) {
globalModalManager.activeInstanceId = instanceId;
globalModalManager.activeAgentId = agentId;
globalModalManager.showCodeSnippet = true;
+ globalModalManager.activeCodeSnippetKey = key;
} else {
globalModalManager.showCodeSnippet = false;
+ globalModalManager.activeCodeSnippetKey = null;
}
// Notify components of modal state change
if (typeof window !== 'undefined') {
@@ -127,6 +131,7 @@ export const useAgentEmbodimentSettings = (
// Local state for this instance
const [activeModal, setActiveModal] = useState<string | null>(null);
const [showCodeSnippet, setShowCodeSnippet] = useState(false);
+ const [activeCodeSnippetKey, setActiveCodeSnippetKey] = useState<string | null>(null);
const { hasReadOnlyPageAccess, userInfo, loading: userInfoLoading } = useAuthCtx();
const planProperties = userInfo?.subs?.plan?.properties;
@@ -150,9 +155,11 @@ export const useAgentEmbodimentSettings = (
if (globalModalManager.isActiveInstance(hookInstanceId)) {
setActiveModal(globalModalManager.activeModal);
setShowCodeSnippet(globalModalManager.showCodeSnippet);
+ setActiveCodeSnippetKey(globalModalManager.activeCodeSnippetKey);
} else {
setActiveModal(null);
setShowCodeSnippet(false);
+ setActiveCodeSnippetKey(null);
}
}, [hookInstanceId]);
@@ -167,14 +174,16 @@ export const useAgentEmbodimentSettings = (
setActiveModal(null);
};
- const openCodeSnippet = () => {
- globalModalManager.setCodeSnippet(hookInstanceId, agentId, true);
+ const openCodeSnippet = (key: string) => {
+ globalModalManager.setCodeSnippet(hookInstanceId, agentId, true, key);
setShowCodeSnippet(true);
+ setActiveCodeSnippetKey(key);
};
const closeCodeSnippet = () => {
- globalModalManager.setCodeSnippet(hookInstanceId, agentId, false);
+ globalModalManager.setCodeSnippet(hookInstanceId, agentId, false, null);
setShowCodeSnippet(false);
+ setActiveCodeSnippetKey(null);
};
// Use React Query helper for agent embodiments first
@@ -192,6 +201,7 @@ export const useAgentEmbodimentSettings = (
showCodeSnippet,
openCodeSnippet,
closeCodeSnippet,
+ activeCodeSnippetKey,
agent: agentQuery?.data,
embodimentsData,
refreshEmbodiments: () => refetchEmbodiments(),
@@ -248,7 +258,7 @@ export const useAgentEmbodimentSettings = (
queryClient.setQueryData(['agentSettings', agentId], updatedSettings);
callCallback();
}
- }, [activeModal, showCodeSnippet, embodimentsData, agentQuery?.data]);
+ }, [activeModal, showCodeSnippet, activeCodeSnippetKey, embodimentsData, agentQuery?.data]);
const { data: availableEmbodiments = [] } = useQuery({
queryKey: ['availableEmbodiments', agentId],
@@ -272,6 +282,7 @@ export const useAgentEmbodimentSettings = (
openCodeSnippet,
closeCodeSnippet,
agent: agentQuery?.data,
+ activeCodeSnippetKey,
embodimentsData,
refreshEmbodiments: () => refetchEmbodiments(),
isActiveInstance: globalModalManager.isActiveInstance(hookInstanceId),
@@ -368,6 +379,7 @@ export const useAgentEmbodimentSettings = (
openModal,
closeModal,
showCodeSnippet,
+ activeCodeSnippetKey,
openCodeSnippet,
closeCodeSnippet,
isActiveInstance: globalModalManager.isActiveInstance(hookInstanceId),
diff --git a/packages/app/src/react/features/embodiments/form-embodiment-modal.tsx b/packages/app/src/react/features/embodiments/form-embodiment-modal.tsx
index 518d44f4..cd9c182d 100644
--- a/packages/app/src/react/features/embodiments/form-embodiment-modal.tsx
+++ b/packages/app/src/react/features/embodiments/form-embodiment-modal.tsx
@@ -19,6 +19,10 @@ export interface FormEmbodimentModalProps {
* Loading state while embodiment data is being fetched.
*/
isLoading?: boolean;
+ /**
+ * Whether to show the back button.
+ */
+ showBackButton?: boolean;
}
/**
@@ -32,6 +36,7 @@ const FormEmbodimentModal: React.FC<FormEmbodimentModalProps> = ({
onClose,
domain = 'your-domain.com',
isLoading = false,
+ showBackButton = true,
}) => {
const [copied, setCopied] = useState(false);
const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -87,6 +92,7 @@ const FormEmbodimentModal: React.FC<FormEmbodimentModalProps> = ({
title="Form Preview Integration Snippet"
onBack={onClose}
onClose={onClose}
+ showBackButton={showBackButton}
/>
{/* Content */}
diff --git a/packages/app/src/react/features/embodiments/modal-header-embodiment.tsx b/packages/app/src/react/features/embodiments/modal-header-embodiment.tsx
index f41554e4..b4784b2b 100644
--- a/packages/app/src/react/features/embodiments/modal-header-embodiment.tsx
+++ b/packages/app/src/react/features/embodiments/modal-header-embodiment.tsx
@@ -21,6 +21,10 @@ export interface ModalHeaderEmbodimentProps {
* Additional CSS classes for the header container.
*/
className?: string;
+ /**
+ * Whether to show the back button.
+ */
+ showBackButton?: boolean;
}
/**
@@ -35,24 +39,27 @@ const ModalHeaderEmbodiment: React.FC<ModalHeaderEmbodimentProps> = ({
onBack,
onClose,
className = '',
+ showBackButton = true,
}) => {
return (
<div className={`relative mb-4 ${className}`}>
{/* Title */}
- <div className="mt-[-7px] pl-8 pr-8">
- <span className="block text-lg font-semibold leading-tight text-[#222] text-left">
+ <div className={`mt-[-7px] pr-8 ${showBackButton ? 'pl-8' : ''}`}>
+ <span className="block text-xl font-semibold leading-tight text-[#1E1E1E] text-left">
{title}
</span>
</div>
{/* Back button */}
- <button
- className="absolute -left-2 -top-2 p-[6px] text-[#222] hover:bg-gray-100 rounded"
- onClick={onBack}
- aria-label="Back"
- style={{ lineHeight: 0 }}
- >
- <BackButtonWithTail width={16} height={14} />
- </button>
+ {showBackButton && (
+ <button
+ className="absolute -left-2 -top-2 p-[6px] text-[#222] hover:bg-gray-100 rounded"
+ onClick={onBack}
+ aria-label="Back"
+ style={{ lineHeight: 0 }}
+ >
+ <BackButtonWithTail width={16} height={14} />
+ </button>
+ )}
{/* Close button */}
<button
className="absolute -right-2 -top-2 p-[6px] text-[#222] hover:bg-gray-100 rounded"
@@ -65,4 +72,4 @@ const ModalHeaderEmbodiment: React.FC<ModalHeaderEmbodimentProps> = ({
);
};
-export default ModalHeaderEmbodiment;
\ No newline at end of file
+export default ModalHeaderEmbodiment;