All files / src/hooks flowsheetAccess.ts

80% Statements 12/15
100% Branches 1/1
100% Functions 1/1
90% Lines 9/10

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                        216417x 216417x 216417x 216417x 248696x   216417x       216417x         226560x 216417x            
import { useSearchParams } from "react-router-dom";
import { useCoreFlowsheetsRetrieveQuery } from "@/api/apiStore.gen";
import {
  getRevisionReadIdentity,
  getRevisionRouteScopeFromUrl,
} from "@/api/flowsheetRevisionScope";
import { useFlowsheetId } from "@/hooks/project";
import {
  deriveRevisionPreviewAccess,
  getFlowsheetAccess,
} from "@/lib/flowsheetAccess";
 
export function useFlowsheetAccess() {
  const flowsheetId = useFlowsheetId();
  const [searchParams] = useSearchParams();
  const revisionScope = getRevisionRouteScopeFromUrl(
    `?${searchParams.toString()}`,
  );
  const revisionReadIdentity = getRevisionReadIdentity(revisionScope);
  // The generated endpoint ignores the extra identity field when building the
  // URL, while RTK Query uses it to move this persistent hook between Current
  // and revision cache entries without remounting the Projects panel.
  const { currentData: flowsheet } = useCoreFlowsheetsRetrieveQuery(
    {
      id: String(flowsheetId),
      revisionReadIdentity,
    } as unknown as Parameters<typeof useCoreFlowsheetsRetrieveQuery>[0],
    { skip: !flowsheetId },
  );
  return deriveRevisionPreviewAccess(
    getFlowsheetAccess(flowsheet),
    revisionScope.isPresent,
  );
}