embed-app-modal.tsx
Overview
embed-app-modal.tsx defines a React functional component named EmbedAppModal that provides an interactive modal dialog interface for embedding an application via an iframe into external websites. The modal allows users to customize certain embed parameters such as hiding the user avatar and selecting the locale (language) for the embedded app. It dynamically generates the iframe embed code based on these settings and provides UI controls to copy the embed token for easy integration.
This component is designed to be used within a larger web application where embedding a mini app or widget is required, facilitating convenient sharing and localization.
Detailed Explanation
Component: EmbedAppModal
Purpose
EmbedAppModal renders a modal dialog that allows users to:
Toggle the visibility of the user avatar in the embedded app.
Select a locale (language) for the embedded app.
View and copy the auto-generated iframe embed code.
Copy the embed token (ID) used for authentication or identification.
Props (IEmbedAppModalProps)
Prop Name | Type | Description |
|---|---|---|
| Controls the visibility state of the modal. Typically a boolean indicating if modal is open or closed. | |
|
| Base URL path to be embedded inside the iframe. |
|
| Authentication or identification token for the embedded app. |
|
| A string indicating the source or context from where the embed is requested. |
| Callback function to toggle the modal's open state. | |
|
| Tenant identifier used for multi-tenant environments. |
|
| Optional beta feature flag or token passed as a query parameter. |
State Variables
hideAvatar(boolean): Controls whether the avatar is visible in the embedded iframe.locale(string): Stores the selected language/locale abbreviation.
Hooks and Utilities Used
useTranslate('search'): Custom hook for internationalized translations scoped to the 'search' namespace.useState: React state hook.useMemo: Memoizes the language options and embed code text for performance.useCallback: Memoizes the iframe source generation function.
Main Functions / Methods
generateIframeSrc
Generates the iframe src URL with query parameters based on current modal state and props.
Returns:
string— Fully constructed URL string for iframe embedding.Algorithm:
Starts with
location.origin + url.Appends mandatory query parameters:
shared_id,from,auth(beta), andtenantId.Conditionally appends
visible_avatar=1if avatar is hidden.Conditionally appends
localeif set.Returns the concatenated string.
text (memoized)
Generates the full HTML iframe embed code snippet as a markdown string with triple backticks and HTML tag.
Example output:
<iframe
src="https://example.com/embed?shared_id=token123&from=source&auth=beta&tenantId=tenant1&visible_avatar=1&locale=en"
style="width: 100%; height: 100%; min-height: 600px"
frameborder="0">
</iframe>
Usage Example
import EmbedAppModal from './embed-app-modal';
function ParentComponent() {
const [modalOpen, setModalOpen] = useState(false);
return (
<>
<button onClick={() => setModalOpen(true)}>Show Embed Modal</button>
<EmbedAppModal
open={modalOpen}
setOpen={setModalOpen}
url="/embed-app"
token="abc123"
from="dashboard"
tenantId="tenant_001"
beta="betaFeatureFlag"
/>
</>
);
}
UI Structure and Interaction
Modal Container (
Modalcomponent): Displays the modal with title translated as'embedIntoSite'.Hide Avatar Toggle: A labeled switch to toggle avatar visibility.
Locale Selector: A dropdown select control populated with language options from
LanguageAbbreviation.Embed Code Display: Shows the generated iframe code snippet with syntax highlighting (
HightLightMarkdowncomponent).ID Field: Read-only text input displaying the embed token with a copy button that copies the token to clipboard and shows a success message using Ant Design's
message.
Important Implementation Details
Language Options Generation: Uses
useMemoto convertLanguageAbbreviationenum values into an array of option objects{ label, value }by mapping to their full language names viaLanguageAbbreviationMap.Dynamic URL Parameters: Query parameters are constructed dynamically inside
generateIframeSrcto reflect current UI state for avatar visibility and locale.Clipboard Copy: Uses the modern
navigator.clipboard.writeTextAPI to copy the token, paired with a toast success notification.Accessibility: The copy button includes a
titleattribute and uses an SVG icon for visual affordance.Styling: Tailwind CSS classes are used extensively for layout and styles.
Interaction with Other System Components
ModalComponent: Imported from@/components/ui/modal/modal, provides the modal window framework.RAGFlowSelectComponent: Custom select dropdown UI for locale selection.SwitchComponent: Toggle switch UI control.HightLightMarkdownComponent: Renders the iframe embed code snippet with syntax highlighting.Constants: Uses
LanguageAbbreviationandLanguageAbbreviationMapfrom common constants.Hooks: Uses
useTranslatehook for localization.Ant Design
message: Provides user feedback on copying token.
This component is likely invoked by a parent page or component responsible for managing app embedding workflows, passing necessary props such as token, url, and tenantId.
Mermaid Component Diagram
componentDiagram
component EmbedAppModal {
+props: IEmbedAppModalProps
+state: hideAvatar:boolean, locale:string
+generateIframeSrc(): string
+render(): JSX.Element
}
EmbedAppModal --> Modal
EmbedAppModal --> Switch
EmbedAppModal --> RAGFlowSelect
EmbedAppModal --> HightLightMarkdown
EmbedAppModal --> useTranslate
EmbedAppModal --> message
Summary
embed-app-modal.tsx provides a reusable, customizable modal component facilitating the embedding of an external app via iframe. It offers user-friendly controls to adjust embed parameters, automatically generates the embed code, and supports copying both the embed code and token for easy integration. The component leverages React hooks, custom UI components, and localization for a seamless user experience in multi-lingual environments.