All files / src/hooks/cache useFlowsheetRevisionRefresh.ts

83.33% Statements 30/36
100% Branches 10/10
37.5% Functions 3/8
100% Lines 26/26

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              103x                                   17x 187x 17x       1384x 1384x     1x     2420x       23619x 23619x   23619x 71x 23955x   23619x 11x 23955x   23619x 1x     1x 24291x   23619x 3x 3x                   24291x   336x         24963x    
import { useCallback } from "react";
import { useSearchParams } from "react-router-dom";
import { enhancedAPI } from "@/api/enhanceEndpoints.ts";
import { setCopiedObject } from "@/store/CopySlice.ts";
import { useAppDispatch } from "@/store/hooks.ts";
 
/** URL selections that identify records inside the replaceable working state. */
export const STATE_LOCAL_SEARCH_PARAMS = [
  "object",
  "parentGroup",
  "scenario",
  "economicsStudy",
  "economicsUnit",
  "solution",
  "henStream",
  "henNode",
  "hxGroup",
  "selType",
  "CurrentGroupID",
] as const;
 
/** Return a copy of the route parameters without identifiers invalidated by restore. */
export function removeStateLocalSearchParams(
  searchParams: URLSearchParams,
): URLSearchParams {
  const next = new URLSearchParams(searchParams);
  STATE_LOCAL_SEARCH_PARAMS.forEach((key) => next.delete(key));
  return next;
}
 
/** Clear route selections whose records were replaced by a restore. */
export function useClearStateLocalSearchParams() {
  const [, setSearchParams] = useSearchParams();
 
  return () => {
    setSearchParams((current) => removeStateLocalSearchParams(current), {
      replace: true,
    });
  };
}
 
/** Discard all client state tied to the replaced working aggregate. */
export function useFlowsheetRevisionRefresh() {
  const dispatch = useAppDispatch();
 
  const refreshRevisionList = useCallback(() => {
    dispatch(enhancedAPI.util.invalidateTags(["FlowsheetRevisions"]));
  }, [dispatch]);
 
  const resetRevisionTransientState = useCallback(() => {
    dispatch(setCopiedObject([]));
  }, [dispatch]);
 
  const refreshAfterRestore = useCallback(() => {
    resetRevisionTransientState();
    // Restore replaces the complete working aggregate. Resetting the generated
    // API cache avoids duplicating the backend ownership boundary in tag lists.
    dispatch(enhancedAPI.util.resetApiState());
  }, [dispatch, resetRevisionTransientState]);
 
  const refreshAfterPreview = useCallback(() => {
    resetRevisionTransientState();
    dispatch(
      enhancedAPI.util.invalidateTags([
        "Groups",
        "SimulationObjects",
        "Ports",
        "GraphicsObjects",
        "RecycleData",
        "ProcessPaths",
      ]),
    );
  }, [dispatch, resetRevisionTransientState]);
 
  return {
    refreshRevisionList,
    refreshAfterRestore,
    refreshAfterPreview,
    resetRevisionTransientState,
  };
}