SelectCollectionsModal.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import React, {
  2. useCallback, useState, useEffect, type JSX,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import {
  6. Modal, ModalHeader, ModalBody, ModalFooter,
  7. } from 'reactstrap';
  8. import { apiPost } from '~/client/util/apiv1-client';
  9. import { toastError, toastSuccess } from '~/client/util/toastr';
  10. const GROUPS_PAGE = [
  11. 'pages', 'revisions', 'tags', 'pagetagrelations', 'pageredirects', 'comments', 'sharelinks',
  12. ];
  13. const GROUPS_USER = [
  14. 'users', 'externalaccounts', 'usergroups', 'usergrouprelations',
  15. 'externalusergroups', 'externalusergrouprelations',
  16. 'useruisettings', 'editorsettings', 'bookmarks', 'bookmarkfolders', 'subscriptions',
  17. 'inappnotificationsettings',
  18. ];
  19. const GROUPS_CONFIG = [
  20. 'configs', 'migrations', 'updateposts', 'globalnotificationsettings', 'slackappintegrations',
  21. 'growiplugins',
  22. ];
  23. const ALL_GROUPED_COLLECTIONS = GROUPS_PAGE.concat(GROUPS_USER).concat(GROUPS_CONFIG);
  24. type Props = {
  25. isOpen: boolean,
  26. onExportingRequested: () => void,
  27. onClose: () => void,
  28. collections: string[],
  29. isAllChecked?: boolean,
  30. };
  31. const SelectCollectionsModal = (props: Props): JSX.Element => {
  32. const { t } = useTranslation();
  33. const {
  34. isOpen, onExportingRequested, onClose, collections, isAllChecked,
  35. } = props;
  36. const [selectedCollections, setSelectedCollections] = useState<Set<string>>(new Set());
  37. const toggleCheckbox = useCallback((e) => {
  38. const { target } = e;
  39. const { name, checked } = target;
  40. setSelectedCollections((prevState) => {
  41. const selectedCollections = new Set(prevState);
  42. if (checked) {
  43. selectedCollections.add(name);
  44. }
  45. else {
  46. selectedCollections.delete(name);
  47. }
  48. return selectedCollections;
  49. });
  50. }, []);
  51. const checkAll = useCallback(() => {
  52. setSelectedCollections(new Set(collections));
  53. }, [collections]);
  54. const uncheckAll = useCallback(() => {
  55. setSelectedCollections(new Set());
  56. }, []);
  57. const doExport = useCallback(async(e) => {
  58. e.preventDefault();
  59. try {
  60. // TODO: use apiv3Post
  61. const result = await apiPost<any>('/v3/export', { collections: Array.from(selectedCollections) });
  62. if (!result.ok) {
  63. throw new Error('Error occured.');
  64. }
  65. toastSuccess('Export process has requested.');
  66. onExportingRequested();
  67. onClose();
  68. uncheckAll();
  69. }
  70. catch (err) {
  71. toastError(err);
  72. }
  73. }, [onClose, onExportingRequested, selectedCollections, uncheckAll]);
  74. const validateForm = useCallback(() => {
  75. return selectedCollections.size > 0;
  76. }, [selectedCollections.size]);
  77. const renderWarnForUser = useCallback(() => {
  78. // whether selectedCollections includes one of GROUPS_USER
  79. const isUserRelatedDataSelected = GROUPS_USER.some((collectionName) => {
  80. return selectedCollections.has(collectionName);
  81. });
  82. if (!isUserRelatedDataSelected) {
  83. return <></>;
  84. }
  85. const html = t('admin:export_management.desc_password_seed');
  86. return (
  87. <div className="card">
  88. <div className="card-body">
  89. {/* eslint-disable-next-line react/no-danger */}
  90. <p className="card-text" dangerouslySetInnerHTML={{ __html: html }} />
  91. </div>
  92. </div>
  93. );
  94. }, [selectedCollections, t]);
  95. const renderCheckboxes = useCallback((collectionNames, color?) => {
  96. const checkboxColor = color ? `form-check-${color}` : 'form-check-info';
  97. return (
  98. <div className={`form-check ${checkboxColor}`}>
  99. <div className="row">
  100. {collectionNames.map((collectionName) => {
  101. return (
  102. <div className="col-sm-6 my-1" key={collectionName}>
  103. <input
  104. type="checkbox"
  105. className="form-check-input"
  106. id={collectionName}
  107. name={collectionName}
  108. value={collectionName}
  109. checked={selectedCollections.has(collectionName)}
  110. onChange={toggleCheckbox}
  111. />
  112. <label className="form-label text-capitalize form-check-label ms-3" htmlFor={collectionName}>
  113. {collectionName}
  114. </label>
  115. </div>
  116. );
  117. })}
  118. </div>
  119. </div>
  120. );
  121. }, [selectedCollections, toggleCheckbox]);
  122. const renderGroups = useCallback((groupList, color?) => {
  123. const collectionNames = groupList.filter((collectionName) => {
  124. return collections.includes(collectionName);
  125. });
  126. return renderCheckboxes(collectionNames, color);
  127. }, [collections, renderCheckboxes]);
  128. const renderOthers = useCallback(() => {
  129. const collectionNames = collections.filter((collectionName) => {
  130. return !ALL_GROUPED_COLLECTIONS.includes(collectionName);
  131. });
  132. return renderCheckboxes(collectionNames);
  133. }, [collections, renderCheckboxes]);
  134. useEffect(() => {
  135. if (isAllChecked) checkAll();
  136. }, [isAllChecked, checkAll]);
  137. return (
  138. <Modal size="lg" isOpen={isOpen} toggle={onClose}>
  139. <ModalHeader tag="h4" toggle={onClose} className="text-info">
  140. {t('admin:export_management.export_collections')}
  141. </ModalHeader>
  142. <form onSubmit={doExport}>
  143. <ModalBody>
  144. <div className="row">
  145. <div className="col-sm-12">
  146. <button type="button" className="btn btn-sm btn-outline-secondary me-2" onClick={checkAll}>
  147. <span className="material-symbols-outlined">check_box</span> {t('admin:export_management.check_all')}
  148. </button>
  149. <button type="button" className="btn btn-sm btn-outline-secondary me-2" onClick={uncheckAll}>
  150. <span className="material-symbols-outlined">check_box_outline_blank</span> {t('admin:export_management.uncheck_all')}
  151. </button>
  152. </div>
  153. </div>
  154. <div className="row mt-4">
  155. <div className="col-sm-12">
  156. <h3 className="admin-setting-header">MongoDB Page Collections</h3>
  157. {renderGroups(GROUPS_PAGE)}
  158. </div>
  159. </div>
  160. <div className="row mt-4">
  161. <div className="col-sm-12">
  162. <h3 className="admin-setting-header">MongoDB User Collections</h3>
  163. {renderGroups(GROUPS_USER, 'danger')}
  164. {renderWarnForUser()}
  165. </div>
  166. </div>
  167. <div className="row mt-4">
  168. <div className="col-sm-12">
  169. <h3 className="admin-setting-header">MongoDB Config Collections</h3>
  170. {renderGroups(GROUPS_CONFIG)}
  171. </div>
  172. </div>
  173. <div className="row mt-4">
  174. <div className="col-sm-12">
  175. <h3 className="admin-setting-header">MongoDB Other Collections</h3>
  176. {renderOthers()}
  177. </div>
  178. </div>
  179. </ModalBody>
  180. <ModalFooter>
  181. <button type="button" className="btn btn-sm btn-outline-secondary" onClick={onClose}>{t('admin:export_management.cancel')}</button>
  182. <button type="submit" className="btn btn-sm btn-primary" disabled={!validateForm()}>{t('admin:export_management.export')}</button>
  183. </ModalFooter>
  184. </form>
  185. </Modal>
  186. );
  187. };
  188. export default SelectCollectionsModal;