index.tsx

Overview

This file implements a React component for uploading JSON files through a modal dialog, intended for use in a file management context. It leverages the Ant Design UI library's Upload and Modal components, combined with custom hooks and constants, to provide a localized, user-friendly interface for single-file JSON uploads.

There are two main components:

The file is structured to handle file state management, UI localization, and integration with parent components through callbacks.


Components and Functions

FileUpload

Description

FileUpload is a controlled React functional component rendering a drag-and-drop uploader (Dragger) from Ant Design. It allows users to select or drag JSON files for upload. It supports uploading directories if the directory prop is set to true.

Props

Prop

Type

Description

directory

boolean

Enables directory upload if true.

fileList

UploadFile[]

The current list of selected files.

setFileList

Dispatch>

Setter function to update the file list state.

Behavior

Return Value

Returns JSX elements representing the drag-and-drop uploader UI.

Usage Example

const [fileList, setFileList] = useState<UploadFile[]>([]);

// Render the uploader without directory support
<FileUpload directory={false} fileList={fileList} setFileList={setFileList} />

JsonUploadModal

Description

JsonUploadModal is the main exported component that renders an Ant Design modal dialog containing the FileUpload component. It manages the state of selected files, handles confirmation (upload) actions, and resets state upon modal closure.

Props

Extends from IModalProps<UploadFile[]> interface:

Prop

Type

Description

visible

boolean

Controls modal visibility.

hideModal

() => void

Function to close/hide the modal.

loading

boolean

Controls loading state of the modal's confirmation button.

onOk

(files: UploadFile[]) => Promise<any> (optional)

Callback executed when user confirms upload, receives selected file list.

Internal State

State

Type

Description

fileList

UploadFile[]

List of files uploaded (non-directory).

directoryFileList

UploadFile[]

List of files uploaded from directories (currently unused in UI).

Methods

Return Value

Returns JSX elements representing the modal UI containing the FileUpload component.

Usage Example

const [modalVisible, setModalVisible] = useState(false);

const handleUploadOk = async (files: UploadFile[]) => {
  // Process uploaded files here
  console.log(files);
  setModalVisible(false);
};

<JsonUploadModal
  visible={modalVisible}
  hideModal={() => setModalVisible(false)}
  loading={false}
  onOk={handleUploadOk}
/>

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

componentDiagram
    JsonUploadModal --> Modal
    JsonUploadModal --> FileUpload
    FileUpload --> Dragger
    FileUpload --> UploadProps
    FileUpload --> InboxOutlined
    JsonUploadModal --uses--> useTranslate
    FileUpload --uses--> useTranslate
    JsonUploadModal --manages--> fileList
    JsonUploadModal --manages--> directoryFileList

Summary

This file defines a reusable JSON file uploader modal component that is well-suited for integration in applications requiring file import functionality. It encapsulates UI, state, and logic for selecting JSON files through drag-and-drop or file dialog, providing a localized and streamlined user experience. The design leverages popular React patterns and Ant Design components, making it easy to extend or customize.


If you need additional details or integration instructions, please let me know!