Skip to content

Commit

Permalink
chore: rename, change types, add story
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Jul 31, 2023
1 parent 71daaf6 commit fb40d25
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type Color } from 'three';
import { ModelsLoadingStateContext } from '../Reveal3DResources/ModelsLoadingContext';
import { SDKProvider } from './SDKProvider';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
import { AuxillaryDivProvider } from './AuxillaryDivProvider';
import { AuxillaryDivProvider } from '../ViewerAnchor/AuxillaryDivProvider';

type RevealContainerProps = {
color?: Color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
* Copyright 2023 Cognite AS
*/

import React, { createContext, useContext, useState, useCallback, type JSX } from 'react';
import { createContext, useContext, useState, useCallback, ReactElement } from 'react';

export const AuxillaryDivProvider = ({ children }: { children: React.ReactNode }): JSX.Element => {
const [elements, setElements] = useState<JSX.Element[]>([]);
export const AuxillaryDivProvider = ({ children }: { children: ReactElement }): ReactElement => {
const [elements, setElements] = useState<ReactElement[]>([]);

// Maintain a local copy of the elements, needed for properly supporting multiple
// `addElement` calls between rerenders
let cachedElements = elements;

const addElement = useCallback(
(element: JSX.Element) => {
(element: ReactElement) => {
const newElementList = [...cachedElements, element];

setElements(newElementList);
Expand All @@ -22,7 +22,7 @@ export const AuxillaryDivProvider = ({ children }: { children: React.ReactNode }
);

const removeElement = useCallback(
(element: JSX.Element) => {
(element: ReactElement) => {
const newElementList = cachedElements.filter((e) => e !== element);

setElements(newElementList);
Expand All @@ -42,8 +42,8 @@ export const AuxillaryDivProvider = ({ children }: { children: React.ReactNode }
};

type AuxillaryContextData = {
addElement: (element: JSX.Element) => void;
removeElement: (element: JSX.Element) => void;
addElement: (element: ReactElement) => void;
removeElement: (element: ReactElement) => void;
};

const AuxillaryDivContext = createContext<AuxillaryContextData | null>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
* Copyright 2023 Cognite AS
*/

import React, { type JSX, useEffect, useRef } from 'react';
import { useEffect, useRef, ReactElement } from 'react';
import { type Vector3 } from 'three';

import { useReveal } from '../RevealContainer/RevealContext';

import { HtmlOverlayTool } from '@cognite/reveal/tools';
import { useAuxillaryDivContext } from '../RevealContainer/AuxillaryDivProvider';
import { useAuxillaryDivContext } from './AuxillaryDivProvider';

export type InfoCardElementMapping = {
export type ViewerAnchorElementMapping = {
ref: React.RefObject<HTMLElement>;
position: Vector3;
};

export type InfoCardProps = {
export type ViewerAnchorProps = {
position: Vector3;
children: React.ReactNode;
children: ReactElement;
uniqueKey: string;
};

export const InfoCard = ({ position, children, uniqueKey }: InfoCardProps): JSX.Element => {
export const ViewerAnchor = ({ position, children, uniqueKey }: ViewerAnchorProps): ReactElement => {
const viewer = useReveal();

const htmlTool = useRef<HtmlOverlayTool>(new HtmlOverlayTool(viewer));
Expand All @@ -30,7 +30,7 @@ export const InfoCard = ({ position, children, uniqueKey }: InfoCardProps): JSX.

const htmlRef = useRef<HTMLDivElement>(null);
const element = (
<div className="infocard" key={uniqueKey} ref={htmlRef} style={{ position: 'absolute' }}>
<div key={uniqueKey} ref={htmlRef} style={{ position: 'absolute' }}>
{children}
</div>
);
Expand Down
1 change: 1 addition & 0 deletions react-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
type Reveal3DResourcesStyling,
type FdmAssetStylingGroup
} from './components/Reveal3DResources/Reveal3DResources';
export { ViewerAnchor } from './components/ViewerAnchor/ViewerAnchor';
export { CameraController } from './components/CameraController/CameraController';
export type {
AddImageCollection360Options,
Expand Down
11 changes: 5 additions & 6 deletions react-components/stories/Reveal3DResources.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import type { Meta, StoryObj } from '@storybook/react';
import { Reveal3DResources, RevealContainer } from '../src';
import { Color, Matrix4, Vector3 } from 'three';
import { CameraController } from '../src/';
import { CameraController, ViewerAnchor } from '../src/';
import { createSdkByUrlToken } from './utilities/createSdkByUrlToken';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { InfoCard } from '../src/components/InfoCard/InfoCard';

const meta = {
title: 'Example/Reveal3DResources',
Expand Down Expand Up @@ -144,7 +143,7 @@ export const Main: Story = {
styling={styling}
fdmAssetMappingConfig={fdmAssetMappingConfig}
/>
<InfoCard position={position} uniqueKey="key2">
<ViewerAnchor position={position} uniqueKey="key2">
<p
style={{
backgroundColor: 'turquoise',
Expand All @@ -156,8 +155,8 @@ export const Main: Story = {
}}>
This label is stuck at position {position.toArray().join(',')}
</p>
</InfoCard>
<InfoCard position={position2} uniqueKey="key1">
</ViewerAnchor>
<ViewerAnchor position={position2} uniqueKey="key1">
<p
style={{
backgroundColor: 'red',
Expand All @@ -169,7 +168,7 @@ export const Main: Story = {
}}>
This label is stuck at position {position2.toArray().join(',')}
</p>
</InfoCard>
</ViewerAnchor>
<CameraController
initialFitCamera={{
to: 'allModels'
Expand Down
104 changes: 104 additions & 0 deletions react-components/stories/ViewerAnchor.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*!
* Copyright 2023 Cognite AS
*/
import type { Meta, StoryObj } from '@storybook/react';
import { Reveal3DResources, RevealContainer } from '../src';
import { Color, Matrix4, Vector3 } from 'three';
import { CameraController, ViewerAnchor } from '../src/';
import { createSdkByUrlToken } from './utilities/createSdkByUrlToken';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';

const meta = {
title: 'Example/ViewerAnchor',
component: Reveal3DResources,
tags: ['autodocs'],
argTypes: {
styling: {
}
}
} satisfies Meta<typeof Reveal3DResources>;

export default meta;
type Story = StoryObj<typeof meta>;

const sdk = createSdkByUrlToken();

export const Main: Story = {
args: {
resources: [
{
modelId: 2551525377383868,
revisionId: 2143672450453400,
transform: new Matrix4().makeTranslation(-340, -480, 80)
}
],
styling: {},
fdmAssetMappingConfig: {
source: {
space: 'hf_3d_schema',
version: '1',
type: 'view',
externalId: 'cdf_3d_connection_data',
},
assetFdmSpace: 'hf_customer_a',
}
},
render: ({ resources, styling, fdmAssetMappingConfig }) => {
const position = new Vector3(50, 30, 50);
const position2 = new Vector3(0, 0, 0);

return (
<RevealContainer
sdk={sdk}
color={new Color(0x4a4a4a)}
uiElements={<ReactQueryDevtools initialIsOpen={false} />}
viewerOptions={{
loadingIndicatorStyle: {
opacity: 1,
placement: 'topRight'
}
}}>
<Reveal3DResources
resources={resources}
styling={styling}
fdmAssetMappingConfig={fdmAssetMappingConfig}
/>
<ViewerAnchor position={position} uniqueKey="key2">
<p
style={{
backgroundColor: 'turquoise',
padding: '10px',
borderRadius: '10px',
borderStyle: 'solid',
maxWidth: '300px',
transform: 'translate(-50%, calc(-100% - 50px))'
}}>
This label is stuck at position {position.toArray().join(',')}
</p>
</ViewerAnchor>
<ViewerAnchor position={position2} uniqueKey="key1">
<p
style={{
backgroundColor: 'red',
padding: '10px',
borderRadius: '10px',
borderStyle: 'solid',
maxWidth: '300px',
transform: 'translate(0px, 0px)'
}}>
This label is stuck at position {position2.toArray().join(',')}
</p>
</ViewerAnchor>
<CameraController
initialFitCamera={{
to: 'allModels'
}}
cameraControlsOptions={{
changeCameraTargetOnClick: true,
mouseWheelAction: 'zoomToCursor'
}}
/>
</RevealContainer>
);
}
};

0 comments on commit fb40d25

Please sign in to comment.