use-delete-graph.ts

Overview

The use-delete-graph.ts file defines a custom React hook named useDeleteKnowledgeGraph. This hook encapsulates the logic required to delete a knowledge graph entity from the system and then navigate the user back to the relevant dataset page after successful deletion. It integrates network or backend operations (via useRemoveKnowledgeGraph), navigation controls (via useNavigatePage), and URL parameter extraction (via useParams from umi).

This hook is intended to be used within React components where knowledge graph deletion functionality is needed, providing a clean and reusable interface for triggering the deletion and handling post-deletion navigation.


Detailed Explanation

useDeleteKnowledgeGraph()

Description

A custom React hook that manages the deletion of a knowledge graph and subsequent navigation to the dataset view page. It returns a handler function to perform the deletion and a loading state indicator.

Returns

An object containing:

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>
  );
}

Parameters

Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Hook Flowchart

flowchart TD
    A[useDeleteKnowledgeGraph Hook] --> B[useRemoveKnowledgeGraph Hook]
    A --> C[useNavigatePage Hook]
    A --> D[useParams (from umi)]
    B --> E[removeKnowledgeGraph() - delete API call]
    C --> F[navigateToDataset(id) - navigation function]

    subgraph handleDeleteKnowledgeGraph
        E --> G{ret === 0?}
        G -- Yes --> F
        G -- No --> H[No navigation]
    end

    A --> handleDeleteKnowledgeGraph
    A --> loading[loading state from useRemoveKnowledgeGraph]

Summary

use-delete-graph.ts provides a clean encapsulation of the deletion and navigation workflow for knowledge graphs within the application. It abstracts the complexity of API interaction and route navigation, exposing a simple API to components that require delete functionality. This promotes code reuse, separation of concerns, and consistency in user experience after deletion actions.