saving-button.tsx
Overview
The saving-button.tsx file defines two React functional components, GeneralSavingButton and SavingButton, which provide reusable "Save" buttons integrated with form handling and asynchronous submission logic for updating knowledge configurations in a knowledge base management system.
Both components are tightly coupled with the React Hook Form library for form state management, use custom hooks for API interaction (useUpdateKnowledge), and leverage internationalization (react-i18next) for button text. The components handle form validation and submission, showing a loading state during ongoing API requests.
Components
1. GeneralSavingButton
Description
GeneralSavingButton renders a button that triggers a save operation for knowledge configuration data. It validates the "name" field specifically before submitting the form data. It retrieves some default values from the form state, such as parser_id, and includes them in the submission payload.
Implementation Details
Uses
useFormContextfromreact-hook-formto access form methods and state.Extracts
kb_id(knowledge base ID) from the URL parameters usinguseParams(fromumi).Uses
useUpdateKnowledgecustom hook to get thesaveKnowledgeConfigurationfunction and the loading state.Uses
useTranslationfrom react-i18next for localizing button text.Uses an asynchronous self-invoking function inside the click handler to:
Trigger validation on the "name" field only.
Retrieve form values (
name,description,permission,avatar).If validation passes, call
saveKnowledgeConfigurationwith these values pluskb_idandparser_id.
Props
This component does not take props; it uses hooks to get all needed data and methods.
Return Value
JSX element rendering a
ButtonLoadingcomponent with loading state and click handler.
Usage Example
import { GeneralSavingButton } from './saving-button';
function KnowledgeConfigForm() {
return (
<form>
{/* form fields */}
<GeneralSavingButton />
</form>
);
}
2. SavingButton
Description
SavingButton renders a more general "Save" button that triggers form submission after validating all fields via formControl.trigger(). It uses react-hook-form's handleSubmit method to gather and validate form data and then submits it by calling saveKnowledgeConfiguration. This component excludes the avatar field from the submitted payload.
Implementation Details
Similar hook usage as
GeneralSavingButton.Full form validation is triggered before submission.
Uses
handleSubmitfromreact-hook-formto manage form submission.On successful validation, calls the save API and excludes the
avatarfield from the payload.Logs errors if the submission process fails.
Props
No props, relies on hooks.
Return Value
JSX element rendering a
ButtonLoadingcomponent with loading indicator and click handler.
Usage Example
import { SavingButton } from './saving-button';
function KnowledgeConfigForm() {
return (
<form>
{/* form fields */}
<SavingButton />
</form>
);
}
Important Implementation Details and Algorithms
Form Validation: Both components use react-hook-form's validation methods (
triggerfor field-level or full form validation).Asynchronous Submission: On button click, an async IIFE (Immediately Invoked Function Expression) handles validation and data submission to avoid async function directly assigned to
onClick.Loading State:
ButtonLoadingcomponent receivesloadingprop to visually indicate when the API request is in progress.Selective Validation:
GeneralSavingButtonvalidates only the "name" field, whileSavingButtonvalidates the entire form.Payload Construction: Both include the knowledge base ID (
kb_id) from route params and additional form values, withSavingButtonexplicitly removing theavatarfrom submitted values.Localization: Button text is localized via the
tfunction fromuseTranslation.
Interaction with Other Parts of the System
useUpdateKnowledgeHook: This custom hook provides the mutation functionsaveKnowledgeConfigurationthat likely handles API requests to update knowledge base configurations.useFormContextHook: Hooks into the React Hook Form context, enabling these buttons to interact with form states and validation logic.useParamsfrom Umi: Extracts route parameters (idaskb_id) indicating which knowledge base is being updated.ButtonLoadingComponent: A UI component that encapsulates a button with integrated loading spinner and disabled state during API calls.Internationalization: The buttons use translation keys to display localized text.
The file acts as a bridge between the UI form and backend API, encapsulating submission logic and user feedback via loading states.
Mermaid Component Diagram
componentDiagram
component "GeneralSavingButton" {
+useFormContext()
+useUpdateKnowledge()
+useParams()
+useTranslation()
+ButtonLoading (loading, onClick)
}
component "SavingButton" {
+useFormContext()
+useUpdateKnowledge()
+useParams()
+useTranslation()
+ButtonLoading (loading, onClick)
}
component "useUpdateKnowledge" <<hook>>
component "useFormContext" <<hook>>
component "useParams" <<hook>>
component "useTranslation" <<hook>>
component "ButtonLoading" <<ui>>
GeneralSavingButton --> useFormContext
GeneralSavingButton --> useUpdateKnowledge
GeneralSavingButton --> useParams
GeneralSavingButton --> useTranslation
GeneralSavingButton --> ButtonLoading
SavingButton --> useFormContext
SavingButton --> useUpdateKnowledge
SavingButton --> useParams
SavingButton --> useTranslation
SavingButton --> ButtonLoading
Summary
The saving-button.tsx file provides two specialized save buttons for knowledge base configuration forms. They are deeply integrated with form validation, async API calls, and UI feedback mechanisms, serving as essential interactive components for saving user input in this application domain. Their design emphasizes modularity, reuse, and clear separation of concerns between UI, form management, and backend communication.