Skip to content

Commit

Permalink
chore: more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Aug 16, 2023
1 parent 29e43f5 commit 6ade95a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 40 deletions.
23 changes: 5 additions & 18 deletions react-components/src/components/NodeCacheProvider/FdmNodeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
SYSTEM_3D_EDGE_SOURCE,
SYSTEM_SPACE_3D_SCHEMA
} from '../../utilities/globalDataModels';
import {
type ModelRevisionKey,
type ModelRevisionToEdgeMap
} from '../../hooks/useMappedEquipmentBy3DModelsList';

export type ModelRevisionKey = `${number}-${number}`;
export type ModelRevisionToEdgeMap = Map<ModelRevisionKey, FdmEdgeWithNode[]>;

import { partition } from 'lodash';

import assert from 'assert';
import { fetchNodesForNodeIds } from './requests';

export class FdmNodeCache {
private readonly _revisionNodeCaches = new Map<RevisionKey, RevisionFdmNodeCache>();
Expand Down Expand Up @@ -235,7 +235,7 @@ async function createModelNodeIdToNodeMap(revisionToNodeIdsMap: Map<RevisionId,
const modelId = modelRevisionIds.find((p) => p.revisionId === revisionId)?.modelId;
assert(modelId !== undefined);

const nodes = await nodeIdsToNodes(modelId, revisionId, nodeIds, cdfClient);
const nodes = await fetchNodesForNodeIds(modelId, revisionId, nodeIds, cdfClient);
nodeIds.forEach((e, ind) => {
const modelNodeIdKey = `${modelId}-${revisionId}-${e}` as const;
revisionNodeIdToNode.set(modelNodeIdKey, nodes[ind]);
Expand All @@ -262,16 +262,3 @@ function createRevisionToNodeIdMap(edges: Array<FdmCadEdge>): Map<RevisionId, No
return revisionNodeIdMap;
}, new Map<RevisionId, NodeId[]>());
}

async function nodeIdsToNodes(
modelId: number,
revisionId: number,
nodeIds: number[],
cogniteClient: CogniteClient
): Promise<Node3D[]> {
return await cogniteClient.revisions3D.retrieve3DNodes(
modelId,
revisionId,
nodeIds.map((id) => ({ id }))
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*/

import { type ReactElement, type ReactNode, createContext, useContext, useMemo } from 'react';
import { FdmNodeCache } from './FdmNodeCache';
import { type ModelRevisionToEdgeMap } from '../../hooks/useMappedEquipmentBy3DModelsList';
import { FdmNodeCache, ModelRevisionToEdgeMap } from './FdmNodeCache';
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { useFdmSdk, useSDK } from '../RevealContainer/SDKProvider';
import { type Fdm3dNodeData } from './types';
Expand All @@ -17,7 +16,7 @@ export type FdmNodeCacheContent = {

export const FdmNodeCacheContext = createContext<FdmNodeCacheContent | undefined>(undefined);

export const useGetAllExternalIds = (
export const useMappedEdgesForRevisions = (
modelRevisionIds: Array<{ modelId: number; revisionId: number }>,
enabled: boolean
): UseQueryResult<ModelRevisionToEdgeMap> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { type CogniteClient, type Node3D } from '@cognite/sdk';
import { type FdmSDK } from '../../utilities/FdmSDK';
import { type TreeIndex } from './FdmNodeCache';
import { type TreeIndex } from './types';

import {
fetchAncestorNodesForTreeIndex,
Expand Down
13 changes: 13 additions & 0 deletions react-components/src/components/NodeCacheProvider/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,16 @@ export async function treeIndexesToNodeIds(
throw Error(`treeIndex-nodeId translation failed for treeIndexes ${treeIndexes.join(',')}`);
}
}

export async function fetchNodesForNodeIds(
modelId: number,
revisionId: number,
nodeIds: number[],
cogniteClient: CogniteClient
): Promise<Node3D[]> {
return await cogniteClient.revisions3D.retrieve3DNodes(
modelId,
revisionId,
nodeIds.map((id) => ({ id }))
);
}
4 changes: 2 additions & 2 deletions react-components/src/hooks/useCalculateModelsStyling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
type NodeStylingGroup,
type CadModelStyling
} from '../components/CadModelContainer/CadModelContainer';
import { useMappedEquipmentByRevisionList } from './useMappedEquipmentBy3DModelsList';
import { type InModel3dEdgeProperties } from '../utilities/globalDataModels';
import { type EdgeItem } from '../utilities/FdmSDK';
import { type NodeAppearance } from '@cognite/reveal';
import { type ThreeDModelMappings } from './types';
import { type CogniteExternalId, type CogniteInternalId } from '@cognite/sdk';
import { useFdmAssetMappings } from './useFdmAssetMappings';
import { useEffect, useMemo } from 'react';
import { useMappedEdgesForRevisions } from '../components/NodeCacheProvider/NodeCacheProvider';

type ModelStyleGroup = {
model: TypedReveal3DModel;
Expand All @@ -43,7 +43,7 @@ function useCalculateMappedStyling(models: TypedReveal3DModel[]): ModelStyleGrou
(model) => model.styling?.mapped !== undefined
);
const shouldFetchAllMappedEquipment = modelsRevisionsWithMappedEquipment.length > 0;
const { data: mappedEquipmentEdges } = useMappedEquipmentByRevisionList(
const { data: mappedEquipmentEdges } = useMappedEdgesForRevisions(
modelsRevisionsWithMappedEquipment,
shouldFetchAllMappedEquipment
);
Expand Down
16 changes: 0 additions & 16 deletions react-components/src/hooks/useMappedEquipmentBy3DModelsList.tsx

This file was deleted.

0 comments on commit 6ade95a

Please sign in to comment.