|
1 |
| -import { |
2 |
| - Button, |
3 |
| - VariantType, |
4 |
| - useModal, |
5 |
| - useToastProvider, |
6 |
| -} from '@openfun/cunningham-react'; |
| 1 | +import { Button, useModal } from '@openfun/cunningham-react'; |
7 | 2 | import { useQueryClient } from '@tanstack/react-query';
|
8 |
| -import { useEffect, useState } from 'react'; |
| 3 | +import dynamic from 'next/dynamic'; |
| 4 | +import { useEffect } from 'react'; |
9 | 5 | import { useTranslation } from 'react-i18next';
|
10 | 6 | import { css } from 'styled-components';
|
11 | 7 |
|
12 |
| -import { |
13 |
| - Box, |
14 |
| - DropdownMenu, |
15 |
| - DropdownMenuOption, |
16 |
| - Icon, |
17 |
| - IconOptions, |
18 |
| -} from '@/components'; |
| 8 | +import { Box, Icon } from '@/components'; |
19 | 9 | import { useCunninghamTheme } from '@/cunningham';
|
20 |
| -import { useEditorStore } from '@/docs/doc-editor/'; |
21 |
| -import { ModalExport } from '@/docs/doc-export/'; |
22 |
| -import { |
23 |
| - Doc, |
24 |
| - KEY_DOC, |
25 |
| - KEY_LIST_DOC, |
26 |
| - ModalRemoveDoc, |
27 |
| - useCopyDocLink, |
28 |
| - useCreateFavoriteDoc, |
29 |
| - useDeleteFavoriteDoc, |
30 |
| -} from '@/docs/doc-management'; |
31 |
| -import { DocShareModal } from '@/docs/doc-share'; |
32 |
| -import { |
33 |
| - KEY_LIST_DOC_VERSIONS, |
34 |
| - ModalSelectVersion, |
35 |
| -} from '@/docs/doc-versioning'; |
36 |
| -import { useAnalytics } from '@/libs'; |
| 10 | +import { Doc } from '@/docs/doc-management'; |
| 11 | +import { KEY_LIST_DOC_VERSIONS } from '@/docs/doc-versioning'; |
37 | 12 | import { useResponsiveStore } from '@/stores';
|
38 | 13 |
|
39 | 14 | interface DocToolBoxProps {
|
40 | 15 | doc: Doc;
|
41 | 16 | }
|
42 | 17 |
|
| 18 | +const DocToolBoxLicence = dynamic(() => |
| 19 | + process.env.MIT_ONLY === 'false' |
| 20 | + ? import('./DocToolBoxLicenceAGPL').then((mod) => mod.DocToolBoxLicenceAGPL) |
| 21 | + : import('./DocToolBoxLicenceMIT').then((mod) => mod.DocToolBoxLicenceMIT), |
| 22 | +); |
| 23 | + |
43 | 24 | export const DocToolBox = ({ doc }: DocToolBoxProps) => {
|
44 | 25 | const { t } = useTranslation();
|
45 | 26 | const hasAccesses = doc.nb_accesses_direct > 1 && doc.abilities.accesses_view;
|
46 | 27 | const queryClient = useQueryClient();
|
47 | 28 |
|
48 |
| - const { spacingsTokens, colorsTokens } = useCunninghamTheme(); |
| 29 | + const { spacingsTokens } = useCunninghamTheme(); |
49 | 30 |
|
50 |
| - const [isModalRemoveOpen, setIsModalRemoveOpen] = useState(false); |
51 |
| - const [isModalExportOpen, setIsModalExportOpen] = useState(false); |
52 |
| - const selectHistoryModal = useModal(); |
| 31 | + const modalHistory = useModal(); |
53 | 32 | const modalShare = useModal();
|
54 | 33 |
|
55 |
| - const { isSmallMobile, isDesktop } = useResponsiveStore(); |
56 |
| - const { editor } = useEditorStore(); |
57 |
| - const { toast } = useToastProvider(); |
58 |
| - const copyDocLink = useCopyDocLink(doc.id); |
59 |
| - const { isFeatureFlagActivated } = useAnalytics(); |
60 |
| - const removeFavoriteDoc = useDeleteFavoriteDoc({ |
61 |
| - listInvalideQueries: [KEY_LIST_DOC, KEY_DOC], |
62 |
| - }); |
63 |
| - const makeFavoriteDoc = useCreateFavoriteDoc({ |
64 |
| - listInvalideQueries: [KEY_LIST_DOC, KEY_DOC], |
65 |
| - }); |
66 |
| - |
67 |
| - const options: DropdownMenuOption[] = [ |
68 |
| - ...(isSmallMobile |
69 |
| - ? [ |
70 |
| - { |
71 |
| - label: t('Share'), |
72 |
| - icon: 'group', |
73 |
| - callback: modalShare.open, |
74 |
| - }, |
75 |
| - { |
76 |
| - label: t('Export'), |
77 |
| - icon: 'download', |
78 |
| - callback: () => { |
79 |
| - setIsModalExportOpen(true); |
80 |
| - }, |
81 |
| - }, |
82 |
| - { |
83 |
| - label: t('Copy link'), |
84 |
| - icon: 'add_link', |
85 |
| - callback: copyDocLink, |
86 |
| - }, |
87 |
| - ] |
88 |
| - : []), |
89 |
| - { |
90 |
| - label: doc.is_favorite ? t('Unpin') : t('Pin'), |
91 |
| - icon: 'push_pin', |
92 |
| - callback: () => { |
93 |
| - if (doc.is_favorite) { |
94 |
| - removeFavoriteDoc.mutate({ id: doc.id }); |
95 |
| - } else { |
96 |
| - makeFavoriteDoc.mutate({ id: doc.id }); |
97 |
| - } |
98 |
| - }, |
99 |
| - testId: `docs-actions-${doc.is_favorite ? 'unpin' : 'pin'}-${doc.id}`, |
100 |
| - }, |
101 |
| - { |
102 |
| - label: t('Version history'), |
103 |
| - icon: 'history', |
104 |
| - disabled: !doc.abilities.versions_list, |
105 |
| - callback: () => { |
106 |
| - selectHistoryModal.open(); |
107 |
| - }, |
108 |
| - show: isDesktop, |
109 |
| - }, |
110 |
| - |
111 |
| - { |
112 |
| - label: t('Copy as {{format}}', { format: 'Markdown' }), |
113 |
| - icon: 'content_copy', |
114 |
| - callback: () => { |
115 |
| - void copyCurrentEditorToClipboard('markdown'); |
116 |
| - }, |
117 |
| - }, |
118 |
| - { |
119 |
| - label: t('Copy as {{format}}', { format: 'HTML' }), |
120 |
| - icon: 'content_copy', |
121 |
| - callback: () => { |
122 |
| - void copyCurrentEditorToClipboard('html'); |
123 |
| - }, |
124 |
| - show: isFeatureFlagActivated('CopyAsHTML'), |
125 |
| - }, |
126 |
| - { |
127 |
| - label: t('Delete document'), |
128 |
| - icon: 'delete', |
129 |
| - disabled: !doc.abilities.destroy, |
130 |
| - callback: () => { |
131 |
| - setIsModalRemoveOpen(true); |
132 |
| - }, |
133 |
| - }, |
134 |
| - ]; |
135 |
| - |
136 |
| - const copyCurrentEditorToClipboard = async ( |
137 |
| - asFormat: 'html' | 'markdown', |
138 |
| - ) => { |
139 |
| - if (!editor) { |
140 |
| - toast(t('Editor unavailable'), VariantType.ERROR, { duration: 3000 }); |
141 |
| - return; |
142 |
| - } |
143 |
| - |
144 |
| - try { |
145 |
| - const editorContentFormatted = |
146 |
| - asFormat === 'html' |
147 |
| - ? await editor.blocksToHTMLLossy() |
148 |
| - : await editor.blocksToMarkdownLossy(); |
149 |
| - await navigator.clipboard.writeText(editorContentFormatted); |
150 |
| - toast(t('Copied to clipboard'), VariantType.SUCCESS, { duration: 3000 }); |
151 |
| - } catch (error) { |
152 |
| - console.error(error); |
153 |
| - toast(t('Failed to copy to clipboard'), VariantType.ERROR, { |
154 |
| - duration: 3000, |
155 |
| - }); |
156 |
| - } |
157 |
| - }; |
| 34 | + const { isSmallMobile } = useResponsiveStore(); |
158 | 35 |
|
159 | 36 | useEffect(() => {
|
160 |
| - if (selectHistoryModal.isOpen) { |
| 37 | + if (modalHistory.isOpen) { |
161 | 38 | return;
|
162 | 39 | }
|
163 | 40 |
|
164 | 41 | void queryClient.resetQueries({
|
165 | 42 | queryKey: [KEY_LIST_DOC_VERSIONS],
|
166 | 43 | });
|
167 |
| - }, [selectHistoryModal.isOpen, queryClient]); |
| 44 | + }, [modalHistory.isOpen, queryClient]); |
168 | 45 |
|
169 | 46 | return (
|
170 | 47 | <Box
|
@@ -222,55 +99,12 @@ export const DocToolBox = ({ doc }: DocToolBoxProps) => {
|
222 | 99 | </>
|
223 | 100 | )}
|
224 | 101 |
|
225 |
| - {!isSmallMobile && ( |
226 |
| - <Button |
227 |
| - color="tertiary-text" |
228 |
| - icon={ |
229 |
| - <Icon iconName="download" $theme="primary" $variation="800" /> |
230 |
| - } |
231 |
| - onClick={() => { |
232 |
| - setIsModalExportOpen(true); |
233 |
| - }} |
234 |
| - size={isSmallMobile ? 'small' : 'medium'} |
235 |
| - /> |
236 |
| - )} |
237 |
| - <DropdownMenu options={options}> |
238 |
| - <IconOptions |
239 |
| - isHorizontal |
240 |
| - $theme="primary" |
241 |
| - $padding={{ all: 'xs' }} |
242 |
| - $css={css` |
243 |
| - border-radius: 4px; |
244 |
| - &:hover { |
245 |
| - background-color: ${colorsTokens['greyscale-100']}; |
246 |
| - } |
247 |
| - ${isSmallMobile |
248 |
| - ? css` |
249 |
| - padding: 10px; |
250 |
| - border: 1px solid ${colorsTokens['greyscale-300']}; |
251 |
| - ` |
252 |
| - : ''} |
253 |
| - `} |
254 |
| - aria-label={t('Open the document options')} |
255 |
| - /> |
256 |
| - </DropdownMenu> |
257 |
| - </Box> |
258 |
| - |
259 |
| - {modalShare.isOpen && ( |
260 |
| - <DocShareModal onClose={() => modalShare.close()} doc={doc} /> |
261 |
| - )} |
262 |
| - {isModalExportOpen && ( |
263 |
| - <ModalExport onClose={() => setIsModalExportOpen(false)} doc={doc} /> |
264 |
| - )} |
265 |
| - {isModalRemoveOpen && ( |
266 |
| - <ModalRemoveDoc onClose={() => setIsModalRemoveOpen(false)} doc={doc} /> |
267 |
| - )} |
268 |
| - {selectHistoryModal.isOpen && ( |
269 |
| - <ModalSelectVersion |
270 |
| - onClose={() => selectHistoryModal.close()} |
| 102 | + <DocToolBoxLicence |
271 | 103 | doc={doc}
|
| 104 | + modalHistory={modalHistory} |
| 105 | + modalShare={modalShare} |
272 | 106 | />
|
273 |
| - )} |
| 107 | + </Box> |
274 | 108 | </Box>
|
275 | 109 | );
|
276 | 110 | };
|
0 commit comments