use-export-json.ts

Overview

The use-export-json.ts file defines a custom React hook useHandleExportOrImportJsonFile that manages the import and export of flow graph data in JSON format within a React application. This hook provides functionalities to:

This hook integrates with other hooks and utilities in the system to enable seamless serialization and deserialization of flow graph data, facilitating persistence and restoration of flow states.


Detailed Explanation

useHandleExportOrImportJsonFile

Description

This is a React custom hook that encapsulates the logic for exporting and importing flow graph JSON files. It manages modal visibility state, file validation, parsing, and updates the flow graph state on successful import.

Returns

An object containing:

Property

Type

Description

fileUploadVisible

boolean

Controls the visibility state of the import JSON file modal.

handleExportJson

() => void

Function to export the current flow graph data to a JSON file.

handleImportJson

() => void

Function to show the file upload modal for importing JSON files.

hideFileUploadModal

() => void

Function to hide the file upload modal.

onFileUploadOk

(fileList: UploadFile[]) => Promise

Async function to handle the file upload confirmation, parse and validate JSON, and update graph state.

Internal Hook Dependencies

Parameters

The hook itself takes no parameters, but internally:

Usage Example

import React from 'react';
import { Button, Modal, Upload } from 'antd';
import { useHandleExportOrImportJsonFile } from './use-export-json';

const FlowExportImportComponent = () => {
  const {
    fileUploadVisible,
    handleExportJson,
    handleImportJson,
    hideFileUploadModal,
    onFileUploadOk,
  } = useHandleExportOrImportJsonFile();

  return (
    <>
      <Button onClick={handleExportJson}>Export Flow JSON</Button>
      <Button onClick={handleImportJson}>Import Flow JSON</Button>

      <Modal
        visible={fileUploadVisible}
        onCancel={hideFileUploadModal}
        footer={null}
        title="Import Flow JSON"
      >
        <Upload
          accept=".json"
          beforeUpload={() => false} // Prevent automatic upload
          onChange={({ fileList }) => onFileUploadOk(fileList)}
          maxCount={1}
        >
          <Button>Select JSON File</Button>
        </Upload>
      </Modal>
    </>
  );
};

Implementation Details

Importing JSON

Exporting JSON

Modal Control


Interaction With Other System Parts


Mermaid Flowchart Diagram

The following flowchart outlines the main functions and their relationships in use-export-json.ts:

flowchart TD
    A[useHandleExportOrImportJsonFile] --> B[buildDslData()]
    A --> C[useFetchFlow.data]
    A --> D[useSetGraphInfo]
    A --> E[useSetModalState]
    A --> F[useTranslation.t]
    A --> G[onFileUploadOk(fileList)]
    A --> H[handleExportJson()]
    A --> I[handleImportJson()]
    G --> J[Validate file type]
    J --> K{Is JSON?}
    K -- No --> L[Show error: Invalid file type]
    K -- Yes --> M[Read file text]
    M --> N[Parse JSON]
    N --> O{Valid graph object?}
    O -- No --> P[Show error: Invalid content]
    O -- Yes --> Q[Update graph state (setGraphInfo)]
    Q --> R[Hide upload modal (hideFileUploadModal)]
    H --> S[Generate DSL data]
    S --> T[Download JSON file with flow title]
    I --> U[Show upload modal (showFileUploadModal)]

Summary

use-export-json.ts provides a focused, reusable hook to handle the export and import of flow graphs in JSON format. It abstracts file validation, modal management, and state synchronization, while integrating with other hooks and utilities in the application. This helps maintain clean separation of concerns and enhances maintainability and user experience for flow persistence features.