G2GDataTransfer.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import * as toastr from 'toastr';
  4. import { useGenerateTransferKeyWithThrottle } from '~/client/services/g2g-transfer';
  5. import { toastError } from '~/client/util/apiNotification';
  6. import { apiv3Get } from '~/client/util/apiv3-client';
  7. import { useAdminSocket } from '~/stores/socket-io';
  8. import customAxios from '~/utils/axios';
  9. import CustomCopyToClipBoard from '../Common/CustomCopyToClipBoard';
  10. import G2GDataTransferExportForm from './G2GDataTransferExportForm';
  11. const IGNORED_COLLECTION_NAMES = [
  12. 'sessions', 'rlflx', 'activities', 'attachmentFiles.files', 'attachmentFiles.chunks',
  13. ];
  14. const G2GDataTransfer = (): JSX.Element => {
  15. const { data: socket } = useAdminSocket();
  16. const { t } = useTranslation();
  17. const [startTransferKey, setStartTransferKey] = useState('');
  18. const [collections, setCollections] = useState<string[]>([]);
  19. const [selectedCollections, setSelectedCollections] = useState<Set<string>>(new Set());
  20. const [optionsMap, setOptionsMap] = useState<any>({});
  21. const [isShowExportForm, setShowExportForm] = useState(false);
  22. const [isExporting, setExporting] = useState(false);
  23. // TODO: データのエクスポートが完了したことが分かるようにする
  24. const [isExported, setExported] = useState(false);
  25. const updateSelectedCollections = (newSelectedCollections: Set<string>) => {
  26. setSelectedCollections(newSelectedCollections);
  27. };
  28. const updateOptionsMap = (newOptionsMap: any) => {
  29. setOptionsMap(newOptionsMap);
  30. };
  31. const onChangeTransferKeyHandler = useCallback((e) => {
  32. setStartTransferKey(e.target.value);
  33. }, []);
  34. const setCollectionsAndSelectedCollections = useCallback(async() => {
  35. const [{ data: collectionsData }, { data: statusData }] = await Promise.all([
  36. apiv3Get<{collections: any[]}>('/mongo/collections', {}),
  37. apiv3Get<{status: { zipFileStats: any[], isExporting: boolean, progressList: any[] }}>('/export/status', {}),
  38. ]);
  39. // filter only not ignored collection names
  40. const filteredCollections = collectionsData.collections.filter((collectionName) => {
  41. return !IGNORED_COLLECTION_NAMES.includes(collectionName);
  42. });
  43. setCollections(filteredCollections);
  44. setSelectedCollections(new Set(filteredCollections));
  45. setExporting(statusData.status.isExporting);
  46. }, []);
  47. const setupWebsocketEventHandler = useCallback(() => {
  48. if (socket != null) {
  49. // websocket event
  50. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  51. setExporting(true);
  52. });
  53. // websocket event
  54. socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
  55. setExporting(false);
  56. setExported(true);
  57. // TODO: toastSuccess, toastError
  58. toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
  59. closeButton: true,
  60. progressBar: true,
  61. newestOnTop: false,
  62. showDuration: '100',
  63. hideDuration: '100',
  64. timeOut: '1200',
  65. extendedTimeOut: '150',
  66. });
  67. });
  68. }
  69. }, [socket]);
  70. const { transferKey, generateTransferKeyWithThrottle } = useGenerateTransferKeyWithThrottle();
  71. const onClickHandler = useCallback(() => {
  72. generateTransferKeyWithThrottle();
  73. }, [generateTransferKeyWithThrottle]);
  74. const startTransfer = useCallback(async(e) => {
  75. e.preventDefault();
  76. try {
  77. await customAxios.post('/_api/v3/g2g-transfer/transfer', {
  78. transferKey: startTransferKey,
  79. collections: selectedCollections,
  80. optionsMap,
  81. });
  82. }
  83. catch (errs) {
  84. toastError('Failed to transfer');
  85. }
  86. }, [startTransferKey, selectedCollections, optionsMap]);
  87. useEffect(() => {
  88. setCollectionsAndSelectedCollections();
  89. setupWebsocketEventHandler();
  90. }, [setCollectionsAndSelectedCollections, setupWebsocketEventHandler]);
  91. return (
  92. <div data-testid="admin-export-archive-data">
  93. <h2 className="border-bottom">{t('admin:g2g_data_transfer.transfer_data_to_another_growi')}</h2>
  94. <button type="button" className="btn btn-outline-secondary mt-4" disabled={isExporting} onClick={() => setShowExportForm(!isShowExportForm)}>
  95. {t('admin:g2g_data_transfer.advanced_options')}
  96. </button>
  97. {collections.length !== 0 && (
  98. <div className={isShowExportForm ? '' : 'd-none'}>
  99. <G2GDataTransferExportForm
  100. allCollectionNames={collections}
  101. selectedCollections={selectedCollections}
  102. updateSelectedCollections={updateSelectedCollections}
  103. optionsMap={optionsMap}
  104. updateOptionsMap={updateOptionsMap}
  105. />
  106. </div>
  107. )}
  108. <form onSubmit={startTransfer}>
  109. <div className="form-group row mt-3">
  110. <div className="col-9">
  111. <input
  112. className="form-control"
  113. type="text"
  114. placeholder={t('admin:g2g_data_transfer.paste_transfer_key')}
  115. onChange={onChangeTransferKeyHandler}
  116. required
  117. />
  118. </div>
  119. <div className="col-3">
  120. <button type="submit" className="btn btn-primary w-100">{t('admin:g2g_data_transfer.start_transfer')}</button>
  121. </div>
  122. </div>
  123. </form>
  124. <h2 className="border-bottom mt-5">{t('admin:g2g_data_transfer.transfer_data_to_this_growi')}</h2>
  125. <div className="form-group row mt-4">
  126. <div className="col-md-3">
  127. <button type="button" className="btn btn-primary w-100" onClick={onClickHandler}>
  128. {t('admin:g2g_data_transfer.publish_transfer_key')}
  129. </button>
  130. </div>
  131. <div className="col-md-9">
  132. <div className="input-group-prepend mx-1">
  133. <input className="form-control" type="text" value={transferKey} readOnly />
  134. <CustomCopyToClipBoard textToBeCopied={transferKey} message="admin:slack_integration.copied_to_clipboard" />
  135. </div>
  136. </div>
  137. </div>
  138. <div className="alert alert-warning mt-4">
  139. <p className="mb-1">{t('admin:g2g_data_transfer.transfer_key_limit')}</p>
  140. <p className="mb-1">{t('admin:g2g_data_transfer.once_transfer_key_used')}</p>
  141. <p className="mb-0">{t('admin:g2g_data_transfer.transfer_to_growi_cloud')}</p>
  142. </div>
  143. </div>
  144. );
  145. };
  146. export default G2GDataTransfer;