modal.tsx
Overview
modal.tsx defines a React functional component named IndentedTreeModal that encapsulates a modal dialog displaying an indented tree visualization. This component is primarily responsible for fetching a knowledge graph data structure, translating UI text, and rendering the data inside an Ant Design Modal component using a child component called IndentedTree.
The modal’s visibility and dismissal behavior are controlled externally via props, making it reusable and easily integrable into different parts of the application where visualization of hierarchical mind maps or knowledge graphs is needed.
Detailed Explanation
Component: IndentedTreeModal
Purpose
IndentedTreeModal renders a modal window that visualizes a hierarchical mind map fetched from a knowledge graph API or hook. It uses internationalization for the modal title and Ant Design’s Modal component for UI consistency.
Props
IndentedTreeModal accepts the following props (based on the combined type IModalProps<any> & { documentId: string }):
Prop Name | Type | Description |
|---|---|---|
|
| Controls the visibility state of the modal. |
| Callback function to close or hide the modal. | |
|
| A string identifier for a document (passed in but unused here). |
Note: Although the
documentIdprop is typed as required, it is not directly used within this component.
Internal Hooks Used
useFetchKnowledgeGraph
Custom hook presumably fetching knowledge graph data asynchronously. Returns an object with adataproperty that contains the fetched mind map underdata.mind_map.useTranslation
Hook from react-i18next providing internationalization support. Used here to translate the modal title key'chunk.mind'.
JSX Structure and Behavior
<Modal
title={t('chunk.mind')}
open={visible}
onCancel={hideModal}
width={'90vw'}
footer={null}
>
<section>
<IndentedTree data={data?.mind_map} show></IndentedTree>
</section>
</Modal>
Modal
title: Translated string for the modal heading.open: Controlled by thevisibleprop.onCancel: InvokeshideModalto close the modal.width: Set to 90% of the viewport width for a wide display.footer: Disabled (set tonull) to remove default modal action buttons.
IndentedTree
Renders the mind map data passed asdataprop and always shows the tree (showprop set to true).
Return Value
IndentedTreeModal returns a React element tree representing the modal dialog interface. It does not return any other values or manipulate state internally.
Usage Example
import React, { useState } from 'react';
import IndentedTreeModal from './modal';
const ParentComponent = () => {
const [isModalVisible, setModalVisible] = useState(false);
const openModal = () => setModalVisible(true);
const closeModal = () => setModalVisible(false);
return (
<>
<button onClick={openModal}>Show Mind Map</button>
<IndentedTreeModal
visible={isModalVisible}
hideModal={closeModal}
documentId="doc_123"
/>
</>
);
};
Important Implementation Details
Data Fetching and Rendering Synchronization
The component relies onuseFetchKnowledgeGraphto asynchronously fetch the knowledge graph data. The tree (IndentedTree) is rendered only whendata?.mind_mapis defined, leveraging optional chaining to prevent runtime errors during loading states.Internationalization
The modal title usesuseTranslationensuring that the label'chunk.mind'can be localized according to the user’s language settings.User Experience
The modal is designed to occupy most of the viewport width (90vw), which is appropriate for visualizing detailed tree structures without requiring scrolling or zooming excessively.Reusability
By acceptingvisibleandhideModalvia props, the component’s modal state management is delegated to the parent, allowing flexible integration within different UI flows.
Interaction with Other System Parts
useFetchKnowledgeGraphHook
This hook is likely defined elsewhere in the codebase under@/hooks/knowledge-hooks. It abstracts the fetching logic of the knowledge graph data, potentially interacting with backend APIs or state management layers.IndentedTreeComponent
Imported from'./indented-tree', this component is responsible for rendering the hierarchical data structure visually. It works closely with this modal component to display the fetched mind map data.IModalPropsInterface
Defines the props contract for modal components within the app, ensuring consistency in how modals are controlled and interacted with.Ant Design Modal
Provides the underlying UI container with standardized styling and behavior, harmonizing with the app’s overall design system.
Mermaid Diagram: Component Interaction Diagram
The following diagram illustrates the structure and interactions between the main components and hooks within this file:
componentDiagram
component IndentedTreeModal {
+visible: boolean
+hideModal(): void
+documentId: string
--uses--
useFetchKnowledgeGraph
useTranslation
AntdModal
IndentedTree
}
component useFetchKnowledgeGraph
component useTranslation
component AntdModal
component IndentedTree
IndentedTreeModal --> useFetchKnowledgeGraph : fetches data
IndentedTreeModal --> useTranslation : translates title
IndentedTreeModal --> AntdModal : renders modal UI
IndentedTreeModal --> IndentedTree : passes mind_map data
Summary
modal.tsx is a concise, focused React component that efficiently combines data fetching, internationalization, and UI rendering to present a modal-based mind map visualization. It cleanly separates concerns by delegating data logic to hooks and visualization to child components, promoting modularity and maintainability within the larger application.