|
1 | | -import React from 'react'; |
| 1 | +import React, { useMemo } from 'react'; |
2 | 2 | import { connect } from 'react-redux'; |
3 | 3 | import type { DataModelingState } from '../../store/reducer'; |
4 | | -import { DrawerSection } from '@mongodb-js/compass-components'; |
| 4 | +import { |
| 5 | + css, |
| 6 | + DrawerSection, |
| 7 | + ItemActionControls, |
| 8 | +} from '@mongodb-js/compass-components'; |
5 | 9 | import CollectionDrawerContent from './collection-drawer-content'; |
6 | 10 | import RelationshipDrawerContent from './relationship-drawer-content'; |
| 11 | +import { |
| 12 | + deleteRelationship, |
| 13 | + selectCurrentModelFromState, |
| 14 | +} from '../../store/diagram'; |
| 15 | +import { getDefaultRelationshipName } from '../../utils'; |
| 16 | + |
7 | 17 | export const DATA_MODELING_DRAWER_ID = 'data-modeling-drawer'; |
8 | 18 |
|
| 19 | +const drawerTitleStyles = css({ |
| 20 | + display: 'flex', |
| 21 | + width: '100%', |
| 22 | +}); |
| 23 | + |
| 24 | +const drawerTitleTextStyles = css({ |
| 25 | + flex: 1, |
| 26 | + whiteSpace: 'nowrap', |
| 27 | + overflow: 'hidden', |
| 28 | + textOverflow: 'ellipsis', |
| 29 | +}); |
| 30 | + |
| 31 | +const drawerTitleActionGroupStyles = css({}); |
| 32 | + |
9 | 33 | type DiagramEditorSidePanelProps = { |
10 | | - selectedItems: { type: 'relationship' | 'collection'; id: string } | null; |
| 34 | + selectedItems: { |
| 35 | + id: string; |
| 36 | + type: 'relationship' | 'collection'; |
| 37 | + label: string; |
| 38 | + } | null; |
| 39 | + onDeleteRelationship: (rId: string) => void; |
11 | 40 | }; |
12 | 41 |
|
13 | 42 | function DiagramEditorSidePanel({ |
14 | 43 | selectedItems, |
| 44 | + onDeleteRelationship, |
15 | 45 | }: DiagramEditorSidePanelProps) { |
16 | | - if (!selectedItems) { |
17 | | - return null; |
18 | | - } |
| 46 | + const { content, label, actions, handleAction } = useMemo(() => { |
| 47 | + if (selectedItems?.type === 'collection') { |
| 48 | + return { |
| 49 | + label: selectedItems.label, |
| 50 | + content: ( |
| 51 | + <CollectionDrawerContent |
| 52 | + key={selectedItems.id} |
| 53 | + namespace={selectedItems.id} |
| 54 | + ></CollectionDrawerContent> |
| 55 | + ), |
| 56 | + actions: [], |
| 57 | + handleAction: () => {}, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + if (selectedItems?.type === 'relationship') { |
| 62 | + return { |
| 63 | + label: selectedItems.label, |
| 64 | + content: ( |
| 65 | + <RelationshipDrawerContent |
| 66 | + key={selectedItems.id} |
| 67 | + relationshipId={selectedItems.id} |
| 68 | + ></RelationshipDrawerContent> |
| 69 | + ), |
| 70 | + actions: [ |
| 71 | + { action: 'delete', label: 'Delete', icon: 'Trash' as const }, |
| 72 | + ], |
| 73 | + handleAction: (actionName: string) => { |
| 74 | + if (actionName === 'delete') { |
| 75 | + onDeleteRelationship(selectedItems.id); |
| 76 | + } |
| 77 | + }, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + return { content: null }; |
| 82 | + }, [selectedItems, onDeleteRelationship]); |
19 | 83 |
|
20 | | - let content; |
21 | | - |
22 | | - if (selectedItems.type === 'collection') { |
23 | | - content = ( |
24 | | - <CollectionDrawerContent |
25 | | - key={selectedItems.id} |
26 | | - namespace={selectedItems.id} |
27 | | - ></CollectionDrawerContent> |
28 | | - ); |
29 | | - } else if (selectedItems.type === 'relationship') { |
30 | | - content = ( |
31 | | - <RelationshipDrawerContent |
32 | | - key={selectedItems.id} |
33 | | - relationshipId={selectedItems.id} |
34 | | - ></RelationshipDrawerContent> |
35 | | - ); |
| 84 | + if (!content) { |
| 85 | + return null; |
36 | 86 | } |
37 | 87 |
|
38 | 88 | return ( |
39 | 89 | <DrawerSection |
40 | 90 | id={DATA_MODELING_DRAWER_ID} |
41 | | - title="Details" |
42 | | - label="Details" |
43 | | - glyph="InfoWithCircle" |
| 91 | + title={ |
| 92 | + <div className={drawerTitleStyles}> |
| 93 | + <span className={drawerTitleTextStyles} title={label}> |
| 94 | + {label} |
| 95 | + </span> |
| 96 | + |
| 97 | + <ItemActionControls |
| 98 | + actions={actions} |
| 99 | + iconSize="small" |
| 100 | + onAction={handleAction} |
| 101 | + className={drawerTitleActionGroupStyles} |
| 102 | + // Because the close button here is out of our control, we have do |
| 103 | + // adjust the actions rendering in a bit of an unconventional way: |
| 104 | + // if there's more than one action available, collapse it to "...", |
| 105 | + // if it's just one, make sure button is not collapsed by setting |
| 106 | + // collapseAfter to >0 |
| 107 | + collapseAfter={actions.length > 1 ? 0 : 1} |
| 108 | + ></ItemActionControls> |
| 109 | + </div> |
| 110 | + } |
| 111 | + label={label} |
| 112 | + glyph="Wrench" |
44 | 113 | autoOpen |
45 | | - // TODO: Leafygreen doesn't allow us to tie close event to a particular |
46 | | - // action. We can add this functionality ourselves, but I'm not sure that |
47 | | - // adding even more logic on top of the drawer is a good idea. Maybe we're |
48 | | - // okay with the drawer close button click just staying there until you |
49 | | - // explicitly click something else? |
50 | | - // onClose={onClose} |
51 | 114 | > |
52 | 115 | {content} |
53 | 116 | </DrawerSection> |
54 | 117 | ); |
55 | 118 | } |
56 | 119 |
|
57 | | -export default connect((state: DataModelingState) => { |
58 | | - return { |
59 | | - selectedItems: state.diagram?.selectedItems ?? null, |
60 | | - }; |
61 | | -})(DiagramEditorSidePanel); |
| 120 | +export default connect( |
| 121 | + (state: DataModelingState) => { |
| 122 | + const selected = state.diagram?.selectedItems; |
| 123 | + |
| 124 | + if (!selected) { |
| 125 | + return { |
| 126 | + selectedItems: null, |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + if (selected.type === 'collection') { |
| 131 | + return { |
| 132 | + selectedItems: { |
| 133 | + ...selected, |
| 134 | + label: selected.id, |
| 135 | + }, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + if (selected.type === 'relationship') { |
| 140 | + const model = selectCurrentModelFromState(state); |
| 141 | + const relationship = model.relationships.find((relationship) => { |
| 142 | + return relationship.id === selected.id; |
| 143 | + }); |
| 144 | + |
| 145 | + if (!relationship) { |
| 146 | + throw new Error( |
| 147 | + 'Can not find corresponding relationship when rendering DiagramEditorSidePanel' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + return { |
| 152 | + selectedItems: { |
| 153 | + ...selected, |
| 154 | + label: getDefaultRelationshipName(relationship.relationship), |
| 155 | + }, |
| 156 | + }; |
| 157 | + } |
| 158 | + }, |
| 159 | + { |
| 160 | + onDeleteRelationship: deleteRelationship, |
| 161 | + } |
| 162 | +)(DiagramEditorSidePanel); |
0 commit comments