use-delete-graph.ts
Overview
The use-delete-graph.ts file defines a custom React hook, useDeleteKnowledgeGraph, designed to facilitate the deletion of a knowledge graph within a knowledge base management application. This hook encapsulates the logic required to trigger the deletion process, manage the loading state, and navigate the user interface upon successful deletion.
This hook leverages other custom hooks (useRemoveKnowledgeGraph, useKnowledgeBaseId) and React Router's navigation (useNavigate) to provide a clean and reusable interface for deleting a knowledge graph and redirecting users accordingly.
Detailed Explanation
useDeleteKnowledgeGraph()
Custom React hook that provides functionality to delete a knowledge graph associated with the current knowledge base and handle post-deletion navigation.
Returns
An object containing:
Property | Type | Description |
|---|---|---|
| An asynchronous function that initiates the deletion of the knowledge graph. | |
|
| A boolean flag indicating if the deletion process is currently in progress (loading state). |
Usage Example
import React from 'react';
import { useDeleteKnowledgeGraph } from './use-delete-graph';
function DeleteGraphButton() {
const { handleDeleteKnowledgeGraph, loading } = useDeleteKnowledgeGraph();
return (
<button onClick={handleDeleteKnowledgeGraph} disabled={loading}>
{loading ? 'Deleting...' : 'Delete Knowledge Graph'}
</button>
);
}
Behavior and Implementation Details
Dependencies:
useRemoveKnowledgeGraph: Custom hook that provides theremoveKnowledgeGraphfunction and its loading state.useKnowledgeBaseId: Custom hook that returns the current knowledge base identifier (likely from context or route parameters).useNavigatefromumi: Provides navigation capabilities to programmatically change routes.
Process:
When
handleDeleteKnowledgeGraphis called:It calls the asynchronous
removeKnowledgeGraphfunction to delete the knowledge graph.
After the deletion call completes, it checks the returned result:
If the result is
0, which presumably indicates success, it navigates the user back to the dataset page for the current knowledge base, using the URL pattern:/knowledge/dataset?id=${knowledgeBaseId}.
The hook exposes the
loadingstate to indicate whether the deletion is in progress, useful for UI feedback.
Use of
useCallback:The deletion handler is memoized with
useCallbackto prevent unnecessary re-creations unless its dependencies change (knowledgeBaseId,navigate,removeKnowledgeGraph).
Interaction with Other Parts of the System
useRemoveKnowledgeGraph
This hook is responsible for the actual deletion logic, likely making an API call or dispatching an action to remove the knowledge graph from the backend or state management system. It provides the deletion function and loading status.useKnowledgeBaseId
Provides the current knowledge base identifier, which is essential for routing after deletion and possibly for scoping the deletion operation.useNavigate(fromumi)
Enables navigation to another route upon successful deletion, ensuring the user is redirected to a relevant page (/knowledge/dataset) with the correct knowledge base context.
The useDeleteKnowledgeGraph hook acts as a coordination layer, combining these pieces into a simple interface for components to use.
Mermaid Diagram
flowchart TD
A[useDeleteKnowledgeGraph Hook] --> B[useRemoveKnowledgeGraph Hook]
A --> C[useKnowledgeBaseId Hook]
A --> D[useNavigate() from umi]
B -- provides --> E[removeKnowledgeGraph() Function]
B -- provides --> F[loading State]
A -- exposes --> G[handleDeleteKnowledgeGraph() Function]
A -- exposes --> F
G -->|calls| E
G -->|on success (ret === 0)| D
D -->|navigate to| H[/knowledge/dataset?id=knowledgeBaseId/]
Summary
The use-delete-graph.ts file encapsulates the deletion logic of a knowledge graph in a reusable React hook. It abstracts away the details of removal, loading state management, and navigation, providing a simple interface for UI components to trigger graph deletion and respond accordingly. This improves code modularity, testability, and maintainability within the knowledge base management system.