Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw error if FDM config not provided when using FDM #3516

Merged
merged 11 commits into from
Jul 31, 2023
40 changes: 16 additions & 24 deletions react-components/src/hooks/useFdmAssetMappings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,31 @@ import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { type FdmAssetMappingsConfig, type ThreeDModelMappings } from './types';
import { DEFAULT_QUERY_STALE_TIME } from '../utilities/constants';

const DefaultFdmConfig: FdmAssetMappingsConfig = {
source: {
space: 'fdm-3d-test-savelii',
version: '1',
type: 'view',
externalId: 'CDF_3D_Connection_Data'
},
assetFdmSpace: 'bark-corporation'
};

/**
* This hook fetches the list of FDM asset mappings for the given external ids
*/
export const useFdmAssetMappings = (
fdmAssetExternalIds: CogniteExternalId[],
fdmConfig: FdmAssetMappingsConfig = DefaultFdmConfig
fdmConfig?: FdmAssetMappingsConfig
): UseQueryResult<ThreeDModelMappings[]> => {
const fdmSdk = useFdmSdk();

const fdmAssetMappingFilter = {
in: {
property: ['edge', 'startNode'],
values: fdmAssetExternalIds.map((externalId) => ({
space: fdmConfig.assetFdmSpace,
externalId
}))
}
};

return useQuery(
['reveal', 'react-components', fdmAssetExternalIds],
async () => {
if (fdmAssetExternalIds?.length === 0) return [];
if (fdmConfig === undefined)
throw Error('FDM config must be defined when using FDM asset mappings');

const fdmAssetMappingFilter = {
in: {
property: ['edge', 'startNode'],
values: fdmAssetExternalIds.map((externalId) => ({
space: fdmConfig.assetFdmSpace,
externalId
}))
}
};

const instances = await fdmSdk.filterInstances(
fdmAssetMappingFilter,
Expand All @@ -56,7 +48,7 @@ export const useFdmAssetMappings = (
];

const modelId = Number.parseInt(instance.endNode.externalId.slice(9));
const revisionId = mappingProperty.RevisionId;
const revisionId = mappingProperty.revisionId;

const isAdded = modelMappingsTemp.some(
(mapping) => mapping.modelId === modelId && mapping.revisionId === revisionId
Expand All @@ -67,7 +59,7 @@ export const useFdmAssetMappings = (
modelId,
revisionId,
mappings: [
{ nodeId: mappingProperty.NodeId, externalId: instance.startNode.externalId }
{ nodeId: mappingProperty.revisionNodeId, externalId: instance.startNode.externalId }
]
});
} else {
Expand All @@ -76,7 +68,7 @@ export const useFdmAssetMappings = (
);

modelMapping?.mappings.push({
nodeId: mappingProperty.NodeId,
nodeId: mappingProperty.revisionNodeId,
externalId: instance.startNode.externalId
});
}
Expand Down
Loading