-
Notifications
You must be signed in to change notification settings - Fork 229
feat(data-modeling): export diagram to json COMPASS-9448 #7046
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cce62ac
extract diagram editor toolbar:
mabaasit a43ed7e
add export modal
mabaasit 39793bd
json export
mabaasit e47687e
tests
mabaasit dbc1ca0
close modal
mabaasit 6d8de76
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit 087936c
tests
mabaasit 6c21b63
ensure test run
mabaasit b7d8dcf
fix toast
mabaasit 7c4c14e
fix electron test
mabaasit f9bcc51
fix link
mabaasit b65f4c8
ensure its thrown
mabaasit adc19a1
asset number of selected collections
mabaasit 6c41226
fix modal styles
mabaasit 86e8a92
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit 7fa0480
return null for tests
mabaasit 8c5c14d
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit ffef0b7
remove comment
mabaasit 467dbbb
Update packages/compass-data-modeling/src/components/diagram-editor-t…
mabaasit 69e315f
flip export props
mabaasit 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
packages/compass-data-modeling/src/components/diagram-editor-toolbar.spec.tsx
This file contains hidden or 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,74 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass'; | ||
import { DiagramEditorToolbar } from './diagram-editor-toolbar'; | ||
import sinon from 'sinon'; | ||
|
||
function renderDiagramEditorToolbar( | ||
props: Partial<React.ComponentProps<typeof DiagramEditorToolbar>> = {} | ||
) { | ||
render( | ||
<DiagramEditorToolbar | ||
step="EDITING" | ||
hasUndo={true} | ||
hasRedo={true} | ||
onUndoClick={() => {}} | ||
onRedoClick={() => {}} | ||
onExportClick={() => {}} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
describe('DiagramEditorToolbar', function () { | ||
it('renders nothing if step is NO_DIAGRAM_SELECTED', function () { | ||
renderDiagramEditorToolbar({ step: 'NO_DIAGRAM_SELECTED' }); | ||
expect(() => screen.getByTestId('diagram-editor-toolbar')).to.throw(); | ||
}); | ||
|
||
it('renders nothing if step is not EDITING', function () { | ||
renderDiagramEditorToolbar({ step: 'ANALYSIS_CANCELED' }); | ||
expect(() => screen.getByTestId('diagram-editor-toolbar')).to.throw(); | ||
}); | ||
|
||
context('undo button', function () { | ||
it('renders it disabled if hasUndo is false', function () { | ||
renderDiagramEditorToolbar({ hasUndo: false }); | ||
const undoButton = screen.getByRole('button', { name: 'Undo' }); | ||
expect(undoButton).to.have.attribute('aria-disabled', 'true'); | ||
}); | ||
it('renders it enabled if hasUndo is true and calls onUndoClick', function () { | ||
const undoSpy = sinon.spy(); | ||
renderDiagramEditorToolbar({ hasUndo: true, onUndoClick: undoSpy }); | ||
const undoButton = screen.getByRole('button', { name: 'Undo' }); | ||
expect(undoButton).to.have.attribute('aria-disabled', 'false'); | ||
userEvent.click(undoButton); | ||
expect(undoSpy).to.have.been.calledOnce; | ||
}); | ||
}); | ||
|
||
context('redo button', function () { | ||
it('renders it disabled if hasRedo is false', function () { | ||
renderDiagramEditorToolbar({ hasRedo: false }); | ||
const redoButton = screen.getByRole('button', { name: 'Redo' }); | ||
expect(redoButton).to.have.attribute('aria-disabled', 'true'); | ||
}); | ||
it('renders it enabled if hasRedo is true and calls onRedoClick', function () { | ||
const redoSpy = sinon.spy(); | ||
renderDiagramEditorToolbar({ hasRedo: true, onRedoClick: redoSpy }); | ||
const redoButton = screen.getByRole('button', { name: 'Redo' }); | ||
expect(redoButton).to.have.attribute('aria-disabled', 'false'); | ||
userEvent.click(redoButton); | ||
expect(redoSpy).to.have.been.calledOnce; | ||
}); | ||
}); | ||
|
||
it('renders export buttona and calls onExportClick', function () { | ||
const exportSpy = sinon.spy(); | ||
renderDiagramEditorToolbar({ onExportClick: exportSpy }); | ||
const exportButton = screen.getByRole('button', { name: 'Export' }); | ||
expect(exportButton).to.exist; | ||
userEvent.click(exportButton); | ||
expect(exportSpy).to.have.been.calledOnce; | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/compass-data-modeling/src/components/diagram-editor-toolbar.tsx
This file contains hidden or 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,47 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import type { DataModelingState } from '../store/reducer'; | ||
import { redoEdit, showExportModal, undoEdit } from '../store/diagram'; | ||
import { Icon, IconButton } from '@mongodb-js/compass-components'; | ||
|
||
export const DiagramEditorToolbar: React.FunctionComponent<{ | ||
step: DataModelingState['step']; | ||
hasUndo: boolean; | ||
hasRedo: boolean; | ||
onUndoClick: () => void; | ||
onRedoClick: () => void; | ||
onExportClick: () => void; | ||
}> = ({ step, hasUndo, onUndoClick, hasRedo, onRedoClick, onExportClick }) => { | ||
if (step !== 'EDITING') { | ||
return null; | ||
} | ||
return ( | ||
<div data-testid="diagram-editor-toolbar"> | ||
<IconButton aria-label="Undo" disabled={!hasUndo} onClick={onUndoClick}> | ||
<Icon glyph="Undo"></Icon> | ||
</IconButton> | ||
<IconButton aria-label="Redo" disabled={!hasRedo} onClick={onRedoClick}> | ||
<Icon glyph="Redo"></Icon> | ||
</IconButton> | ||
<IconButton aria-label="Export" onClick={onExportClick}> | ||
<Icon glyph="Export"></Icon> | ||
</IconButton> | ||
</div> | ||
); | ||
}; | ||
|
||
export default connect( | ||
(state: DataModelingState) => { | ||
const { diagram, step } = state; | ||
return { | ||
step: step, | ||
hasUndo: (diagram?.edits.prev.length ?? 0) > 0, | ||
hasRedo: (diagram?.edits.next.length ?? 0) > 0, | ||
}; | ||
}, | ||
{ | ||
onUndoClick: undoEdit, | ||
onRedoClick: redoEdit, | ||
onExportClick: showExportModal, | ||
} | ||
)(DiagramEditorToolbar); |
This file contains hidden or 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
147 changes: 147 additions & 0 deletions
147
packages/compass-data-modeling/src/components/export-diagram-modal.tsx
This file contains hidden or 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,147 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { | ||
Button, | ||
css, | ||
Icon, | ||
Label, | ||
Link, | ||
Modal, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
Radio, | ||
RadioGroup, | ||
spacing, | ||
} from '@mongodb-js/compass-components'; | ||
import { | ||
closeExportModal, | ||
selectCurrentModel, | ||
getCurrentDiagramFromState, | ||
} from '../store/diagram'; | ||
import { connect } from 'react-redux'; | ||
import type { DataModelingState } from '../store/reducer'; | ||
import type { StaticModel } from '../services/data-model-storage'; | ||
import { exportToJson } from '../services/export-diagram'; | ||
|
||
const nbsp = '\u00a0'; | ||
|
||
const modelBodyStyles = css({ | ||
paddingTop: spacing[600], | ||
}); | ||
|
||
const contentContainerStyles = css({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: spacing[300], | ||
}); | ||
|
||
const radioItemStyles = css({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: spacing[200], | ||
}); | ||
|
||
const footerStyles = css({ | ||
display: 'flex', | ||
gap: spacing[200], | ||
}); | ||
|
||
type ExportDiagramModalProps = { | ||
isModalOpen: boolean; | ||
diagramLabel: string; | ||
model: StaticModel | null; | ||
onCloseClick: () => void; | ||
}; | ||
|
||
const ExportDiagramModal = ({ | ||
isModalOpen, | ||
diagramLabel, | ||
model, | ||
onCloseClick, | ||
}: ExportDiagramModalProps) => { | ||
const [exportFormat, setExportFormat] = useState<'json' | null>(null); | ||
|
||
const onExport = useCallback(() => { | ||
if (!exportFormat || !model) { | ||
return; | ||
} | ||
exportToJson(diagramLabel, model); | ||
onCloseClick(); | ||
}, [exportFormat, onCloseClick, model, diagramLabel]); | ||
|
||
return ( | ||
<Modal | ||
open={isModalOpen} | ||
setOpen={onCloseClick} | ||
data-testid="export-diagram-modal" | ||
> | ||
<ModalHeader | ||
title="Export data model" | ||
subtitle={ | ||
<div> | ||
Export the data modal to JSON format. | ||
{nbsp} | ||
<Link | ||
href="https://www.mongodb.com/docs/manual/data-modeling//" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn more | ||
</Link> | ||
</div> | ||
} | ||
/> | ||
<ModalBody className={modelBodyStyles}> | ||
<div className={contentContainerStyles}> | ||
<Label htmlFor="">Select file format:</Label> | ||
<RadioGroup className={contentContainerStyles} value={exportFormat}> | ||
<div className={radioItemStyles}> | ||
<Icon glyph="CurlyBraces" /> | ||
<Radio | ||
checked={exportFormat === 'json'} | ||
value="json" | ||
aria-label="JSON" | ||
onClick={() => setExportFormat('json')} | ||
> | ||
JSON | ||
</Radio> | ||
</div> | ||
</RadioGroup> | ||
</div> | ||
</ModalBody> | ||
<ModalFooter className={footerStyles}> | ||
<Button | ||
variant="primary" | ||
onClick={() => void onExport()} | ||
data-testid="export-button" | ||
> | ||
Export | ||
</Button> | ||
<Button | ||
variant="default" | ||
onClick={onCloseClick} | ||
data-testid="cancel-button" | ||
> | ||
Cancel | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default connect( | ||
(state: DataModelingState) => { | ||
const { diagram } = state; | ||
const model = diagram | ||
? selectCurrentModel(getCurrentDiagramFromState(state)) | ||
: null; | ||
return { | ||
model, | ||
diagramLabel: diagram?.name ?? 'Schema Preview', | ||
isModalOpen: Boolean(diagram?.isExportModalOpen), | ||
}; | ||
}, | ||
{ | ||
onCloseClick: closeExportModal, | ||
} | ||
)(ExportDiagramModal); |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Should we try/catch this and show the error to the user? I'm wondering what happens if a user tries to save in a place where they might not have permissions or another error with file system saving that could happen like no space left.
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.
I think that will be handled by the toasts which we show for file downloads as well.
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.
That's correct, both success and failure will be handled on the app level (for electron)
compass/packages/compass/src/main/application.ts
Line 493 in f9f5848