-
Notifications
You must be signed in to change notification settings - Fork 20
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): select single model simple list when has no scene #4746
Open
danpriori
wants to merge
27
commits into
master
Choose a base branch
from
danpriori/BND3D-3961-select-single-model-simple-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
de144fe
add simple list + button to select single 3d model
danpriori bc20f9c
improve the all 3d model list and add the revision and model id fetchers
danpriori 743e6ea
cleanup
danpriori e27c037
lint
danpriori 07bd25f
Merge branch 'master' into danpriori/BND3D-3961-select-single-model-s…
danpriori 44a0674
update toolbar screenshot
danpriori f51740e
changes from cr
danpriori f8e536b
refactoring and add initial useRevisions utest
danpriori add9cc0
add initial utest for all3dmodels hook
danpriori e84ecda
utest refactoring
danpriori 82cf0b3
Merge branch 'master' into danpriori/BND3D-3961-select-single-model-s…
danpriori 17dfe70
Merge remote-tracking branch 'origin/master' into danpriori/BND3D-396…
danpriori 4b52c09
Merge branch 'danpriori/BND3D-3961-select-single-model-simple-list' o…
danpriori 612d255
cr changes
danpriori e1bf69b
Merge remote-tracking branch 'origin/master' into danpriori/BND3D-396…
danpriori cc1179e
remove unnecessary extra context
danpriori 12aa21f
Merge remote-tracking branch 'origin/master' into danpriori/BND3D-396…
danpriori 42bd5c9
fix query keys and return the query parameters to the default
danpriori d6c6da6
move resource selection type definitions to the react components and …
danpriori bfb2f78
Merge remote-tracking branch 'origin/master' into danpriori/BND3D-396…
danpriori ffd5aa2
move exports
danpriori f4ae0d6
refactoring to support changes for single model selection - wip
danpriori b09014c
cleanup and update type
danpriori 8911551
add callback for any resource is loaded
danpriori a43f6c2
add loading icon and execute parallel for revisions
danpriori 477d80b
add search input and filtering
danpriori 072eae5
Merge remote-tracking branch 'origin/master' into danpriori/BND3D-396…
danpriori File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions
39
react-components/src/components/RevealToolbar/ModelsList.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,39 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type ReactElement } from 'react'; | ||
|
||
import { Menu } from '@cognite/cogs.js'; | ||
import { type ModelWithRevision } from '../../hooks/types'; | ||
|
||
type ModelListProps = { | ||
modelsWithRevision: ModelWithRevision[]; | ||
selectedModel: ModelWithRevision | undefined; | ||
onModelChange: (model: ModelWithRevision | undefined) => void; | ||
}; | ||
|
||
export const ModelsList = ({ | ||
modelsWithRevision, | ||
selectedModel, | ||
onModelChange | ||
}: ModelListProps): ReactElement => { | ||
if (modelsWithRevision.length === 0) { | ||
return <></>; | ||
} | ||
return ( | ||
<> | ||
{modelsWithRevision.map((modelData) => { | ||
return ( | ||
<Menu.Item | ||
key={`${modelData.model.id}`} | ||
toggled={selectedModel?.model.id === modelData.model.id} | ||
onClick={() => { | ||
onModelChange(modelData); | ||
}}> | ||
{modelData.model?.name} | ||
</Menu.Item> | ||
); | ||
})} | ||
</> | ||
); | ||
}; |
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
29 changes: 29 additions & 0 deletions
29
react-components/src/components/RevealToolbar/SelectSingle3DModelButton.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,29 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type ReactElement } from 'react'; | ||
|
||
import { useSDK } from '../RevealCanvas/SDKProvider'; | ||
import { RevealContext } from '../RevealContext/RevealContext'; | ||
import { type ModelWithRevision } from '../../hooks/types'; | ||
import { Single3DModelSelection } from './SelectSingle3DModelSelection'; | ||
|
||
type SelectSingle3DModelButtonProps = { | ||
onSingleModelChanged?: (model: ModelWithRevision | undefined) => void; | ||
}; | ||
|
||
export const SelectSingle3DModelButton = ({ | ||
onSingleModelChanged | ||
}: SelectSingle3DModelButtonProps): ReactElement => { | ||
const sdk = useSDK(); | ||
|
||
const handleSingleModelChange = (model: ModelWithRevision | undefined): void => { | ||
if (onSingleModelChanged !== undefined) onSingleModelChanged(model); | ||
}; | ||
|
||
return ( | ||
<RevealContext sdk={sdk}> | ||
<Single3DModelSelection sdk={sdk} onModelChange={handleSingleModelChange} /> | ||
</RevealContext> | ||
); | ||
}; |
63 changes: 63 additions & 0 deletions
63
react-components/src/components/RevealToolbar/SelectSingle3DModelSelection.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,63 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { useState, type ReactElement } from 'react'; | ||
|
||
import { Button, Dropdown, Menu, Tooltip as CogsTooltip } from '@cognite/cogs.js'; | ||
import { useTranslation } from '../i18n/I18n'; | ||
import styled from 'styled-components'; | ||
import { useAll3dModels } from '../../hooks/useAll3dModels'; | ||
import { type CogniteClient } from '@cognite/sdk'; | ||
import { ModelsList } from './ModelsList'; | ||
import { useRevisions } from '../../hooks/useRevisions'; | ||
import { type ModelWithRevision } from '../../hooks/types'; | ||
|
||
type Single3DModelSelectionProps = { | ||
sdk: CogniteClient; | ||
onModelChange: (model: ModelWithRevision | undefined) => void; | ||
}; | ||
|
||
export const Single3DModelSelection = ({ | ||
sdk, | ||
onModelChange | ||
}: Single3DModelSelectionProps): ReactElement => { | ||
const { data: models } = useAll3dModels(sdk, true); | ||
|
||
const { data: modelsWithRevision } = useRevisions(sdk, models); | ||
|
||
const [selectedModel, setSelectedModel] = useState<ModelWithRevision | undefined>(); | ||
|
||
const { t } = useTranslation(); | ||
|
||
const handleSelectedModelChange = (model: ModelWithRevision | undefined): void => { | ||
danpriori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setSelectedModel(model); | ||
onModelChange(model); | ||
}; | ||
|
||
return ( | ||
<CogsTooltip | ||
content={t('MODEL_SELECT_HEADER', 'Select 3D model')} | ||
placement="right" | ||
appendTo={document.body}> | ||
<Dropdown | ||
placement="right-start" | ||
content={ | ||
<StyledMenu> | ||
<Menu.Header>{t('MODEL_SELECT_HEADER', 'Select 3D model')}</Menu.Header> | ||
<ModelsList | ||
modelsWithRevision={modelsWithRevision ?? []} | ||
selectedModel={selectedModel} | ||
onModelChange={handleSelectedModelChange} | ||
/> | ||
</StyledMenu> | ||
danpriori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}> | ||
<Button icon="World" aria-label="Select 3D model" type="ghost"></Button> | ||
</Dropdown> | ||
</CogsTooltip> | ||
); | ||
}; | ||
|
||
const StyledMenu = styled(Menu)` | ||
max-height: 400px; | ||
overflow: auto; | ||
`; |
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,8 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type CogniteClient, type Model3D } from '@cognite/sdk'; | ||
|
||
export const get3dModels = async (sdk: CogniteClient): Promise<Model3D[]> => { | ||
return await sdk.models3D.list().autoPagingToArray({ limit: Infinity }); | ||
}; |
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,19 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type Model3D, type CogniteClient, type Revision3D } from '@cognite/sdk'; | ||
import { type ModelWithRevision } from '../types'; | ||
|
||
export const getRevisions = async ( | ||
model: Model3D | undefined, | ||
sdk: CogniteClient | ||
): Promise<ModelWithRevision | undefined> => { | ||
if (model === undefined) return; | ||
const revisions = await sdk.revisions3D.list(model.id).autoPagingToArray({ limit: Infinity }); | ||
const revisionFound = | ||
revisions.find((revision: Revision3D) => revision.published) ?? revisions[0]; | ||
return { | ||
model, | ||
revision: revisionFound | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*! | ||
danpriori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { useQuery, type UseQueryResult } from '@tanstack/react-query'; | ||
|
||
import { type CogniteClient, type HttpError, type Model3D } from '@cognite/sdk'; | ||
import { get3dModels } from './network/get3dModels'; | ||
|
||
export const useAll3dModels = (sdk: CogniteClient, enabled: boolean): UseQueryResult<Model3D[]> => { | ||
return useQuery<Model3D[], HttpError>({ | ||
queryKey: ['models'], | ||
queryFn: async () => await get3dModels(sdk), | ||
staleTime: Infinity, | ||
enabled | ||
}); | ||
}; |
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,27 @@ | ||
/*! | ||
danpriori marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { useQuery, type UseQueryResult } from '@tanstack/react-query'; | ||
|
||
import { type Model3D, type CogniteClient } from '@cognite/sdk'; | ||
import { type ModelWithRevision } from './types'; | ||
import { getRevisions } from './network/getRevisions'; | ||
|
||
const STALE_TIME = 10 * 1000; | ||
const REFRESH_INTERVAL = 10 * 1000; | ||
|
||
export function useRevisions( | ||
sdk: CogniteClient, | ||
models: Model3D[] | undefined | ||
): UseQueryResult<ModelWithRevision[]> { | ||
return useQuery({ | ||
queryKey: ['model-revision', models], | ||
queryFn: async () => { | ||
const fetchPromises = models?.map(async (model) => await getRevisions(model, sdk)); | ||
return await Promise.all(fetchPromises ?? []); | ||
}, | ||
staleTime: STALE_TIME, | ||
refetchInterval: REFRESH_INTERVAL, | ||
enabled: models !== undefined && models.length > 0 | ||
}); | ||
} |
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
76 changes: 76 additions & 0 deletions
76
react-components/tests/unit-tests/utilities/useAll3dModels.test.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,76 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { type CogniteClient, type Model3D } from '@cognite/sdk'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { useAll3dModels } from '../../../src/hooks/useAll3dModels'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { get3dModels } from '../../../src/hooks/network/get3dModels'; | ||
|
||
const models: Model3D[] = [ | ||
{ | ||
id: 1, | ||
name: 'model1', | ||
createdTime: new Date() | ||
}, | ||
{ | ||
id: 2, | ||
name: 'model2', | ||
createdTime: new Date() | ||
}, | ||
{ | ||
id: 3, | ||
name: 'model3', | ||
createdTime: new Date() | ||
}, | ||
{ | ||
id: 4, | ||
name: 'model4', | ||
createdTime: new Date() | ||
} | ||
]; | ||
|
||
const sdk = { | ||
post: vi.fn().mockResolvedValue({ data: {} }), | ||
project: 'project' | ||
} as unknown as CogniteClient; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const wrapper = ({ children }: { children: any }): any => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
vi.mock('../../../src/hooks/network/get3dModels'); | ||
|
||
describe('useAll3dModels', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
queryClient.clear(); | ||
}); | ||
|
||
test('should return a list of models', async () => { | ||
vi.mocked(get3dModels).mockResolvedValue(models); | ||
|
||
const { result } = renderHook(() => useAll3dModels(sdk, true), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual(models); | ||
}); | ||
}); | ||
|
||
test('should return an empty list of models if no models exist', async () => { | ||
vi.mocked(get3dModels).mockResolvedValue([]); | ||
const { result } = renderHook(() => useAll3dModels(sdk, true), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual([]); | ||
}); | ||
}); | ||
|
||
test('should return undefined if not enabled', async () => { | ||
const { result } = renderHook(() => useAll3dModels(sdk, false), { wrapper }); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should the component be under
RevealContext
?As
SelectSingle3DModelButton
is exported underRevealToolbar
which should be withinRevealContext
, I do not think it is necessaryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm strange. I only added because it was always complaining about without the context. Even under revealToolbar. Now is working without it.🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc1179e