ImportFolderDrawer.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import React, { useState, useEffect } from 'react';
  2. import { X, FolderInput, ArrowRight, Info, Layers, Clock, Upload, Calendar } from 'lucide-react';
  3. import { isExtensionAllowed } from '../constants/fileSupport';
  4. import { useLanguage } from '../contexts/LanguageContext';
  5. import { ModelConfig, ModelType, IndexingConfig, KnowledgeGroup } from '../types';
  6. import { modelConfigService } from '../services/modelConfigService';
  7. import { knowledgeGroupService } from '../services/knowledgeGroupService';
  8. import { apiClient } from '../services/apiClient';
  9. import { useToast } from '../contexts/ToastContext';
  10. import IndexingModalWithMode from './IndexingModalWithMode';
  11. interface ImportFolderDrawerProps {
  12. isOpen: boolean;
  13. onClose: () => void;
  14. authToken: string;
  15. initialGroupId?: string;
  16. initialGroupName?: string;
  17. onImportSuccess?: () => void;
  18. }
  19. interface FileWithPath {
  20. file: File;
  21. relativePath: string;
  22. }
  23. type ImportMode = 'immediate' | 'scheduled';
  24. export const ImportFolderDrawer: React.FC<ImportFolderDrawerProps> = ({
  25. isOpen,
  26. onClose,
  27. authToken,
  28. initialGroupId,
  29. initialGroupName,
  30. onImportSuccess,
  31. }) => {
  32. const { t } = useLanguage();
  33. const { showError, showSuccess } = useToast();
  34. // Tab
  35. const [importMode, setImportMode] = useState<ImportMode>('immediate');
  36. // Immediate mode state
  37. const [localFiles, setLocalFiles] = useState<FileWithPath[]>([]);
  38. const [folderName, setFolderName] = useState('');
  39. const [targetName, setTargetName] = useState('');
  40. const [useHierarchy, setUseHierarchy] = useState(false);
  41. const fileInputRef = React.useRef<HTMLInputElement>(null);
  42. const [isIndexingConfigOpen, setIsIndexingConfigOpen] = useState(false);
  43. const [models, setModels] = useState<ModelConfig[]>([]);
  44. // Scheduled mode state
  45. const [serverPath, setServerPath] = useState('');
  46. const [scheduledTime, setScheduledTime] = useState(() => {
  47. // Default to 30 min from now
  48. const d = new Date();
  49. d.setMinutes(d.getMinutes() + 30);
  50. d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
  51. return d.toISOString().slice(0, 16); // "YYYY-MM-DDTHH:mm"
  52. });
  53. const [schedTargetName, setSchedTargetName] = useState('');
  54. const [schedUseHierarchy, setSchedUseHierarchy] = useState(false);
  55. const [isLoading, setIsLoading] = useState(false);
  56. const [allGroups, setAllGroups] = useState<KnowledgeGroup[]>([]);
  57. const [parentGroupId, setParentGroupId] = useState<string>('');
  58. const [schedParentGroupId, setSchedParentGroupId] = useState<string>('');
  59. useEffect(() => {
  60. if (isOpen) {
  61. setLocalFiles([]);
  62. setFolderName('');
  63. setTargetName(initialGroupName || '');
  64. setUseHierarchy(false);
  65. setIsIndexingConfigOpen(false);
  66. setImportMode('immediate');
  67. setServerPath('');
  68. setSchedTargetName(initialGroupName || '');
  69. setSchedUseHierarchy(false);
  70. setParentGroupId('');
  71. setSchedParentGroupId('');
  72. // Default scheduled time = 30min from now
  73. const d = new Date();
  74. d.setMinutes(d.getMinutes() + 30);
  75. d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
  76. setScheduledTime(d.toISOString().slice(0, 16));
  77. modelConfigService.getAll(authToken).then(res => {
  78. setModels(res.filter(m => m.type === ModelType.EMBEDDING));
  79. });
  80. knowledgeGroupService.getGroups().then(groups => {
  81. const flat: any[] = [];
  82. function walk(items: any[], depth = 0) {
  83. for (const g of items) {
  84. flat.push({ ...g, d: depth });
  85. if (g.children?.length) walk(g.children, depth + 1);
  86. }
  87. }
  88. walk(groups);
  89. setAllGroups(flat);
  90. });
  91. }
  92. }, [isOpen, authToken, initialGroupName]);
  93. // ---- Immediate mode handlers ----
  94. const handleLocalFolderChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  95. if (e.target.files && e.target.files.length > 0) {
  96. const allFiles = Array.from(e.target.files);
  97. const files: FileWithPath[] = allFiles
  98. .filter(file => {
  99. const ext = file.name.split('.').pop() || '';
  100. return isExtensionAllowed(ext, 'group');
  101. })
  102. .map(file => ({
  103. file,
  104. relativePath: file.webkitRelativePath || file.name,
  105. }));
  106. if (files.length === 0 && allFiles.length > 0) {
  107. showError(t('noFilesFound'));
  108. return;
  109. }
  110. setLocalFiles(files);
  111. const firstPath = allFiles[0].webkitRelativePath;
  112. if (firstPath) {
  113. const parts = firstPath.split('/');
  114. if (parts.length > 0) {
  115. const name = parts[0];
  116. setFolderName(name);
  117. if (!initialGroupId && !targetName) setTargetName(name);
  118. }
  119. }
  120. }
  121. };
  122. const handleImmediateNext = () => {
  123. if (localFiles.length === 0) {
  124. showError(t('clickToSelectFolder'));
  125. return;
  126. }
  127. if (!initialGroupId && !targetName) {
  128. showError(t('fillTargetName'));
  129. return;
  130. }
  131. setIsIndexingConfigOpen(true);
  132. };
  133. const handleConfirmConfig = async (config: IndexingConfig) => {
  134. setIsLoading(true);
  135. try {
  136. const { uploadService } = await import('../services/uploadService');
  137. if (useHierarchy) {
  138. // Step 1: Determine root group
  139. let rootGroupId = initialGroupId ?? null;
  140. if (!rootGroupId) {
  141. const newGroup = await knowledgeGroupService.createGroup({
  142. name: targetName,
  143. description: t('importedFromLocalFolder').replace('$1', folderName),
  144. parentId: parentGroupId || null,
  145. });
  146. rootGroupId = newGroup.id;
  147. }
  148. // Step 2: Collect all unique directory paths
  149. const dirSet = new Set<string>();
  150. for (const { relativePath } of localFiles) {
  151. const parts = relativePath.split('/');
  152. const dirParts = initialGroupId
  153. ? parts.slice(1, parts.length - 1)
  154. : parts.slice(0, parts.length - 1);
  155. for (let i = 1; i <= dirParts.length; i++) {
  156. dirSet.add(dirParts.slice(0, i).join('/'));
  157. }
  158. }
  159. // Step 3: Sort by depth, create groups sequentially
  160. const sortedDirs = Array.from(dirSet).sort((a, b) =>
  161. a.split('/').length - b.split('/').length
  162. );
  163. const dirToGroupId = new Map<string, string>();
  164. dirToGroupId.set(initialGroupId ? '' : folderName, rootGroupId);
  165. for (const dirPath of sortedDirs) {
  166. if (!dirPath || dirToGroupId.has(dirPath)) continue;
  167. const segments = dirPath.split('/');
  168. const segName = segments[segments.length - 1];
  169. const parentPath = segments.slice(0, segments.length - 1).join('/');
  170. const parentId = dirToGroupId.get(parentPath) ?? rootGroupId;
  171. const newGroup = await knowledgeGroupService.createGroup({ name: segName, parentId });
  172. dirToGroupId.set(dirPath, newGroup.id);
  173. }
  174. // Step 4: Upload files in parallel batches
  175. const BATCH_SIZE = 3;
  176. for (let i = 0; i < localFiles.length; i += BATCH_SIZE) {
  177. const batch = localFiles.slice(i, i + BATCH_SIZE);
  178. await Promise.all(batch.map(async ({ file, relativePath }) => {
  179. try {
  180. const parts = relativePath.split('/');
  181. const dirParts = initialGroupId
  182. ? parts.slice(1, parts.length - 1)
  183. : parts.slice(0, parts.length - 1);
  184. const fileDirPath = dirParts.join('/');
  185. const targetGroupId = dirToGroupId.get(fileDirPath) ?? rootGroupId!;
  186. const uploadedKb = await uploadService.uploadFileWithConfig(file, config, authToken);
  187. await knowledgeGroupService.addFileToGroups(uploadedKb.id, [targetGroupId]);
  188. } catch (err) {
  189. console.error(`Failed to upload ${file.name}:`, err);
  190. }
  191. }));
  192. }
  193. } else {
  194. // Single-group mode
  195. let groupId = initialGroupId ?? null;
  196. if (!groupId) {
  197. const newGroup = await knowledgeGroupService.createGroup({
  198. name: targetName,
  199. description: t('importedFromLocalFolder').replace('$1', folderName),
  200. });
  201. groupId = newGroup.id;
  202. }
  203. const BATCH_SIZE = 3;
  204. for (let i = 0; i < localFiles.length; i += BATCH_SIZE) {
  205. const batch = localFiles.slice(i, i + BATCH_SIZE);
  206. await Promise.all(batch.map(async ({ file }) => {
  207. try {
  208. const uploadedKb = await uploadService.uploadFileWithConfig(file, config, authToken);
  209. if (groupId) await knowledgeGroupService.addFileToGroups(uploadedKb.id, [groupId]);
  210. } catch (err) {
  211. console.error(`Failed to upload ${file.name}:`, err);
  212. }
  213. }));
  214. }
  215. }
  216. showSuccess(t('importComplete'));
  217. onImportSuccess?.();
  218. onClose();
  219. } catch (error: any) {
  220. showError(t('submitFailed', error.message));
  221. } finally {
  222. setIsLoading(false);
  223. setIsIndexingConfigOpen(false);
  224. }
  225. };
  226. // ---- Scheduled mode handler ----
  227. const handleScheduledSubmit = async () => {
  228. if (!serverPath.trim()) {
  229. showError(t('fillServerPath'));
  230. return;
  231. }
  232. if (!initialGroupId && !schedTargetName.trim()) {
  233. showError(t('fillTargetName'));
  234. return;
  235. }
  236. const scheduledAt = new Date(scheduledTime);
  237. if (isNaN(scheduledAt.getTime())) {
  238. showError(t('invalidDateTime' as any));
  239. return;
  240. }
  241. setIsLoading(true);
  242. try {
  243. let finalGroupId = initialGroupId || undefined;
  244. if (!finalGroupId && schedTargetName.trim()) {
  245. const newGroup = await knowledgeGroupService.createGroup({
  246. name: schedTargetName.trim(),
  247. description: t('importedFromLocalFolder').replace('$1', schedTargetName.trim()),
  248. parentId: schedParentGroupId || null,
  249. });
  250. finalGroupId = newGroup.id;
  251. }
  252. const defaultModel = models[0];
  253. await apiClient.post('/import-tasks', {
  254. sourcePath: serverPath.trim(),
  255. targetGroupId: finalGroupId,
  256. targetGroupName: schedTargetName.trim() || initialGroupName || undefined,
  257. embeddingModelId: defaultModel?.id,
  258. scheduledAt: scheduledAt.toISOString(),
  259. chunkSize: 500,
  260. chunkOverlap: 50,
  261. mode: 'fast',
  262. useHierarchy: schedUseHierarchy,
  263. });
  264. showSuccess(t('scheduleTaskCreated'));
  265. onImportSuccess?.();
  266. onClose();
  267. } catch (error: any) {
  268. showError(t('submitFailed', error.message));
  269. } finally {
  270. setIsLoading(false);
  271. }
  272. };
  273. if (!isOpen) return null;
  274. return (
  275. <>
  276. <div className="fixed inset-0 z-50 flex justify-end">
  277. <div className="absolute inset-0 bg-black/20 backdrop-blur-sm transition-opacity" onClick={onClose} />
  278. <div className="relative w-full max-w-md bg-white shadow-2xl flex flex-col h-full animate-in slide-in-from-right duration-300">
  279. {/* Header */}
  280. <div className="px-6 py-4 border-b border-slate-100 flex items-center justify-between shrink-0 bg-white">
  281. <h2 className="text-lg font-bold text-slate-800 flex items-center gap-2">
  282. <FolderInput className="w-5 h-5 text-blue-600" />
  283. {t('importFolderTitle')}
  284. </h2>
  285. <button onClick={onClose} className="p-2 -mr-2 text-slate-400 hover:text-slate-600 hover:bg-slate-50 rounded-full transition-colors">
  286. <X size={20} />
  287. </button>
  288. </div>
  289. {/* Mode Tabs */}
  290. <div className="flex border-b border-slate-100 shrink-0">
  291. <button
  292. onClick={() => setImportMode('immediate')}
  293. className={`flex-1 flex items-center justify-center gap-2 py-3 text-sm font-semibold transition-colors border-b-2 ${importMode === 'immediate'
  294. ? 'border-blue-600 text-blue-600 bg-blue-50/40'
  295. : 'border-transparent text-slate-500 hover:text-slate-700'
  296. }`}
  297. >
  298. <Upload size={15} />
  299. {t('importImmediate')}
  300. </button>
  301. <button
  302. onClick={() => setImportMode('scheduled')}
  303. className={`flex-1 flex items-center justify-center gap-2 py-3 text-sm font-semibold transition-colors border-b-2 ${importMode === 'scheduled'
  304. ? 'border-blue-600 text-blue-600 bg-blue-50/40'
  305. : 'border-transparent text-slate-500 hover:text-slate-700'
  306. }`}
  307. >
  308. <Clock size={15} />
  309. {t('importScheduled')}
  310. </button>
  311. </div>
  312. {/* Body */}
  313. <div className="flex-1 overflow-y-auto p-6 space-y-5">
  314. {importMode === 'immediate' ? (
  315. <>
  316. {/* Immediate: folder picker */}
  317. <div
  318. onClick={() => fileInputRef.current?.click()}
  319. className="border-2 border-dashed border-slate-200 rounded-xl p-8 flex flex-col items-center justify-center gap-3 cursor-pointer hover:border-blue-400 hover:bg-blue-50 transition-all group"
  320. >
  321. <div className="w-12 h-12 bg-slate-50 text-slate-400 rounded-full flex items-center justify-center group-hover:bg-blue-100 group-hover:text-blue-600 transition-colors">
  322. <FolderInput size={24} />
  323. </div>
  324. <div className="text-center">
  325. <p className="text-sm font-medium text-slate-700">
  326. {localFiles.length > 0
  327. ? t('selectedFilesCount').replace('$1', localFiles.length.toString())
  328. : t('clickToSelectFolder')}
  329. </p>
  330. <p className="text-xs text-slate-400 mt-1">
  331. {localFiles.length > 0 ? folderName : t('selectFolderTip')}
  332. </p>
  333. </div>
  334. <input
  335. type="file"
  336. ref={fileInputRef}
  337. onChange={handleLocalFolderChange}
  338. className="hidden"
  339. multiple
  340. // @ts-ignore
  341. webkitdirectory=""
  342. directory=""
  343. />
  344. </div>
  345. {/* Target group */}
  346. <div className="space-y-1.5">
  347. <label className="text-sm font-medium text-slate-700">{t('lblTargetGroup')}</label>
  348. <input
  349. type="text"
  350. value={targetName}
  351. onChange={e => setTargetName(e.target.value)}
  352. disabled={!!initialGroupId}
  353. placeholder={t('placeholderNewGroup')}
  354. className={`w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none ${initialGroupId ? 'bg-slate-50 text-slate-500' : ''}`}
  355. />
  356. {initialGroupId && <p className="text-xs text-slate-400">{t('importToCurrentGroup')}</p>}
  357. </div>
  358. {!initialGroupId && (
  359. <div className="space-y-1.5">
  360. <label className="text-sm font-medium text-slate-700">{t('parentCategory') || 'Parent Category'}</label>
  361. <select
  362. value={parentGroupId}
  363. onChange={e => setParentGroupId(e.target.value)}
  364. className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none"
  365. >
  366. <option value="">{t('allGroups' as any) || '-- Root --'}</option>
  367. {allGroups.map((g: any) => (
  368. <option key={g.id} value={g.id}>
  369. {'\u00A0'.repeat(g.d * 4)}{g.name}
  370. </option>
  371. ))}
  372. </select>
  373. </div>
  374. )}
  375. {/* Hierarchy toggle */}
  376. <HierarchyToggle value={useHierarchy} onChange={setUseHierarchy} t={t} />
  377. </>
  378. ) : (
  379. <>
  380. {/* Scheduled: server path */}
  381. <div className="bg-amber-50 border border-amber-100 rounded-lg p-3 text-sm text-amber-800 flex items-start gap-2">
  382. <Info className="w-4 h-4 mt-0.5 shrink-0" />
  383. <p className="text-xs">{t('scheduledImportTip')}</p>
  384. </div>
  385. <div className="space-y-1.5">
  386. <label className="text-sm font-medium text-slate-700">{t('lblServerPath')}</label>
  387. <input
  388. type="text"
  389. value={serverPath}
  390. onChange={e => setServerPath(e.target.value)}
  391. placeholder={t('placeholderServerPath')}
  392. className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none font-mono"
  393. />
  394. </div>
  395. {/* Target group */}
  396. <div className="space-y-1.5">
  397. <label className="text-sm font-medium text-slate-700">{t('lblTargetGroup')}</label>
  398. <input
  399. type="text"
  400. value={schedTargetName}
  401. onChange={e => setSchedTargetName(e.target.value)}
  402. disabled={!!initialGroupId}
  403. placeholder={t('placeholderNewGroup')}
  404. className={`w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none ${initialGroupId ? 'bg-slate-50 text-slate-500' : ''}`}
  405. />
  406. {initialGroupId && <p className="text-xs text-slate-400">{t('importToCurrentGroup')}</p>}
  407. </div>
  408. {!initialGroupId && (
  409. <div className="space-y-1.5">
  410. <label className="text-sm font-medium text-slate-700">{t('parentCategory') || 'Parent Category'}</label>
  411. <select
  412. value={schedParentGroupId}
  413. onChange={e => setSchedParentGroupId(e.target.value)}
  414. className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none"
  415. >
  416. <option value="">{t('allGroups' as any) || '-- Root --'}</option>
  417. {allGroups.map((g: any) => (
  418. <option key={g.id} value={g.id}>
  419. {'\u00A0'.repeat(g.d * 4)}{g.name}
  420. </option>
  421. ))}
  422. </select>
  423. </div>
  424. )}
  425. {/* Scheduled datetime */}
  426. <div className="space-y-1.5">
  427. <label className="text-sm font-medium text-slate-700 flex items-center gap-1.5">
  428. <Calendar size={14} className="text-blue-500" />
  429. {t('lblScheduledTime')}
  430. </label>
  431. <input
  432. type="datetime-local"
  433. value={scheduledTime}
  434. onChange={e => setScheduledTime(e.target.value)}
  435. className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none"
  436. />
  437. <p className="text-xs text-slate-400">{t('scheduledTimeHint')}</p>
  438. </div>
  439. {/* Hierarchy toggle */}
  440. <HierarchyToggle value={schedUseHierarchy} onChange={setSchedUseHierarchy} t={t} />
  441. </>
  442. )}
  443. </div>
  444. {/* Footer */}
  445. <div className="p-6 border-t border-slate-100 shrink-0 flex gap-3 bg-slate-50">
  446. <button
  447. onClick={onClose}
  448. className="flex-1 px-4 py-2 text-sm font-medium text-slate-600 hover:bg-slate-200 rounded-lg transition-colors"
  449. >
  450. {t('cancel')}
  451. </button>
  452. {importMode === 'immediate' ? (
  453. <button
  454. onClick={handleImmediateNext}
  455. className="flex-1 px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-lg shadow-sm flex justify-center items-center gap-2 transition-all"
  456. disabled={isLoading}
  457. >
  458. <span>{isLoading ? t('uploading') : t('nextStep')}</span>
  459. <ArrowRight size={16} />
  460. </button>
  461. ) : (
  462. <button
  463. onClick={handleScheduledSubmit}
  464. className="flex-1 px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-lg shadow-sm flex justify-center items-center gap-2 transition-all"
  465. disabled={isLoading}
  466. >
  467. <Clock size={16} />
  468. <span>{isLoading ? t('uploading') : t('scheduleImport')}</span>
  469. </button>
  470. )}
  471. </div>
  472. </div>
  473. </div>
  474. {/* Indexing Config Modal (immediate mode only) */}
  475. <IndexingModalWithMode
  476. isOpen={isIndexingConfigOpen}
  477. onClose={() => setIsIndexingConfigOpen(false)}
  478. files={[]}
  479. embeddingModels={models}
  480. defaultEmbeddingId={models.length > 0 ? models[0].id : ''}
  481. onConfirm={handleConfirmConfig}
  482. isReconfiguring={false}
  483. />
  484. </>
  485. );
  486. };
  487. /** Reusable hierarchy toggle */
  488. const HierarchyToggle: React.FC<{
  489. value: boolean;
  490. onChange: (v: boolean) => void;
  491. t: (key: string) => string;
  492. }> = ({ value, onChange, t }) => (
  493. <label className="flex items-center gap-3 cursor-pointer select-none">
  494. <div className="relative">
  495. <input
  496. type="checkbox"
  497. checked={value}
  498. onChange={e => onChange(e.target.checked)}
  499. className="sr-only"
  500. />
  501. <div className={`w-10 h-5 rounded-full transition-colors ${value ? 'bg-blue-600' : 'bg-slate-200'}`} />
  502. <div className={`absolute top-0.5 left-0.5 w-4 h-4 rounded-full bg-white shadow transition-transform ${value ? 'translate-x-5' : ''}`} />
  503. </div>
  504. <div>
  505. <p className="text-sm font-medium text-slate-700 flex items-center gap-1.5">
  506. <Layers size={14} className="text-blue-500" />
  507. {t('useHierarchyImport')}
  508. </p>
  509. <p className="text-xs text-slate-400 mt-0.5">{t('useHierarchyImportDesc')}</p>
  510. </div>
  511. </label>
  512. );