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

fix(react-components): search hook to consider data model version #4773

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ function setDefaultConfigOnNewHandlers(
modelHandlers: ModelLayerHandlers,
defaultLayersConfig: DefaultLayersConfiguration | undefined
): void {
const containsCadModel = newHandlers.cadHandlers.length > 0;
const containsPointCloudModel = newHandlers.pointCloudHandlers.length > 0;
newHandlers.cadHandlers.forEach((newHandler) => {
if (!modelHandlers.cadHandlers.some((oldHandler) => oldHandler.isSame(newHandler))) {
newHandler.setVisibility(defaultLayersConfig?.cad ?? true);
Expand All @@ -139,13 +141,17 @@ function setDefaultConfigOnNewHandlers(

newHandlers.pointCloudHandlers.forEach((newHandler) => {
if (!modelHandlers.pointCloudHandlers.some((oldHandler) => oldHandler.isSame(newHandler))) {
newHandler.setVisibility(defaultLayersConfig?.pointcloud ?? true);
newHandler.setVisibility(containsCadModel ? defaultLayersConfig?.pointcloud ?? true : true);
}
});

newHandlers.image360Handlers.forEach((newHandler) => {
if (!modelHandlers.image360Handlers.some((oldHandler) => oldHandler.isSame(newHandler))) {
newHandler.setVisibility(defaultLayersConfig?.image360 ?? true);
if (!containsCadModel && !containsPointCloudModel) {
newHandler.setVisibility(true);
} else {
newHandler.setVisibility(defaultLayersConfig?.image360 ?? true);
}
}
});
}
2 changes: 1 addition & 1 deletion react-components/src/data-providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*/

export type { FdmInstanceWithView, InstanceReference, AssetInstanceReference } from './types';
export type { Source, DmsUniqueIdentifier } from './FdmSDK';
export type { Source, DmsUniqueIdentifier, SimpleSource } from './FdmSDK';
53 changes: 11 additions & 42 deletions react-components/src/query/useSearchMappedEquipmentFDM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import {
type NodeItem,
type FdmSDK,
type Source,
type DmsUniqueIdentifier,
type InstanceFilter
type InstanceFilter,
type SimpleSource
} from '../data-providers/FdmSDK';
import { useFdm3dDataProvider, useFdmSdk } from '../components/RevealCanvas/SDKProvider';
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { type AddModelOptions } from '@cognite/reveal';
import { isEqual, uniq, chunk } from 'lodash';
import { type Fdm3dDataProvider } from '../data-providers/Fdm3dDataProvider';
import { removeEmptyProperties } from '../utilities/removeEmptyProperties';
import { isDefined } from '../utilities/isDefined';

export type InstancesWithView = { view: Source; instances: NodeItem[] };

export const useSearchMappedEquipmentFDM = (
query: string,
viewsToSearch: DmsUniqueIdentifier[],
viewsToSearch: SimpleSource[],
models: AddModelOptions[],
instancesFilter: InstanceFilter | undefined,
limit: number = 100
Expand Down Expand Up @@ -53,7 +52,7 @@ export const useSearchMappedEquipmentFDM = (
if (models.length === 0) {
return [];
}
const sources = await createSourcesFromViews(viewsToSearch, fdmSdk);
const sources = createSourcesFromViews(viewsToSearch);
const chunkedSources = chunk(sources, 10);
if (chunkedSources.length === 0) {
chunkedSources.push([]);
Expand Down Expand Up @@ -127,15 +126,14 @@ const searchNodesWithViewsAndModels = async (

export const useAllMappedEquipmentFDM = (
models: AddModelOptions[],
viewsToSearch: DmsUniqueIdentifier[]
viewsToSearch: SimpleSource[]
): UseQueryResult<NodeItem[]> => {
const fdmSdk = useFdmSdk();
const fdmDataProvider = useFdm3dDataProvider();

return useQuery({
queryKey: ['reveal', 'react-components', 'all-mapped-equipment-fdm', viewsToSearch, models],
queryFn: async () => {
const viewSources = await createSourcesFromViews(viewsToSearch, fdmSdk);
const viewSources = createSourcesFromViews(viewsToSearch);

return await fdmDataProvider.listAllMappedFdmNodes(models, viewSources, undefined);
},
Expand Down Expand Up @@ -189,38 +187,9 @@ function convertQueryNodeItemsToSearchResultsWithViews(
}, []);
}

async function createSourcesFromViews(
viewsToSearch: DmsUniqueIdentifier[],
fdmSdk: FdmSDK
): Promise<Source[]> {
try {
const dataModelResult = await fdmSdk.listDataModels();
const viewToVersionMap = new Map<string, string>(
dataModelResult.items.flatMap((dataModel: { views: Source[] }) => {
return dataModel.views.map(
(view: Source) => [`${view.space}/${view.externalId}`, view.version] as const
);
})
);

return viewsToSearch
.map((view) => {
const version = viewToVersionMap.get(`${view.space}/${view.externalId}`);
if (version === undefined) {
console.error(
`Could not find version for view with space/externalId ${view.space}/${view.externalId}`
);
return undefined;
}
return {
...view,
type: 'view' as const,
version
};
})
.filter(isDefined);
} catch (e) {
console.error('Error when fetching sources from views', e);
throw e;
}
function createSourcesFromViews(viewsToSearch: SimpleSource[]): Source[] {
return viewsToSearch.map((view) => ({
...view,
type: 'view'
}));
}
6 changes: 3 additions & 3 deletions react-components/stories/SearchHooks.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import { is360ImageAddOptions } from '../src/components/Reveal3DResources/typeGu
const queryClient = new QueryClient();
const sdk = createSdkByUrlToken();
const viewsToSearch = [
{ externalId: 'Equipment', space: 'fdx-boys' },
{ externalId: 'WorkOrderMultiple', space: 'fdx-boys' },
{ externalId: 'WorkOrderSingle', space: 'fdx-boys' }
{ externalId: 'Equipment', space: 'fdx-boys', version: 'e040583320d31a' },
{ externalId: 'WorkOrderMultiple', space: 'fdx-boys', version: 'e040583320d31a' },
{ externalId: 'WorkOrderSingle', space: 'fdx-boys', version: 'e040583320d31a' }
];

type Equipment = {
Expand Down
Loading