-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
relates to: #34
- Loading branch information
Showing
7 changed files
with
105 additions
and
14 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
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,34 @@ | ||
import { getCamelRandomId } from './camel-random-id'; | ||
|
||
describe('camel-random-id', () => { | ||
it('should return a random number', () => { | ||
expect(getCamelRandomId('route')).toEqual(expect.any(String)); | ||
}); | ||
|
||
it('should return a random number with a given length', () => { | ||
jest | ||
.spyOn(global, 'crypto', 'get') | ||
.mockImplementationOnce(() => ({ getRandomValues: () => [19508888] }) as unknown as Crypto); | ||
expect(getCamelRandomId('route', 6)).toEqual('route-195088'); | ||
}); | ||
|
||
it('should return a random number using Date.now() if crypto module is not available', () => { | ||
jest.spyOn(global, 'crypto', 'get').mockImplementationOnce(() => undefined as unknown as Crypto); | ||
jest.spyOn(global.Date, 'now').mockReturnValueOnce(888); | ||
|
||
const result = getCamelRandomId('route'); | ||
|
||
expect(result).toEqual('route-888'); | ||
}); | ||
|
||
it('should return a random number using msCrypto if crypto module is not available', () => { | ||
Object.defineProperty(global, 'msCrypto', { | ||
value: global.crypto, | ||
writable: true, | ||
}); | ||
|
||
jest.spyOn(global, 'crypto', 'get').mockImplementationOnce(() => undefined as unknown as Crypto); | ||
|
||
expect(getCamelRandomId('route')).toEqual(expect.any(String)); | ||
}); | ||
}); |
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,6 @@ | ||
export const getCamelRandomId = (kind: string, length = 4): string => { | ||
const cryptoObj = window.crypto || (window as Window & { msCrypto?: Crypto }).msCrypto; | ||
const randomNumber = Math.floor(cryptoObj?.getRandomValues(new Uint32Array(1))[0] ?? Date.now()); | ||
|
||
return `${kind}-${randomNumber.toString(10).slice(0, length)}`; | ||
}; |
39 changes: 33 additions & 6 deletions
39
packages/ui/src/components/Visualization/Visualization.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 |
---|---|---|
@@ -1,33 +1,60 @@ | ||
import { FunctionComponent, PropsWithChildren, useState } from 'react'; | ||
import { Button, Modal } from '@patternfly/react-core'; | ||
import { FunctionComponent, PropsWithChildren, useCallback, useState } from 'react'; | ||
import { CamelRoute, Step } from '../../camel-entities'; | ||
import { getCamelRandomId } from '../../camel-utils/camel-random-id'; | ||
import { Catalog, ITile } from '../Catalog'; | ||
import { VisualizationCanvas } from './VisualizationCanvas'; | ||
import './VisualizationCanvas.scss'; | ||
import { CamelRoute } from '../../camel-entities'; | ||
import { Button } from '@patternfly/react-core'; | ||
|
||
interface CanvasProps { | ||
className?: string; | ||
tiles: Record<string, ITile[]>; | ||
} | ||
|
||
export const Visualization: FunctionComponent<PropsWithChildren<CanvasProps>> = (props) => { | ||
const [entities, setEntities] = useState<CamelRoute[]>([]); | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [selectedCamelEntity, setSelectedCamelEntity] = useState<CamelRoute | undefined>(undefined); | ||
|
||
const onTileClick = useCallback( | ||
(tile: ITile) => { | ||
if (selectedCamelEntity === undefined) { | ||
return; | ||
} | ||
|
||
setIsModalOpen(false); | ||
const firstStep = new Step({ name: tile.name }); | ||
selectedCamelEntity._addStep(firstStep); | ||
setEntities([...entities, selectedCamelEntity]); | ||
}, | ||
[entities, selectedCamelEntity], | ||
); | ||
|
||
const handleModalToggle = useCallback(() => { | ||
setIsModalOpen(!isModalOpen); | ||
}, [isModalOpen]); | ||
|
||
return ( | ||
<div className={`canvasSurface ${props.className ?? ''}`}> | ||
<VisualizationCanvas | ||
contextToolbar={ | ||
<Button | ||
onClick={() => { | ||
setIsModalOpen(true); | ||
const newEntity = new CamelRoute(); | ||
newEntity.id = 'new-route'; | ||
|
||
setEntities([...entities, newEntity]); | ||
newEntity.id = getCamelRandomId('route'); | ||
setSelectedCamelEntity(newEntity); | ||
}} | ||
> | ||
New Camel route | ||
</Button> | ||
} | ||
entities={entities} | ||
/> | ||
|
||
<Modal title="Catalog browser" isOpen={isModalOpen} onClose={handleModalToggle} ouiaId="BasicModal"> | ||
<Catalog tiles={props.tiles} onTileClick={onTileClick} /> | ||
</Modal> | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,30 @@ | ||
import { Title } from '@patternfly/react-core'; | ||
import { FunctionComponent } from 'react'; | ||
import { FunctionComponent, useEffect, useState } from 'react'; | ||
import { camelComponentToTile, camelProcessorToTile, kameletToTile } from '../../camel-utils'; | ||
import { ITile } from '../../components/Catalog'; | ||
import { Visualization } from '../../components/Visualization'; | ||
import { CatalogKind } from '../../models'; | ||
import { useCatalogStore } from '../../store'; | ||
import './DesignPage.scss'; | ||
|
||
export const DesignPage: FunctionComponent = () => { | ||
/** TODO: Extract this logic into a separate provider */ | ||
const catalogs = useCatalogStore((state) => state.catalogs); | ||
const [tiles, setTiles] = useState<Record<string, ITile[]>>({}); | ||
|
||
useEffect(() => { | ||
setTiles({ | ||
Component: Object.values(catalogs[CatalogKind.Component] ?? {}).map(camelComponentToTile), | ||
Processor: Object.values(catalogs[CatalogKind.Processor] ?? {}).map(camelProcessorToTile), | ||
Kamelet: Object.values(catalogs[CatalogKind.Kamelet] ?? {}).map(kameletToTile), | ||
}); | ||
}, [catalogs]); | ||
|
||
return ( | ||
<div className="canvasPage"> | ||
<Title headingLevel="h1">Visualization</Title> | ||
|
||
<Visualization className="canvasPage__canvas" /> | ||
<Visualization className="canvasPage__canvas" tiles={tiles} /> | ||
</div> | ||
); | ||
}; |
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