use-export-json.ts


Overview

This file defines a React hook, useHandleExportOrImportJsonFile, that manages the export and import of JSON files representing graph data in a flow-based application. The primary functionality includes:

This hook integrates with various custom hooks and utilities in the system, facilitating seamless interaction with UI modals, toast notifications, translation, and graph state management.


Detailed Explanation

Export / Import Hook: useHandleExportOrImportJsonFile

This is the default exported hook from the file, encapsulating all logic related to exporting and importing graph JSON files.

Returned Object


onFileUploadOk Function

This method handles the user action of uploading JSON files for import.

Parameters:

Behavior:

  1. Checks if fileList contains at least one file.

  2. Validates that the uploaded file's MIME type is JSON (application/json).

  3. Reads the file content as text.

  4. Parses the content into a JSON object.

  5. Validates that the parsed JSON object is non-empty and contains a nodes array.

  6. If valid, updates the graph state using setGraphInfo and closes the modal.

  7. If invalid or on parsing error, displays an error message to the user.

Error Handling:

Example Usage:

// Called when user confirms file upload in modal
onFileUploadOk({ fileList: [uploadedFile], platform: Platform.Web });

handleExportJson Function

Triggers the export of the current graph data to a JSON file.

Behavior:

Example Usage:

// Called on click of "Export JSON" button
handleExportJson();

Important Implementation Details


Interaction with Other Parts of the System


Usage Example in a Component

import React from 'react';
import { useHandleExportOrImportJsonFile } from './use-export-json';

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

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

      {fileUploadVisible && (
        <FileUploadModal
          visible={fileUploadVisible}
          onOk={onFileUploadOk}
          onCancel={hideFileUploadModal}
        />
      )}
    </>
  );
};

Mermaid Diagram of File Structure and Workflow

flowchart TD
    A[useHandleExportOrImportJsonFile Hook] --> B(buildDslData)
    A --> C(useSetModalState)
    A --> D(useSetGraphInfo)
    A --> E(useFetchAgent)
    A --> F(useToast)
    A --> G(useTranslation)
    A --> H(downloadJsonFile)
    A --> I[onFileUploadOk]
    A --> J[handleExportJson]

    I -->|calls| D
    I -->|calls| F
    I -->|calls| message.error
    J -->|calls| B
    J -->|calls| H

Summary

The use-export-json.ts file provides a clean, reusable React hook that abstracts the complexity of exporting and importing flow graph data as JSON files. It ensures file validation, user feedback, and seamless integration with the application’s state and UI layers. This modular approach enhances maintainability and user experience in managing flow data serialization.