-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): add Reveal container and container for Cognit…
…eCadModel (#3419) * chore: restructure folder layout * asd * asd * feat: add RevealContainer * fix: update transformation when updating prop * fix: conflicts * fix: update storybook setup * fix: update reveal version * fix: QA --------- Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e6130fa
commit f8581af
Showing
17 changed files
with
309 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
react-components/src/components/CogniteCadModelContainer/CogniteCadModelContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { AddModelOptions, CogniteCadModel } from "@cognite/reveal"; | ||
import { useReveal } from "../RevealContainer/RevealContext"; | ||
import { Matrix4 } from "three"; | ||
|
||
type Cognite3dModelProps = { | ||
addModelOptions: AddModelOptions; | ||
transform?: THREE.Matrix4; | ||
}; | ||
|
||
export default function CogniteCadModelContainer({ | ||
addModelOptions, | ||
transform, | ||
}: Cognite3dModelProps) { | ||
const modelRef = useRef<CogniteCadModel>(); | ||
const viewer = useReveal(); | ||
const { modelId, revisionId } = addModelOptions; | ||
|
||
useEffect(() => { | ||
addModel(modelId, revisionId, transform).catch(console.error); | ||
return () => { | ||
if ( | ||
modelRef.current === undefined || | ||
!viewer.models.includes(modelRef.current) | ||
) { | ||
return; | ||
} | ||
viewer.removeModel(modelRef.current); | ||
modelRef.current = undefined; | ||
}; | ||
}, [addModelOptions]); | ||
|
||
useEffect(() => { | ||
if (modelRef.current === undefined || transform === undefined) return; | ||
modelRef.current.setModelTransformation(transform); | ||
}, [transform]); | ||
|
||
return <></>; | ||
|
||
async function addModel( | ||
modelId: number, | ||
revisionId: number, | ||
transform?: Matrix4 | ||
) { | ||
const cadModel = await viewer.addCadModel({ modelId, revisionId }); | ||
if (transform !== undefined) { | ||
cadModel.setModelTransformation(transform); | ||
} | ||
modelRef.current = cadModel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
react-components/src/components/RevealContainer/RevealContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { CogniteClient } from "@cognite/sdk"; | ||
import { useEffect, useRef, ReactNode, useState, useMemo } from "react"; | ||
import { Cognite3DViewer } from '@cognite/reveal'; | ||
import { RevealContext } from "./RevealContext"; | ||
import { Color } from "three"; | ||
|
||
type RevealContainerProps = { | ||
color?: Color; | ||
sdk: CogniteClient; | ||
children?: ReactNode; | ||
} | ||
|
||
export default function RevealContainer({ children, sdk, color }: RevealContainerProps) { | ||
const [viewer, setViewer] = useState<Cognite3DViewer>(); | ||
const revealDomElementRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
const viewer = new Cognite3DViewer({ sdk, domElement: revealDomElementRef.current! }); | ||
viewer.setBackgroundColor({ color, alpha: 1 }) | ||
setViewer(viewer); | ||
return () => { | ||
viewer.dispose(); | ||
setViewer(undefined); | ||
} | ||
}, []); | ||
|
||
const mountChildren = (viewer: Cognite3DViewer | undefined) => { | ||
if (viewer === undefined) return <></>; | ||
return ( | ||
<> | ||
<RevealContext.Provider value={viewer}> | ||
{children} | ||
</RevealContext.Provider> | ||
</> | ||
) | ||
} | ||
|
||
return ( | ||
<div ref={revealDomElementRef}> | ||
{mountChildren(viewer)} | ||
</div> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
react-components/src/components/RevealContainer/RevealContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Cognite3DViewer } from '@cognite/reveal'; | ||
import {createContext, useContext} from 'react'; | ||
|
||
export const RevealContext = createContext<Cognite3DViewer | null>(null); | ||
|
||
export const useReveal = () => { | ||
const reveal = useContext(RevealContext); | ||
if (!reveal) { | ||
throw new Error('useReveal must be used within a RevealProvider'); | ||
} | ||
return reveal; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import '@cognite/cogs.js/dist/cogs.css'; | ||
export { Image360HistoricalDetails } from './View'; | ||
export { default as RevealContainer } from './RevealContainer/RevealContainer'; | ||
export { default as CogniteCadModelContainer } from './CogniteCadModelContainer/CogniteCadModelContainer'; | ||
export { Image360HistoricalDetails } from './Image360HistoricalDetails/Image360HistoricalDetails'; |
41 changes: 41 additions & 0 deletions
41
react-components/src/stories/CogniteCadModelContainer.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { CogniteCadModelContainer, RevealContainer } from ".."; | ||
import { CogniteClient } from "@cognite/sdk"; | ||
import { Color, Matrix4 } from "three"; | ||
|
||
const meta = { | ||
title: "Example/CogniteCadModelContainer", | ||
component: CogniteCadModelContainer, | ||
tags: ["autodocs"], | ||
} satisfies Meta<typeof CogniteCadModelContainer>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
// TODO: See if we can improve this with some proper authentication | ||
const token = new URLSearchParams(window.location.search).get("token")!; | ||
const sdk = new CogniteClient({ | ||
appId: "reveal.example", | ||
baseUrl: "https://greenfield.cognitedata.com", | ||
project: "3d-test", | ||
getToken: () => Promise.resolve(token), | ||
}); | ||
|
||
export const Main: Story = { | ||
args: { | ||
addModelOptions: { | ||
modelId: 1791160622840317, | ||
revisionId: 498427137020189, | ||
}, | ||
transform: new Matrix4().makeTranslation(0, 10, 0), | ||
}, | ||
render: ({ addModelOptions, transform }) => ( | ||
<RevealContainer sdk={sdk} color={new Color(0x4a4a4a)}> | ||
<CogniteCadModelContainer addModelOptions={addModelOptions} /> | ||
<CogniteCadModelContainer | ||
addModelOptions={addModelOptions} | ||
transform={transform} | ||
/> | ||
</RevealContainer> | ||
), | ||
}; |
2 changes: 1 addition & 1 deletion
2
react-components/src/stories/Image360HistoricalDetails.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.