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

handleDeleteKnowledgeGraph

() => Promise

An asynchronous function that initiates the deletion of the knowledge graph.

loading

boolean

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


Interaction with Other Parts of the System

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.