forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerInfo.tsx
More file actions
253 lines (235 loc) · 7.25 KB
/
ContainerInfo.tsx
File metadata and controls
253 lines (235 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import {
Button,
Stack,
Tab,
Tabs,
Dropdown,
Icon,
IconButton,
useToggle,
Alert,
} from '@openedx/paragon';
import React, { useCallback } from 'react';
import { Link } from 'react-router-dom';
import { InfoOutline, MoreVert } from '@openedx/paragon/icons';
import { useClipboard } from '@src/generic/clipboard';
import { ContainerType, getBlockType } from '@src/generic/key-utils';
import { useComponentPickerContext } from '../common/context/ComponentPickerContext';
import { useOptionalLibraryContext } from '../common/context/LibraryContext';
import {
type ContainerInfoTab,
CONTAINER_INFO_TABS,
isContainerInfoTab,
useSidebarContext,
} from '../common/context/SidebarContext';
import ContainerOrganize from './ContainerOrganize';
import ContainerUsage from './ContainerUsage';
import { useLibraryRoutes } from '../routes';
import { LibraryUnitBlocks } from '../units/LibraryUnitBlocks';
import { LibraryContainerChildren } from '../section-subsections/LibraryContainerChildren';
import messages from './messages';
import { useContainer } from '../data/apiHooks';
import ContainerDeleter from './ContainerDeleter';
import { ContainerPublisher } from './ContainerPublisher';
import { PublishDraftButton, PublishedChip } from '../generic/publish-status-buttons';
import { ContainerDetails } from './ContainerDetails';
type ContainerPreviewProps = {
containerId: string;
};
const ContainerMenu = ({ containerId }: ContainerPreviewProps) => {
const intl = useIntl();
const { copyToClipboard } = useClipboard();
const handleCopy = useCallback(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
copyToClipboard(containerId);
}, [copyToClipboard, containerId]);
const [isConfirmingDelete, confirmDelete, cancelDelete] = useToggle(false);
return (
<>
<Dropdown id="container-info-dropdown">
<Dropdown.Toggle
id="container-info-menu-toggle"
as={IconButton}
src={MoreVert}
iconAs={Icon}
variant="primary"
alt={intl.formatMessage(messages.containerCardMenuAlt)}
data-testid="container-info-menu-toggle"
/>
<Dropdown.Menu>
<Dropdown.Item onClick={handleCopy}>
<FormattedMessage {...messages.menuCopyContainer} />
</Dropdown.Item>
<Dropdown.Item onClick={confirmDelete}>
<FormattedMessage {...messages.menuDeleteContainer} />
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
{isConfirmingDelete && (
<ContainerDeleter
close={cancelDelete}
containerId={containerId}
/>
)}
</>
);
};
const ContainerPreview = ({ containerId }: ContainerPreviewProps) => {
const containerType = getBlockType(containerId);
if (containerType === ContainerType.Unit) {
return <LibraryUnitBlocks unitId={containerId} readOnly />;
}
return <LibraryContainerChildren containerKey={containerId} readOnly />;
};
const ContainerActions = ({
containerId,
containerType,
hasUnpublishedChanges,
}: {
containerId: string;
containerType: string;
hasUnpublishedChanges: boolean;
}) => {
const intl = useIntl();
const { libraryId } = useOptionalLibraryContext();
const { componentPickerMode } = useComponentPickerContext();
const { insideUnit, insideSubsection, insideSection } = useLibraryRoutes();
const [isPublisherOpen, openPublisher, closePublisher] = useToggle(false);
const showOpenButton = libraryId && !componentPickerMode && !(
insideUnit || insideSubsection || insideSection
);
if (isPublisherOpen) {
return (
<ContainerPublisher
handleClose={closePublisher}
containerId={containerId}
/>
);
}
return (
<div className="d-flex flex-wrap">
{showOpenButton && (
<Button
variant="outline-primary"
className="m-1 text-nowrap flex-grow-1"
as={Link}
to={`/library/${libraryId}/${containerType}/${containerId}`}
>
{intl.formatMessage(messages.openButton)}
</Button>
)}
{!componentPickerMode && (
!hasUnpublishedChanges ?
(
<div className="m-1 flex-grow-1">
<PublishedChip />
</div>
) :
(
<div className="flex-grow-1">
<PublishDraftButton
onClick={openPublisher}
/>
</div>
)
)}
{showOpenButton && (
<div className="mt-1">
<ContainerMenu containerId={containerId} />
</div>
)}
</div>
);
};
/* istanbul ignore next */
/* istanbul ignore next */
const ContainerSettings = () => (
<Alert icon={InfoOutline} variant="info">
<p>
<FormattedMessage {...messages.containerSettingsMsg} />
</p>
</Alert>
);
const ContainerInfo = () => {
const intl = useIntl();
const {
defaultTab,
hiddenTabs,
sidebarTab,
setSidebarTab,
sidebarItemInfo,
resetSidebarAction,
} = useSidebarContext();
const containerId = sidebarItemInfo?.id;
const containerType = containerId ? getBlockType(containerId) : undefined;
const { data: container } = useContainer(containerId);
const defaultContainerTab = defaultTab.container;
const tab: ContainerInfoTab = (
sidebarTab && isContainerInfoTab(sidebarTab)
) ?
sidebarTab :
defaultContainerTab;
/* istanbul ignore next */
const handleTabChange = (newTab: ContainerInfoTab) => {
resetSidebarAction();
setSidebarTab(newTab);
};
const renderTab = useCallback((infoTab: ContainerInfoTab, title: string, component?: React.ReactNode) => {
if (hiddenTabs.includes(infoTab)) {
// For some reason, returning anything other than empty list breaks the tab style
return [];
}
return (
<Tab eventKey={infoTab} title={title}>
{component}
</Tab>
);
}, [hiddenTabs, defaultContainerTab, containerId]);
if (!container || !containerId || !containerType) {
return null;
}
return (
<Stack>
<ContainerActions
containerId={containerId}
containerType={containerType}
hasUnpublishedChanges={container.hasUnpublishedChanges}
/>
<Tabs
variant="tabs"
className="my-3 d-flex justify-content-around"
defaultActiveKey={defaultContainerTab}
activeKey={tab}
onSelect={handleTabChange}
>
{renderTab(
CONTAINER_INFO_TABS.Preview,
intl.formatMessage(messages.previewTabTitle),
<ContainerPreview containerId={containerId} />,
)}
{renderTab(
CONTAINER_INFO_TABS.Manage,
intl.formatMessage(messages.manageTabTitle),
<ContainerOrganize />,
)}
{renderTab(
CONTAINER_INFO_TABS.Usage,
intl.formatMessage(messages.usageTabTitle),
<ContainerUsage />,
)}
{renderTab(
CONTAINER_INFO_TABS.Settings,
intl.formatMessage(messages.settingsTabTitle),
<ContainerSettings />,
)}
{renderTab(
CONTAINER_INFO_TABS.Details,
intl.formatMessage(messages.detailsTabTitle),
<ContainerDetails />,
)}
</Tabs>
</Stack>
);
};
export default ContainerInfo;