CreateNoteFromPDFDialog.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import React, { useState, useEffect } from 'react';
  2. import { X, Loader, Image as ImageIcon, Box } from 'lucide-react';
  3. import { ocrService } from '../services/ocrService';
  4. import { knowledgeGroupService } from '../services/knowledgeGroupService';
  5. import { KnowledgeGroup } from '../types';
  6. import { useLanguage } from '../contexts/LanguageContext';
  7. import { useToast } from '../contexts/ToastContext';
  8. interface CreateNoteFromPDFDialogProps {
  9. screenshot: Blob;
  10. extractedText: string;
  11. onSave: (title: string, content: string, groupId?: string) => Promise<void>;
  12. onCancel: () => void;
  13. authToken: string;
  14. initialGroupId?: string;
  15. initialPageNumber?: number;
  16. }
  17. export const CreateNoteFromPDFDialog: React.FC<CreateNoteFromPDFDialogProps> = ({
  18. screenshot,
  19. extractedText,
  20. onSave,
  21. onCancel,
  22. authToken,
  23. initialGroupId,
  24. initialPageNumber,
  25. }) => {
  26. const { t } = useLanguage();
  27. const { showToast } = useToast();
  28. const defaultTitle = initialPageNumber
  29. ? `${t('createPDFNote')} - ${t('page')} ${initialPageNumber} - ${new Date().toLocaleString()}`
  30. : `${t('createPDFNote')} - ${new Date().toLocaleString()}`;
  31. const [title, setTitle] = useState(defaultTitle);
  32. const [content, setContent] = useState(extractedText);
  33. const [selectedGroupId, setSelectedGroupId] = useState<string | undefined>(initialGroupId);
  34. const [groups, setGroups] = useState<KnowledgeGroup[]>([]);
  35. const [saving, setSaving] = useState(false);
  36. const [ocrLoading, setOcrLoading] = useState(false);
  37. const [screenshotUrl, setScreenshotUrl] = useState<string>('');
  38. useEffect(() => {
  39. if (authToken) {
  40. knowledgeGroupService.getGroups().then(setGroups).catch(console.error);
  41. }
  42. }, [authToken]);
  43. useEffect(() => {
  44. const url = URL.createObjectURL(screenshot);
  45. setScreenshotUrl(url);
  46. // Trigger OCR if initial text is empty
  47. if (!extractedText && authToken) {
  48. setOcrLoading(true);
  49. ocrService.recognizeText(authToken, screenshot)
  50. .then(text => setContent(text))
  51. .catch(err => console.error('OCR failed:', err))
  52. .finally(() => setOcrLoading(false));
  53. }
  54. return () => URL.revokeObjectURL(url);
  55. }, [screenshot, extractedText, authToken]);
  56. const handleSave = async () => {
  57. // ナレッジグループが選択されているか確認
  58. if (!selectedGroupId) {
  59. showToast('warning', t('pleaseSelectKnowledgeGroupFirst')); // 使用 toast 提示用户先选择知识组
  60. return;
  61. }
  62. setSaving(true);
  63. try {
  64. await onSave(title, content, selectedGroupId);
  65. } catch (error) {
  66. console.error('Failed to save note:', error);
  67. } finally {
  68. setSaving(false);
  69. }
  70. };
  71. return (
  72. <div className="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50 p-4">
  73. <div className="bg-white rounded-lg w-full max-w-3xl max-h-[90vh] flex flex-col">
  74. {/* Header */}
  75. <div className="flex items-center justify-between p-4 border-b">
  76. <h2 className="text-lg font-semibold text-gray-900">{t('createPDFNote')}</h2>
  77. <button
  78. onClick={onCancel}
  79. className="p-2 text-gray-400 hover:text-gray-600 transition-colors"
  80. disabled={saving}
  81. >
  82. <X size={20} />
  83. </button>
  84. </div>
  85. {/* Content */}
  86. <div className="flex-1 overflow-y-auto p-4 space-y-4">
  87. {/* Screenshot Preview */}
  88. <div className="space-y-2">
  89. <label className="block text-sm font-medium text-gray-700">
  90. <ImageIcon size={16} className="inline mr-1" />
  91. {t('screenshotPreview')}
  92. </label>
  93. <div className="border rounded-lg p-2 bg-gray-50">
  94. {screenshotUrl && (
  95. <img
  96. src={screenshotUrl}
  97. alt="PDF Selection"
  98. className="max-w-full h-auto rounded"
  99. />
  100. )}
  101. </div>
  102. </div>
  103. {/* Knowledge Group selector */}
  104. <div className="space-y-2">
  105. <label className="block text-sm font-medium text-gray-700">
  106. <Box size={16} className="inline mr-1" />
  107. {t('associateKnowledgeGroup')}
  108. </label>
  109. <select
  110. value={selectedGroupId || ''}
  111. onChange={(e) => setSelectedGroupId(e.target.value || undefined)}
  112. className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none"
  113. disabled={saving}
  114. >
  115. <option value="">{t('globalNoSpecificGroup')}</option>
  116. {groups.map(group => (
  117. <option key={group.id} value={group.id}>
  118. {group.name}
  119. </option>
  120. ))}
  121. </select>
  122. </div>
  123. {/* Title Input */}
  124. <div className="space-y-2">
  125. <label className="block text-sm font-medium text-gray-700">
  126. {t('title')}
  127. </label>
  128. <input
  129. type="text"
  130. value={title}
  131. onChange={(e) => setTitle(e.target.value)}
  132. className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
  133. placeholder={t('enterNoteTitle')}
  134. disabled={saving}
  135. />
  136. </div>
  137. {/* Content Textarea */}
  138. <div className="space-y-2">
  139. <label className="block text-sm font-medium text-gray-700">
  140. {t('contentOCR')}
  141. </label>
  142. <textarea
  143. value={content}
  144. onChange={(e) => setContent(e.target.value)}
  145. className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent font-mono text-sm"
  146. rows={10}
  147. placeholder={ocrLoading ? t('extractingText') : t('placeholderText')}
  148. disabled={saving || ocrLoading}
  149. />
  150. {ocrLoading && (
  151. <div className="flex items-center gap-2 mt-2 p-2 bg-blue-50/50 rounded-md border border-blue-100/50">
  152. <Loader size={12} className="animate-spin" />
  153. {t('analyzingImage')}
  154. </div>
  155. )}
  156. {!ocrLoading && !content && (
  157. <p className="text-sm text-gray-500">
  158. {t('noTextExtracted')}
  159. </p>
  160. )}
  161. </div>
  162. </div>
  163. {/* Footer */}
  164. <div className="flex items-center justify-end gap-2 p-4 border-t bg-gray-50">
  165. <button
  166. onClick={onCancel}
  167. className="px-4 py-2 text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition-colors"
  168. disabled={saving}
  169. >
  170. {t('cancel')}
  171. </button>
  172. <button
  173. onClick={handleSave}
  174. className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
  175. disabled={saving || !title.trim()}
  176. >
  177. {saving && <Loader size={16} className="animate-spin" />}
  178. {saving ? t('saving') : t('saveNote')}
  179. </button>
  180. </div>
  181. </div>
  182. </div>
  183. );
  184. };