ConfigManager.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useRef } from 'react';
  16. import {
  17. Button,
  18. Typography,
  19. Toast,
  20. Modal,
  21. Dropdown,
  22. } from '@douyinfe/semi-ui';
  23. import {
  24. Download,
  25. Upload,
  26. RotateCcw,
  27. Settings2,
  28. } from 'lucide-react';
  29. import { useTranslation } from 'react-i18next';
  30. import { exportConfig, importConfig, clearConfig, hasStoredConfig, getConfigTimestamp } from './configStorage';
  31. const ConfigManager = ({
  32. currentConfig,
  33. onConfigImport,
  34. onConfigReset,
  35. styleState,
  36. messages,
  37. }) => {
  38. const { t } = useTranslation();
  39. const fileInputRef = useRef(null);
  40. const handleExport = () => {
  41. try {
  42. // 在导出前先保存当前配置,确保导出的是最新内容
  43. const configWithTimestamp = {
  44. ...currentConfig,
  45. timestamp: new Date().toISOString(),
  46. };
  47. localStorage.setItem('playground_config', JSON.stringify(configWithTimestamp));
  48. exportConfig(currentConfig, messages);
  49. Toast.success({
  50. content: t('配置已导出到下载文件夹'),
  51. duration: 3,
  52. });
  53. } catch (error) {
  54. Toast.error({
  55. content: t('导出配置失败: ') + error.message,
  56. duration: 3,
  57. });
  58. }
  59. };
  60. const handleImportClick = () => {
  61. fileInputRef.current?.click();
  62. };
  63. const handleFileChange = async (event) => {
  64. const file = event.target.files[0];
  65. if (!file) return;
  66. try {
  67. const importedConfig = await importConfig(file);
  68. Modal.confirm({
  69. title: t('确认导入配置'),
  70. content: t('导入的配置将覆盖当前设置,是否继续?'),
  71. okText: t('确定导入'),
  72. cancelText: t('取消'),
  73. onOk: () => {
  74. onConfigImport(importedConfig);
  75. Toast.success({
  76. content: t('配置导入成功'),
  77. duration: 3,
  78. });
  79. },
  80. });
  81. } catch (error) {
  82. Toast.error({
  83. content: t('导入配置失败: ') + error.message,
  84. duration: 3,
  85. });
  86. } finally {
  87. // 重置文件输入,允许重复选择同一文件
  88. event.target.value = '';
  89. }
  90. };
  91. const handleReset = () => {
  92. Modal.confirm({
  93. title: t('重置配置'),
  94. content: t('将清除所有保存的配置并恢复默认设置,此操作不可撤销。是否继续?'),
  95. okText: t('确定重置'),
  96. cancelText: t('取消'),
  97. okButtonProps: {
  98. type: 'danger',
  99. },
  100. onOk: () => {
  101. // 询问是否同时重置消息
  102. Modal.confirm({
  103. title: t('重置选项'),
  104. content: t('是否同时重置对话消息?选择"是"将清空所有对话记录并恢复默认示例;选择"否"将保留当前对话记录。'),
  105. okText: t('同时重置消息'),
  106. cancelText: t('仅重置配置'),
  107. okButtonProps: {
  108. type: 'danger',
  109. },
  110. onOk: () => {
  111. clearConfig();
  112. onConfigReset({ resetMessages: true });
  113. Toast.success({
  114. content: t('配置和消息已全部重置'),
  115. duration: 3,
  116. });
  117. },
  118. onCancel: () => {
  119. clearConfig();
  120. onConfigReset({ resetMessages: false });
  121. Toast.success({
  122. content: t('配置已重置,对话消息已保留'),
  123. duration: 3,
  124. });
  125. },
  126. });
  127. },
  128. });
  129. };
  130. const getConfigStatus = () => {
  131. if (hasStoredConfig()) {
  132. const timestamp = getConfigTimestamp();
  133. if (timestamp) {
  134. const date = new Date(timestamp);
  135. return t('上次保存: ') + date.toLocaleString();
  136. }
  137. return t('已有保存的配置');
  138. }
  139. return t('暂无保存的配置');
  140. };
  141. const dropdownItems = [
  142. {
  143. node: 'item',
  144. name: 'export',
  145. onClick: handleExport,
  146. children: (
  147. <div className="flex items-center gap-2">
  148. <Download size={14} />
  149. {t('导出配置')}
  150. </div>
  151. ),
  152. },
  153. {
  154. node: 'item',
  155. name: 'import',
  156. onClick: handleImportClick,
  157. children: (
  158. <div className="flex items-center gap-2">
  159. <Upload size={14} />
  160. {t('导入配置')}
  161. </div>
  162. ),
  163. },
  164. {
  165. node: 'divider',
  166. },
  167. {
  168. node: 'item',
  169. name: 'reset',
  170. onClick: handleReset,
  171. children: (
  172. <div className="flex items-center gap-2 text-red-600">
  173. <RotateCcw size={14} />
  174. {t('重置配置')}
  175. </div>
  176. ),
  177. },
  178. ];
  179. if (styleState.isMobile) {
  180. // 移动端显示简化的下拉菜单
  181. return (
  182. <>
  183. <Dropdown
  184. trigger="click"
  185. position="bottomLeft"
  186. showTick
  187. menu={dropdownItems}
  188. >
  189. <Button
  190. icon={<Settings2 size={14} />}
  191. theme="borderless"
  192. type="tertiary"
  193. size="small"
  194. className="!rounded-lg !text-gray-600 hover:!text-blue-600 hover:!bg-blue-50"
  195. />
  196. </Dropdown>
  197. <input
  198. ref={fileInputRef}
  199. type="file"
  200. accept=".json"
  201. onChange={handleFileChange}
  202. style={{ display: 'none' }}
  203. />
  204. </>
  205. );
  206. }
  207. // 桌面端显示紧凑的按钮组
  208. return (
  209. <div className="space-y-3">
  210. {/* 配置状态信息和重置按钮 */}
  211. <div className="flex items-center justify-between">
  212. <Typography.Text className="text-xs text-gray-500">
  213. {getConfigStatus()}
  214. </Typography.Text>
  215. <Button
  216. icon={<RotateCcw size={12} />}
  217. size="small"
  218. theme="borderless"
  219. type="danger"
  220. onClick={handleReset}
  221. className="!rounded-full !text-xs !px-2"
  222. />
  223. </div>
  224. {/* 导出和导入按钮 */}
  225. <div className="flex gap-2">
  226. <Button
  227. icon={<Download size={12} />}
  228. size="small"
  229. theme="solid"
  230. type="primary"
  231. onClick={handleExport}
  232. className="!rounded-lg flex-1 !text-xs !h-7"
  233. >
  234. {t('导出')}
  235. </Button>
  236. <Button
  237. icon={<Upload size={12} />}
  238. size="small"
  239. theme="outline"
  240. type="primary"
  241. onClick={handleImportClick}
  242. className="!rounded-lg flex-1 !text-xs !h-7"
  243. >
  244. {t('导入')}
  245. </Button>
  246. </div>
  247. <input
  248. ref={fileInputRef}
  249. type="file"
  250. accept=".json"
  251. onChange={handleFileChange}
  252. style={{ display: 'none' }}
  253. />
  254. </div>
  255. );
  256. };
  257. export default ConfigManager;