G2GDataTransfer.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import * as toastr from 'toastr';
  4. import { apiv3Get } from '~/client/util/apiv3-client';
  5. import { useAdminSocket } from '~/stores/socket-io';
  6. import CustomCopyToClipBoard from '../Common/CustomCopyToClipBoard';
  7. import SelectCollectionsModal from './ExportArchiveData/SelectCollectionsModal';
  8. const IGNORED_COLLECTION_NAMES = [
  9. 'sessions', 'rlflx', 'activities',
  10. ];
  11. const G2GDataTransfer = (): JSX.Element => {
  12. const { data: socket } = useAdminSocket();
  13. const { t } = useTranslation();
  14. const [collections, setCollections] = useState<any[]>([]);
  15. const [isExportModalOpen, setExportModalOpen] = useState(false);
  16. const [isExporting, setExporting] = useState(false);
  17. // TODO: データのエクスポートが完了したことが分かるようにする
  18. const [isExported, setExported] = useState(false);
  19. const [transferKey, setTransferKey] = useState('');
  20. const fetchData = useCallback(async() => {
  21. const [{ data: collectionsData }, { data: statusData }] = await Promise.all([
  22. apiv3Get<{collections: any[]}>('/mongo/collections', {}),
  23. apiv3Get<{status: { zipFileStats: any[], isExporting: boolean, progressList: any[] }}>('/export/status', {}),
  24. ]);
  25. // filter only not ignored collection names
  26. const filteredCollections = collectionsData.collections.filter((collectionName) => {
  27. return !IGNORED_COLLECTION_NAMES.includes(collectionName);
  28. });
  29. setCollections(filteredCollections);
  30. setExporting(statusData.status.isExporting);
  31. }, []);
  32. const setupWebsocketEventHandler = useCallback(() => {
  33. if (socket != null) {
  34. // websocket event
  35. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  36. setExporting(true);
  37. });
  38. // websocket event
  39. socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
  40. setExporting(false);
  41. setExported(true);
  42. // TODO: toastSuccess, toastError
  43. toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
  44. closeButton: true,
  45. progressBar: true,
  46. newestOnTop: false,
  47. showDuration: '100',
  48. hideDuration: '100',
  49. timeOut: '1200',
  50. extendedTimeOut: '150',
  51. });
  52. });
  53. }
  54. }, [socket]);
  55. const publishTransferKey = () => {
  56. // 移行キー発行の処理
  57. setTransferKey('transferKey');
  58. };
  59. const transferData = () => {
  60. // データ移行の処理
  61. };
  62. const exportingRequestedHandler = useCallback(() => {}, []);
  63. useEffect(() => {
  64. fetchData();
  65. setupWebsocketEventHandler();
  66. }, [fetchData, setupWebsocketEventHandler]);
  67. return (
  68. <div data-testid="admin-export-archive-data">
  69. <h2 className="border-bottom">{t('g2g_data_transfer.transfer_data_to_another_growi')}</h2>
  70. <button type="button" className="btn btn-outline-secondary mt-4" disabled={isExporting} onClick={() => setExportModalOpen(true)}>
  71. {t('g2g_data_transfer.advanced_options')}
  72. </button>
  73. <form onSubmit={transferData}>
  74. <div className="form-group row mt-3">
  75. <div className="col-9">
  76. <input className="form-control" type="text" placeholder={t('g2g_data_transfer.paste_transfer_key')} />
  77. </div>
  78. <div className="col-3">
  79. <button type="submit" className="btn btn-primary w-100">{t('g2g_data_transfer.start_transfer')}</button>
  80. </div>
  81. </div>
  82. </form>
  83. <h2 className="border-bottom mt-5">{t('g2g_data_transfer.transfer_data_to_this_growi')}</h2>
  84. <div className="form-group row mt-4">
  85. <div className="col-md-3">
  86. <button type="button" className="btn btn-primary w-100" onClick={publishTransferKey}>{t('g2g_data_transfer.publish_transfer_key')}</button>
  87. </div>
  88. <div className="col-md-9">
  89. <div className="input-group-prepend mx-1">
  90. <input className="form-control" type="text" value={transferKey} readOnly />
  91. <CustomCopyToClipBoard textToBeCopied={transferKey} message="admin:slack_integration.copied_to_clipboard" />
  92. </div>
  93. </div>
  94. </div>
  95. <div className="alert alert-warning mt-4">
  96. <p className="mb-1">{t('g2g_data_transfer.transfer_key_limit')}</p>
  97. <p className="mb-1">{t('g2g_data_transfer.once_transfer_key_used')}</p>
  98. <p className="mb-0">{t('g2g_data_transfer.transfer_to_growi_cloud')}</p>
  99. </div>
  100. <SelectCollectionsModal
  101. isOpen={isExportModalOpen}
  102. onExportingRequested={exportingRequestedHandler}
  103. onClose={() => setExportModalOpen(false)}
  104. collections={collections}
  105. />
  106. </div>
  107. );
  108. };
  109. export default G2GDataTransfer;