ConfigManager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import React, { useRef } from 'react';
  2. import {
  3. Button,
  4. Typography,
  5. Toast,
  6. Modal,
  7. Dropdown,
  8. } from '@douyinfe/semi-ui';
  9. import {
  10. Download,
  11. Upload,
  12. RotateCcw,
  13. Settings2,
  14. } from 'lucide-react';
  15. import { useTranslation } from 'react-i18next';
  16. import { exportConfig, importConfig, clearConfig, hasStoredConfig, getConfigTimestamp } from './configStorage';
  17. const ConfigManager = ({
  18. currentConfig,
  19. onConfigImport,
  20. onConfigReset,
  21. styleState,
  22. }) => {
  23. const { t } = useTranslation();
  24. const fileInputRef = useRef(null);
  25. const handleExport = () => {
  26. try {
  27. exportConfig(currentConfig);
  28. Toast.success({
  29. content: t('配置已导出到下载文件夹'),
  30. duration: 3,
  31. });
  32. } catch (error) {
  33. Toast.error({
  34. content: t('导出配置失败: ') + error.message,
  35. duration: 3,
  36. });
  37. }
  38. };
  39. const handleImportClick = () => {
  40. fileInputRef.current?.click();
  41. };
  42. const handleFileChange = async (event) => {
  43. const file = event.target.files[0];
  44. if (!file) return;
  45. try {
  46. const importedConfig = await importConfig(file);
  47. Modal.confirm({
  48. title: t('确认导入配置'),
  49. content: t('导入的配置将覆盖当前设置,是否继续?'),
  50. okText: t('确定导入'),
  51. cancelText: t('取消'),
  52. onOk: () => {
  53. onConfigImport(importedConfig);
  54. Toast.success({
  55. content: t('配置导入成功'),
  56. duration: 3,
  57. });
  58. },
  59. });
  60. } catch (error) {
  61. Toast.error({
  62. content: t('导入配置失败: ') + error.message,
  63. duration: 3,
  64. });
  65. } finally {
  66. // 重置文件输入,允许重复选择同一文件
  67. event.target.value = '';
  68. }
  69. };
  70. const handleReset = () => {
  71. Modal.confirm({
  72. title: t('重置配置'),
  73. content: t('将清除所有保存的配置并恢复默认设置,此操作不可撤销。是否继续?'),
  74. okText: t('确定重置'),
  75. cancelText: t('取消'),
  76. okButtonProps: {
  77. type: 'danger',
  78. },
  79. onOk: () => {
  80. clearConfig();
  81. onConfigReset();
  82. Toast.success({
  83. content: t('配置已重置为默认值'),
  84. duration: 3,
  85. });
  86. },
  87. });
  88. };
  89. const getConfigStatus = () => {
  90. if (hasStoredConfig()) {
  91. const timestamp = getConfigTimestamp();
  92. if (timestamp) {
  93. const date = new Date(timestamp);
  94. return t('上次保存: ') + date.toLocaleString();
  95. }
  96. return t('已有保存的配置');
  97. }
  98. return t('暂无保存的配置');
  99. };
  100. const dropdownItems = [
  101. {
  102. node: 'item',
  103. name: 'export',
  104. onClick: handleExport,
  105. children: (
  106. <div className="flex items-center gap-2">
  107. <Download size={14} />
  108. {t('导出配置')}
  109. </div>
  110. ),
  111. },
  112. {
  113. node: 'item',
  114. name: 'import',
  115. onClick: handleImportClick,
  116. children: (
  117. <div className="flex items-center gap-2">
  118. <Upload size={14} />
  119. {t('导入配置')}
  120. </div>
  121. ),
  122. },
  123. {
  124. node: 'divider',
  125. },
  126. {
  127. node: 'item',
  128. name: 'reset',
  129. onClick: handleReset,
  130. children: (
  131. <div className="flex items-center gap-2 text-red-600">
  132. <RotateCcw size={14} />
  133. {t('重置配置')}
  134. </div>
  135. ),
  136. },
  137. ];
  138. if (styleState.isMobile) {
  139. // 移动端显示简化的下拉菜单
  140. return (
  141. <>
  142. <Dropdown
  143. trigger="click"
  144. position="bottomLeft"
  145. showTick
  146. menu={dropdownItems}
  147. >
  148. <Button
  149. icon={<Settings2 size={14} />}
  150. theme="borderless"
  151. type="tertiary"
  152. size="small"
  153. className="!rounded-lg !text-gray-600 hover:!text-blue-600 hover:!bg-blue-50"
  154. />
  155. </Dropdown>
  156. <input
  157. ref={fileInputRef}
  158. type="file"
  159. accept=".json"
  160. onChange={handleFileChange}
  161. style={{ display: 'none' }}
  162. />
  163. </>
  164. );
  165. }
  166. // 桌面端显示紧凑的按钮组
  167. return (
  168. <div className="space-y-3">
  169. {/* 配置状态信息,使用较小的字体 */}
  170. <div className="text-center">
  171. <Typography.Text className="text-xs text-gray-500">
  172. {getConfigStatus()}
  173. </Typography.Text>
  174. </div>
  175. {/* 紧凑的按钮布局 */}
  176. <div className="flex gap-2">
  177. <Button
  178. icon={<Download size={12} />}
  179. size="small"
  180. theme="solid"
  181. type="primary"
  182. onClick={handleExport}
  183. className="!rounded-lg flex-1 !text-xs !h-7"
  184. >
  185. {t('导出')}
  186. </Button>
  187. <Button
  188. icon={<Upload size={12} />}
  189. size="small"
  190. theme="outline"
  191. type="primary"
  192. onClick={handleImportClick}
  193. className="!rounded-lg flex-1 !text-xs !h-7"
  194. >
  195. {t('导入')}
  196. </Button>
  197. <Button
  198. icon={<RotateCcw size={12} />}
  199. size="small"
  200. theme="borderless"
  201. type="danger"
  202. onClick={handleReset}
  203. className="!rounded-lg !text-xs !h-7 !px-2"
  204. style={{ minWidth: 'auto' }}
  205. />
  206. </div>
  207. <input
  208. ref={fileInputRef}
  209. type="file"
  210. accept=".json"
  211. onChange={handleFileChange}
  212. style={{ display: 'none' }}
  213. />
  214. </div>
  215. );
  216. };
  217. export default ConfigManager;