use-run-document.ts


Overview

The use-run-document.ts file defines a custom React hook named useHandleRunDocumentByIds which facilitates running or stopping the execution of a document by its ID. It leverages another custom hook, useRunDocument, to perform the actual document operation request.

This hook manages internal loading state specific to the given document ID, ensuring that concurrent operations on the same document are avoided. It provides an asynchronous function to trigger the run/stop process, optionally supporting deletion of the document during the operation.


Detailed Explanation

Hook: useHandleRunDocumentByIds

Purpose

useHandleRunDocumentByIds abstracts the logic for running or stopping a document workflow identified by a single document ID, with optional deletion. It encapsulates:

Signature

useHandleRunDocumentByIds(id: string): {
  handleRunDocumentByIds: (
    documentId: string,
    isRunning: boolean,
    shouldDelete?: boolean
  ) => Promise<void>;
  loading: boolean;
}

Parameters

Returns

An object containing:

handleRunDocumentByIds function parameters:

Behavior and Implementation Details

Usage Example

import React from 'react';
import { useHandleRunDocumentByIds } from './use-run-document';

const DocumentRunner: React.FC<{ docId: string }> = ({ docId }) => {
  const { handleRunDocumentByIds, loading } = useHandleRunDocumentByIds(docId);

  const runDocument = () => {
    handleRunDocumentByIds(docId, true);
  };

  const stopDocument = () => {
    handleRunDocumentByIds(docId, false, true);
  };

  return (
    <div>
      <button onClick={runDocument} disabled={loading}>
        Run Document
      </button>
      <button onClick={stopDocument} disabled={loading}>
        Stop & Delete Document
      </button>
      {loading && <p>Processing document {docId}...</p>}
    </div>
  );
};

Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
    class useHandleRunDocumentByIds {
        -currentId: string
        -isLoading: boolean
        +handleRunDocumentByIds(documentId: string, isRunning: boolean, shouldDelete?: boolean): Promise<void>
        +loading: boolean
    }
    useHandleRunDocumentByIds ..> useRunDocument : uses
    class useRunDocument {
        +runDocumentByIds(params: { documentIds: string[]; run: number; shouldDelete: boolean }): Promise<void>
        +loading: boolean
    }

Summary

This file is a focused utility hook that encapsulates concerns related to running/stopping documents by IDs while handling concurrent request prevention and loading state management differentiated by document ID.