use-import-json.ts


Overview

The use-import-json.ts file defines a custom React hook named useHandleImportJsonFile that facilitates importing and processing JSON files within a React application. Its primary responsibility is to manage the user interaction flow related to uploading JSON files representing graph data, validate the file type and content, parse the JSON into a specific data structure, and then update the application state accordingly.

This hook integrates modal visibility state management, file validation, JSON parsing, error handling, and toast notifications to provide a seamless user experience when importing JSON files, particularly for setting or updating an "agent" configuration within the app.


Detailed Explanation

Hook: useHandleImportJsonFile

export const useHandleImportJsonFile = () => { ... }

Description

This custom hook exposes functionality for handling the import process of a JSON file containing graph data. It manages modal visibility for file upload, validates the uploaded file type and content, parses the JSON, and updates the agent state if successful.

Returned Object Properties

Property

Type

Description

fileUploadVisible

boolean

Indicates whether the file upload modal is currently visible.

handleImportJson

() => void

Function to show the file upload modal (trigger import dialog).

hideFileUploadModal

() => void

Function to hide the file upload modal.

onFileUploadOk

(params: FormSchemaType) => Promise<void>

Async handler called when the user confirms file upload; validates and processes the file.

loading

boolean

Represents the loading state during the agent setting operation.

Internal Dependencies


Method: onFileUploadOk

async ({ fileList, name }: FormSchemaType) => void

Description

This asynchronous callback is triggered when the user confirms the file upload in the modal. It performs several key steps:

  1. Checks if any files were uploaded.

  2. Validates the MIME type of the first file is JSON.

  3. Reads the file content as text.

  4. Attempts to parse the JSON string.

  5. Checks the parsed object to ensure it contains a non-empty nodes array.

  6. If valid, merges the parsed graph into the EmptyDsl template and updates the agent state.

  7. If invalid or an error occurs, shows error messages via toast or antd message.

Parameters

Parameter

Type

Description

fileList

File[]

Array of files uploaded by the user.

name

string

The name/title associated with the uploaded file.

Returns

Promise<void> — completes after processing or error handling.

Usage Example

const {
  fileUploadVisible,
  handleImportJson,
  hideFileUploadModal,
  onFileUploadOk,
  loading,
} = useHandleImportJsonFile();

// To open the file import modal
handleImportJson();

// When a file is selected and confirmed in the modal form:
await onFileUploadOk({ fileList: [selectedFile], name: selectedFile.name });

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

Below is a Mermaid flowchart showing the main functions and their relationships within use-import-json.ts:

flowchart TD
    A[useHandleImportJsonFile Hook]
    A --> B[fileUploadVisible state]
    A --> C[handleImportJson (show modal)]
    A --> D[hideFileUploadModal (hide modal)]
    A --> E[onFileUploadOk(fileList, name)]
    A --> F[loading state]

    E --> G{fileList.length > 0?}
    G -- No --> H[Do nothing]
    G -- Yes --> I[file = fileList[0]]
    I --> J{file.type === JSON?}
    J -- No --> K[toast error: invalid file type]
    J -- Yes --> L[Read file.text()]
    L --> M[Parse JSON]
    M --> N{Valid graph?}
    N -- Yes --> O[Create DSL with graph]
    O --> P[setAgent({title, dsl})]
    P --> Q[hideFileUploadModal()]
    N -- No --> R[message.error(content error)]
    M -->|catch| R

Summary

The use-import-json.ts file encapsulates the logic for importing and validating JSON files representing graph data within a React application. It provides a reusable hook that manages modal visibility, user interactions, file validation, JSON parsing, error handling, and updates the application's agent state accordingly. By modularizing this functionality, it enables other components or pages to easily integrate JSON import features with consistent user feedback and localization support.


If you have any questions or need further clarification on specific parts of this file, feel free to ask!