All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar useMonitoringTables.ts

92.59% Statements 25/27
83.33% Branches 5/6
90.9% Functions 10/11
95.65% Lines 22/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                            8784x           8784x         8784x 8784x 8784x     8784x 1121x       8784x 1121x       8784x 1121x               8784x   4x 4x                               8784x   1x               8784x   3x                     8784x   2x                       8784x                   8784x                          
import { useCallback, useMemo } from "react";
import {
  PatchedMonitoringTable,
  useCoreMonitoringTablesCreateMutation,
  useCoreMonitoringTablesDestroyMutation,
  useCoreMonitoringTablesListQuery,
  useCoreMonitoringTablesPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useProjectId } from "@/hooks/project";
 
/**
 * Hook to manage multiple monitoring tables for a specific group.
 */
export function useMonitoringTables(groupId: number | undefined) {
  const flowsheetId = useProjectId();
 
  const {
    data: allTables = [],
    isLoading,
    isFetching,
  } = useCoreMonitoringTablesListQuery(
    { flowsheet: flowsheetId },
    { skip: !flowsheetId },
  );
 
  const [createMutation] = useCoreMonitoringTablesCreateMutation();
  const [updateMutation] = useCoreMonitoringTablesPartialUpdateMutation();
  const [destroyMutation] = useCoreMonitoringTablesDestroyMutation();
 
  // Filter tables for the current group
  const tables = useMemo(
    () => (groupId != null ? allTables.filter((t) => t.group === groupId) : []),
    [allTables, groupId],
  );
 
  const visibleTables = useMemo(
    () => tables.filter((t) => t.visible),
    [tables],
  );
 
  const hiddenTables = useMemo(
    () => tables.filter((t) => !t.visible),
    [tables],
  );
 
  /**
   * Create a new monitoring table with a given name.
   * New tables are visible and minimised by default.
   */
  const createTable = useCallback(
    async (name: string) => {
      Iif (!flowsheetId || groupId == null) return;
      return createMutation({
        monitoringTable: {
          flowsheet: flowsheetId,
          group: groupId,
          title: name,
          visible: true,
          minimised: true,
        },
      }).unwrap();
    },
    [flowsheetId, groupId, createMutation],
  );
 
  /**
   * Delete a monitoring table permanently
   */
  const deleteTable = useCallback(
    async (tableId: number) => {
      return destroyMutation({ id: tableId }).unwrap();
    },
    [destroyMutation],
  );
 
  /**
   * Update table's visibility
   */
  const setTableVisibility = useCallback(
    async (tableId: number, isVisible: boolean) => {
      return updateMutation({
        id: tableId,
        patchedMonitoringTable: { visible: isVisible },
      }).unwrap();
    },
    [updateMutation],
  );
 
  /**
   * Update table's name
   */
  const renameTable = useCallback(
    async (tableId: number, newName: string) => {
      return updateMutation({
        id: tableId,
        patchedMonitoringTable: { title: newName },
      }).unwrap();
    },
    [updateMutation],
  );
 
  /**
   * Generic partial update for any table fields
   * (position, size, minimised, selectedProperties, etc.)
   */
  const updateTable = useCallback(
    async (tableId: number, patch: PatchedMonitoringTable) => {
      return updateMutation({
        id: tableId,
        patchedMonitoringTable: patch,
      }).unwrap();
    },
    [updateMutation],
  );
 
  return {
    tables,
    visibleTables,
    hiddenTables,
    createTable,
    deleteTable,
    setTableVisibility,
    renameTable,
    updateTable,
    isLoading,
    isFetching,
  };
}