EditNotebookDrawer.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { useEffect, useState } from 'react'
  2. import { Plus, ChevronRight, Save } from 'lucide-react'
  3. import { KnowledgeGroup, UpdateGroupData } from '../types'
  4. import { useLanguage } from '../contexts/LanguageContext'
  5. import { useToast } from '../contexts/ToastContext'
  6. interface EditNotebookDrawerProps {
  7. isOpen: boolean
  8. onClose: () => void
  9. notebook: KnowledgeGroup
  10. onUpdate: (id: string, data: UpdateGroupData) => Promise<void>
  11. }
  12. export const EditNotebookDrawer: React.FC<EditNotebookDrawerProps> = ({
  13. isOpen,
  14. onClose,
  15. notebook,
  16. onUpdate,
  17. }) => {
  18. const { t } = useLanguage()
  19. const { showError } = useToast()
  20. const [name, setName] = useState(notebook.name)
  21. const [description, setDescription] = useState(notebook.description || '')
  22. const [isSubmitting, setIsSubmitting] = useState(false)
  23. // Reset form when notebook changes or drawer opens
  24. useEffect(() => {
  25. if (isOpen) {
  26. setName(notebook.name)
  27. setDescription(notebook.description || '')
  28. }
  29. }, [isOpen, notebook])
  30. const handleSubmit = async (e: React.FormEvent) => {
  31. e.preventDefault()
  32. if (!name.trim()) return
  33. try {
  34. setIsSubmitting(true)
  35. await onUpdate(notebook.id, {
  36. name: name.trim(),
  37. description: description.trim(),
  38. })
  39. onClose()
  40. } catch (error) {
  41. console.error('Failed to update notebook:', error)
  42. showError(t('updateFailedRetry'))
  43. } finally {
  44. setIsSubmitting(false)
  45. }
  46. }
  47. return (
  48. <>
  49. {/* Backdrop */}
  50. {isOpen && (
  51. <div
  52. className="fixed inset-0 bg-black/20 backdrop-blur-sm z-40 transition-opacity duration-300"
  53. onClick={onClose}
  54. />
  55. )}
  56. {/* Drawer */}
  57. <div
  58. className={`fixed right-0 top-0 h-full w-full max-w-md bg-white shadow-2xl z-50 transform transition-transform duration-300 ease-out ${isOpen ? 'translate-x-0' : 'translate-x-full'
  59. }`}
  60. >
  61. <div className="flex flex-col h-full">
  62. {/* Header */}
  63. <div className="flex items-center justify-between px-6 py-4 border-b bg-slate-50">
  64. <h2 className="text-xl font-semibold text-slate-800 flex items-center gap-2">
  65. <Save className="w-5 h-5 text-blue-600" />
  66. {t('editNotebookTitle')}
  67. </h2>
  68. <button
  69. onClick={onClose}
  70. className="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-200 rounded-full transition-colors"
  71. >
  72. <ChevronRight size={24} />
  73. </button>
  74. </div>
  75. {/* Content */}
  76. <div className="flex-1 overflow-y-auto p-6">
  77. <form id="edit-notebook-form" onSubmit={handleSubmit} className="space-y-6">
  78. <div>
  79. <label className="block text-sm font-medium text-slate-700 mb-1">
  80. {t('name')} <span className="text-red-500">*</span>
  81. </label>
  82. <input
  83. type="text"
  84. value={name}
  85. onChange={(e) => setName(e.target.value)}
  86. className="w-full px-4 py-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 bg-slate-50"
  87. placeholder={t('namePlaceholder')}
  88. required
  89. autoFocus
  90. />
  91. </div>
  92. <div>
  93. <label className="block text-sm font-medium text-slate-700 mb-1">
  94. {t('shortDescription')}
  95. </label>
  96. <input
  97. type="text"
  98. value={description}
  99. onChange={(e) => setDescription(e.target.value)}
  100. className="w-full px-4 py-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 bg-slate-50"
  101. placeholder={t('descPlaceholder')}
  102. />
  103. </div>
  104. </form>
  105. </div>
  106. {/* Footer */}
  107. <div className="p-6 border-t bg-slate-50">
  108. <button
  109. type="submit"
  110. form="edit-notebook-form"
  111. disabled={isSubmitting || !name.trim()}
  112. className="w-full flex items-center justify-center gap-2 px-6 py-3 bg-blue-600 text-white font-medium rounded-xl hover:bg-blue-700 active:scale-[0.98] transition-all disabled:opacity-50 disabled:cursor-not-allowed shadow-lg shadow-blue-600/20"
  113. >
  114. <Save size={20} />
  115. {isSubmitting ? t('saving') : t('save')}
  116. </button>
  117. </div>
  118. </div>
  119. </div>
  120. </>
  121. )
  122. }