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(react-components): clicked node hook #3557

Merged
merged 27 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ba0494b
feat: useClickedNode hook
haakonflatval-cognite Aug 7, 2023
578a99c
feat(WIP): ClickedNode context
haakonflatval-cognite Aug 7, 2023
16a1f06
chore: lint fix
haakonflatval-cognite Aug 7, 2023
9583185
refactor: make data query function into a hook
haakonflatval-cognite Aug 8, 2023
6732615
feat: introduce `useClickedNode` hook instead of higher-order component
haakonflatval-cognite Aug 8, 2023
ad72a6f
fix: correct query hash keys
haakonflatval-cognite Aug 8, 2023
2592d97
chore: make sure story hook doesn't do unnecessary updates
haakonflatval-cognite Aug 8, 2023
007a7d6
Merge branch 'master' into hflatval/clicked-node-hook
haakonflatval-cognite Aug 8, 2023
d4bb156
chore: lint fix
haakonflatval-cognite Aug 8, 2023
7e4aeb2
fix: return promise as promised
haakonflatval-cognite Aug 8, 2023
765a791
refactor: refactor hook to not deal with intersection logic
haakonflatval-cognite Aug 8, 2023
e461cab
chore: lint fix
haakonflatval-cognite Aug 9, 2023
291ad0e
fix: build fix
haakonflatval-cognite Aug 9, 2023
bcbf697
chore: remove unnecessary check
haakonflatval-cognite Aug 9, 2023
0b6008d
chore: remove unused comments
haakonflatval-cognite Aug 9, 2023
53e0d05
refactor: rename to useClickedNodeData
haakonflatval-cognite Aug 14, 2023
c09d3e0
fix: avoid stringification of circular JSON struct
haakonflatval-cognite Aug 14, 2023
a17c851
refactor: combine all react queries into one
haakonflatval-cognite Aug 14, 2023
8831e63
chore: Remove new story
haakonflatval-cognite Aug 14, 2023
c6a294a
Merge branch 'master' into hflatval/clicked-node-hook
haakonflatval-cognite Aug 14, 2023
4abbbf2
fix: correct use of space names and properties
haakonflatval-cognite Aug 14, 2023
a0b9c99
chore: lint fix
haakonflatval-cognite Aug 14, 2023
576d10f
Merge branch 'master' into hflatval/clicked-node-hook
haakonflatval-cognite Aug 15, 2023
b3daabb
fix: add zoom-to-clicked in HighlightNode
haakonflatval-cognite Aug 15, 2023
8893ab6
chore: lint fix
haakonflatval-cognite Aug 15, 2023
201fbb1
chore: remove old `queryMappedData` file
haakonflatval-cognite Aug 16, 2023
bb2550f
Merge master into hflatval/clicked-node-hook
cognite-bulldozer[bot] Aug 16, 2023
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 @@ -2,7 +2,7 @@
* Copyright 2023 Cognite AS
*/
import { useRef, type ReactElement, useState, useEffect } from 'react';
import { type Cognite3DViewer, type PointerEventData } from '@cognite/reveal';
import { type Cognite3DViewer } from '@cognite/reveal';
import { CadModelContainer, type CadModelStyling } from '../CadModelContainer/CadModelContainer';
import {
PointCloudContainer,
Expand All @@ -18,9 +18,8 @@ import {
type Reveal3DResourcesProps,
type DefaultResourceStyling
} from './types';
import { queryMappedData } from './queryMappedData';
import { useFdmSdk, useSDK } from '../RevealContainer/SDKProvider';
import { useCalculateModelsStyling } from '../../hooks/useCalculateModelsStyling';
import { useClickedNodeData } from '../..';

export const Reveal3DResources = ({
resources,
Expand All @@ -32,8 +31,6 @@ export const Reveal3DResources = ({
const [reveal3DModels, setReveal3DModels] = useState<TypedReveal3DModel[]>([]);

const viewer = useReveal();
const fdmSdk = useFdmSdk();
const client = useSDK();
const numModelsLoaded = useRef(0);

useEffect(() => {
Expand All @@ -49,20 +46,13 @@ export const Reveal3DResources = ({
}, [resources, viewer]);

const reveal3DModelsStyling = useCalculateModelsStyling(reveal3DModels, instanceStyling ?? []);
const clickedNodeData = useClickedNodeData();

useEffect(() => {
const callback = (event: PointerEventData): void => {
if (onNodeClick === undefined) return;
const data = queryMappedData(viewer, client, fdmSdk, event);
onNodeClick(data);
};

viewer.on('click', callback);

return () => {
viewer.off('click', callback);
};
}, [viewer, client, fdmSdk, onNodeClick]);
if (clickedNodeData !== undefined) {
onNodeClick?.(Promise.resolve(clickedNodeData));
}
}, [clickedNodeData, onNodeClick]);

const image360CollectionAddOptions = resources.filter(
(resource): resource is AddImageCollection360Options =>
Expand Down
149 changes: 0 additions & 149 deletions react-components/src/components/Reveal3DResources/queryMappedData.ts

This file was deleted.

7 changes: 3 additions & 4 deletions react-components/src/components/Reveal3DResources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/

import {
type NodeAppearance,
type AddModelOptions,
type SupportedModelTypes,
type CadIntersection,
type NodeAppearance
type SupportedModelTypes
} from '@cognite/reveal';

import { type Matrix4 } from 'three';
import { type DmsUniqueIdentifier, type Source } from '../../utilities/FdmSDK';
import { type Node3D } from '@cognite/sdk/dist/src';
Expand All @@ -29,7 +29,6 @@ export type NodeDataResult = {
nodeExternalId: string;
view: Source;
cadNode: Node3D;
intersection: CadIntersection;
};

export type FdmAssetStylingGroup = {
Expand Down
45 changes: 45 additions & 0 deletions react-components/src/hooks/useClickedNode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* Copyright 2023 Cognite AS
*/

import { type CadIntersection, type PointerEventData } from '@cognite/reveal';
import { useReveal, type NodeDataResult } from '../';
import { useEffect, useState } from 'react';
import { useNodeMappedData } from './useNodeMappedData';

export type ClickedNodeData = NodeDataResult & {
intersection: CadIntersection;
};

export const useClickedNodeData = (): ClickedNodeData | undefined => {
const viewer = useReveal();

const [cadIntersection, setCadIntersection] = useState<CadIntersection | undefined>(undefined);

useEffect(() => {
const callback = (event: PointerEventData): void => {
void (async () => {
const intersection = await viewer.getIntersectionFromPixel(event.offsetX, event.offsetY);

if (intersection === null || intersection.type !== 'cad') {
return;
}

setCadIntersection(intersection);
})();
};

viewer.on('click', callback);

return () => {
viewer.off('click', callback);
};
}, [viewer]);

const nodeData = useNodeMappedData(cadIntersection?.treeIndex, cadIntersection?.model);

if (nodeData === undefined || cadIntersection === undefined) {
return undefined;
}
Savokr marked this conversation as resolved.
Show resolved Hide resolved
return { intersection: cadIntersection, ...nodeData };
};
Loading
Loading