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

70.9% Statements 39/55
54.16% Branches 13/24
55.55% Functions 5/9
93.33% Lines 28/30

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                        3235x 3235x     3235x     3235x 3380x 3371x     3235x 3235x 3235x       3235x   3235x   3235x           3235x 4x                   4015x         3235x 1x 3371x         3235x 3x       3371x         3235x 1x       3371x           3235x               3371x   3235x                     35585x    
import {
  PatchedMonitoringTable,
  useCoreMonitoringTablesCreateMutation,
  useCoreMonitoringTablesDestroyMutation,
  useCoreMonitoringTablesListQuery,
  useCoreMonitoringTablesPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useFlowsheetId } from "@/hooks/project";
 
/**
 * Hook to manage multiple monitoring tables for a specific group.
 */
export function useMonitoringTables(groupId: number | undefined) {
  const flowsheetId = useFlowsheetId();
 
  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 =
    groupId != null ? allTables.filter((t) => t.group === groupId) : [];
 
  const visibleTables = tables.filter((t) => t.visible);
 
  const hiddenTables = tables.filter((t) => !t.visible);
 
  /**
   * Create a new monitoring table with a given name.
   * New tables are visible and minimised by default.
   */
  const createTable = async (name: string) => {
    Iif (!flowsheetId || groupId == null) return;
    return createMutation({
      monitoringTable: {
        flowsheet: flowsheetId,
        group: groupId,
        title: name,
        visible: true,
        minimised: true,
      },
    }).unwrap();
  };
 
  /**
   * Delete a monitoring table permanently
   */
  const deleteTable = async (tableId: number) => {
    return destroyMutation({ id: tableId }).unwrap();
  };
 
  /**
   * Update table's visibility
   */
  const setTableVisibility = async (tableId: number, isVisible: boolean) => {
    return updateMutation({
      id: tableId,
      patchedMonitoringTable: { visible: isVisible },
    }).unwrap();
  };
 
  /**
   * Update table's name
   */
  const renameTable = async (tableId: number, newName: string) => {
    return updateMutation({
      id: tableId,
      patchedMonitoringTable: { title: newName },
    }).unwrap();
  };
 
  /**
   * Generic partial update for any table fields
   * (position, size, minimised, selectedProperties, etc.)
   */
  const updateTable = async (
    tableId: number,
    patch: PatchedMonitoringTable,
  ) => {
    return updateMutation({
      id: tableId,
      patchedMonitoringTable: patch,
    }).unwrap();
  };
 
  return {
    tables,
    visibleTables,
    hiddenTables,
    createTable,
    deleteTable,
    setTableVisibility,
    renameTable,
    updateTable,
    isLoading,
    isFetching,
  };
}