set-meta-dialog.tsx


Overview

set-meta-dialog.tsx defines a React functional component named SetMetaDialog that provides a user interface dialog for viewing and editing JSON meta data associated with a document or knowledge entry. This component leverages a rich code editor (Monaco Editor) to allow users to input or modify JSON-formatted metadata with validation and helpful tooltips. It integrates form state management, validation, and loading state feedback to offer a seamless and robust metadata editing experience.


Detailed Description

Component: SetMetaDialog

Purpose

SetMetaDialog is a modal dialog component that enables users to set or update the meta data of a document or knowledge item by entering JSON data. It validates the JSON input, displays error messages if invalid, and calls back to a parent handler on submission.

Props

Prop Name

Type

Description

hideModal

() => void (optional)

Callback to close or hide the modal dialog.

onOk

(meta: string) => Promise (optional)

Async callback triggered on form submit with the meta JSON string. Should return truthy if successful.

loading

boolean (optional)

Indicates if the form submission is in progress. Controls the loading state of the submit button.

initialMetaData

IDocumentInfo['meta_fields'] (optional)

Initial meta data object to populate the editor on dialog open.

Internal State & Hooks

Form Validation

Submission Handling

UI Elements


Usage Example

import React, { useState } from 'react';
import { SetMetaDialog } from './set-meta-dialog';
import { IDocumentInfo } from '@/interfaces/database/document';

function Example() {
  const [visible, setVisible] = useState(true);
  const [loading, setLoading] = useState(false);
  const initialMeta: IDocumentInfo['meta_fields'] = {
    author: 'John Doe',
    version: 1,
  };

  async function handleOk(metaJson: string) {
    setLoading(true);
    try {
      const meta = JSON.parse(metaJson);
      // Save meta to backend or state here...
      console.log('Saving meta:', meta);
      return true; // Indicate success
    } catch (e) {
      return false;
    } finally {
      setLoading(false);
    }
  }

  return (
    visible && (
      <SetMetaDialog
        hideModal={() => setVisible(false)}
        onOk={handleOk}
        loading={loading}
        initialMetaData={initialMeta}
      />
    )
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Component Structure

componentDiagram
    direction LR
    SetMetaDialog -- uses --> Dialog
    SetMetaDialog -- uses --> Form
    SetMetaDialog -- uses --> MonacoEditor
    SetMetaDialog -- uses --> ButtonLoading
    SetMetaDialog -- uses --> DOMPurify
    SetMetaDialog -- uses --> useTranslation
    SetMetaDialog -- uses --> react-hook-form
    SetMetaDialog -- uses --> zod

Summary

set-meta-dialog.tsx is a React component designed to facilitate editing JSON metadata within a modal dialog, featuring rich editing capabilities, robust validation, and seamless integration with application state and UI frameworks. It serves as a crucial user interface element for managing structured metadata in a user-friendly way while ensuring data integrity and security.