From b657d38de9a1cb6dbb45c3bf878330e3556242af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 15 Jul 2024 16:33:49 +0200 Subject: [PATCH] chore: remove 'nodeIds' support and add type guards --- .../src/components/CadModelContainer/types.ts | 16 ++++++++++++++-- .../useApplyCadModelStyling.tsx | 17 ++++++++++++----- .../src/components/Reveal3DResources/types.ts | 8 ++++++-- .../useCalculateCadStyling.tsx | 4 ++-- react-components/src/index.ts | 2 +- .../stories/Reveal3DResources.stories.tsx | 3 +-- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/react-components/src/components/CadModelContainer/types.ts b/react-components/src/components/CadModelContainer/types.ts index 39d7ab064d3..2bb25278356 100644 --- a/react-components/src/components/CadModelContainer/types.ts +++ b/react-components/src/components/CadModelContainer/types.ts @@ -13,9 +13,21 @@ export type TreeIndexStylingGroup = { style?: NodeAppearance; }; -export type CadStyleGroup = NodeStylingGroup | TreeIndexStylingGroup; +export type CadStylingGroup = NodeStylingGroup | TreeIndexStylingGroup; export type CadModelStyling = { defaultStyle?: NodeAppearance; - groups?: CadStyleGroup[]; + groups?: CadStylingGroup[]; }; + +export function isNodeStylingGroup( + stylingGroup: CadStylingGroup +): stylingGroup is NodeStylingGroup { + return 'nodeIds' in stylingGroup; +} + +export function isTreeIndexStylingGroup( + stylingGroup: CadStylingGroup +): stylingGroup is TreeIndexStylingGroup { + return 'treeIndexSet' in stylingGroup; +} diff --git a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx index 3bbbb0ac0e4..bcb61d3b007 100644 --- a/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx +++ b/react-components/src/components/CadModelContainer/useApplyCadModelStyling.tsx @@ -15,7 +15,14 @@ import { type CogniteClient } from '@cognite/sdk'; import { isEqual } from 'lodash'; import { useReveal } from '../RevealCanvas/ViewerContext'; import { modelExists } from '../../utilities/modelExists'; -import { type CadModelStyling, type NodeStylingGroup, type TreeIndexStylingGroup } from './types'; +import { + isNodeStylingGroup, + isTreeIndexStylingGroup, + type CadModelStyling, + type NodeStylingGroup, + type TreeIndexStylingGroup +} from './types'; +import { assertNever } from '../../utilities/assertNever'; export const useApplyCadModelStyling = ( model?: CogniteCadModel, @@ -57,15 +64,15 @@ async function applyStyling( if (stylingGroup.style === undefined) continue; - if ('treeIndexSet' in stylingGroup) { + if (isTreeIndexStylingGroup(stylingGroup)) { const nodes = new TreeIndexNodeCollection(stylingGroup.treeIndexSet); model.assignStyledNodeCollection(nodes, stylingGroup.style); - } - - if ('nodeIds' in stylingGroup) { + } else if (isNodeStylingGroup(stylingGroup)) { const nodes = new NodeIdNodeCollection(sdk, model); await nodes.executeFilter(stylingGroup.nodeIds); model.assignStyledNodeCollection(nodes, stylingGroup.style); + } else { + assertNever(stylingGroup); } } diff --git a/react-components/src/components/Reveal3DResources/types.ts b/react-components/src/components/Reveal3DResources/types.ts index e37211cdb38..5fe66ba3c48 100644 --- a/react-components/src/components/Reveal3DResources/types.ts +++ b/react-components/src/components/Reveal3DResources/types.ts @@ -11,7 +11,7 @@ import { import { type Matrix4 } from 'three'; import { type DmsUniqueIdentifier, type Source } from '../../utilities/FdmSDK'; import { type CogniteInternalId, type Node3D } from '@cognite/sdk'; -import { type CadStyleGroup } from '../CadModelContainer/types'; +import { type TreeIndexStylingGroup } from '../CadModelContainer/types'; export type AddImage360CollectionOptions = | AddImage360CollectionEventsOptions @@ -68,7 +68,11 @@ export type AddPointCloudResourceOptions = Add3dResourceOptions & { }; export type AddCadResourceOptions = AddModelOptions & { transform?: Matrix4 } & { - styling?: { default?: NodeAppearance; mapped?: NodeAppearance; nodeGroups?: CadStyleGroup[] }; + styling?: { + default?: NodeAppearance; + mapped?: NodeAppearance; + nodeGroups?: TreeIndexStylingGroup[]; + }; }; export type TypedReveal3DModel = CadModelOptions | PointCloudModelOptions; diff --git a/react-components/src/components/Reveal3DResources/useCalculateCadStyling.tsx b/react-components/src/components/Reveal3DResources/useCalculateCadStyling.tsx index 4a3dd26d459..8fa743a5482 100644 --- a/react-components/src/components/Reveal3DResources/useCalculateCadStyling.tsx +++ b/react-components/src/components/Reveal3DResources/useCalculateCadStyling.tsx @@ -21,7 +21,7 @@ import { type ModelRevisionAssetNodesResult } from '../CacheProvider/types'; import { - type CadStyleGroup, + type CadStylingGroup, type NodeStylingGroup, type TreeIndexStylingGroup } from '../CadModelContainer/types'; @@ -55,7 +55,7 @@ type StyledModelWithMappingsFetched = { export type StyledModel = { model: CadModelOptions; - styleGroups: CadStyleGroup[]; + styleGroups: CadStylingGroup[]; }; export const useCalculateCadStyling = ( diff --git a/react-components/src/index.ts b/react-components/src/index.ts index be7c52657a3..5af7604702d 100644 --- a/react-components/src/index.ts +++ b/react-components/src/index.ts @@ -91,7 +91,7 @@ export { export { type CogniteCadModelProps } from './components/CadModelContainer/CadModelContainer'; export { type CadModelStyling, - type CadStyleGroup, + type CadStylingGroup, type TreeIndexStylingGroup, type NodeStylingGroup } from './components/CadModelContainer/types'; diff --git a/react-components/stories/Reveal3DResources.stories.tsx b/react-components/stories/Reveal3DResources.stories.tsx index 09770e685a7..7470a6dc956 100644 --- a/react-components/stories/Reveal3DResources.stories.tsx +++ b/react-components/stories/Reveal3DResources.stories.tsx @@ -28,8 +28,7 @@ export const Main: Story = { transform: new Matrix4().makeTranslation(40, 0, 0), styling: { nodeGroups: [ - { treeIndexSet: new IndexSet([2, 4, 6, 8]), style: { color: new Color('blue') } }, - { nodeIds: [7152264879809192, 7899489067916664], style: { visible: false } } + { treeIndexSet: new IndexSet([2, 4, 6, 8]), style: { color: new Color('blue') } } ] } },