ProjectPageModal.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import { useState, useEffect } from 'react';
  2. import DOMPurify from 'dompurify';
  3. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import {
  5. X,
  6. User,
  7. Calendar,
  8. FileText,
  9. Image,
  10. Edit3,
  11. Save,
  12. ExternalLink,
  13. ChevronLeft,
  14. ChevronRight,
  15. } from 'lucide-react';
  16. import { api } from '../api/client';
  17. import { Button } from './Button';
  18. import { RichTextEditor } from './RichTextEditor';
  19. interface ProjectPageModalProps {
  20. archiveId: number;
  21. archiveName?: string;
  22. onClose: () => void;
  23. }
  24. export function ProjectPageModal({ archiveId, archiveName, onClose }: ProjectPageModalProps) {
  25. const queryClient = useQueryClient();
  26. const [isEditing, setIsEditing] = useState(false);
  27. const [selectedImageIndex, setSelectedImageIndex] = useState<number | null>(null);
  28. const [editData, setEditData] = useState<{
  29. title?: string;
  30. description?: string;
  31. designer?: string;
  32. license?: string;
  33. profile_title?: string;
  34. profile_description?: string;
  35. }>({});
  36. const { data: projectPage, isLoading, error } = useQuery({
  37. queryKey: ['archive-project-page', archiveId],
  38. queryFn: () => api.getArchiveProjectPage(archiveId),
  39. });
  40. const updateMutation = useMutation({
  41. mutationFn: (data: typeof editData) => api.updateArchiveProjectPage(archiveId, data),
  42. onSuccess: () => {
  43. queryClient.invalidateQueries({ queryKey: ['archive-project-page', archiveId] });
  44. setIsEditing(false);
  45. setEditData({});
  46. },
  47. });
  48. // Handle escape key to close modal
  49. useEffect(() => {
  50. const handleKeyDown = (e: KeyboardEvent) => {
  51. if (e.key === 'Escape') {
  52. if (selectedImageIndex !== null) {
  53. setSelectedImageIndex(null);
  54. } else if (isEditing) {
  55. handleCancelEdit();
  56. } else {
  57. onClose();
  58. }
  59. }
  60. };
  61. document.addEventListener('keydown', handleKeyDown);
  62. return () => document.removeEventListener('keydown', handleKeyDown);
  63. }, [selectedImageIndex, isEditing, onClose]);
  64. // Combine all images for gallery
  65. const allImages = [
  66. ...(projectPage?.model_pictures || []),
  67. ...(projectPage?.profile_pictures || []),
  68. ];
  69. const handleStartEdit = () => {
  70. setEditData({
  71. title: projectPage?.title || '',
  72. description: projectPage?.description || '',
  73. designer: projectPage?.designer || '',
  74. license: projectPage?.license || '',
  75. profile_title: projectPage?.profile_title || '',
  76. profile_description: projectPage?.profile_description || '',
  77. });
  78. setIsEditing(true);
  79. };
  80. const handleSave = () => {
  81. updateMutation.mutate(editData);
  82. };
  83. const handleCancelEdit = () => {
  84. setIsEditing(false);
  85. setEditData({});
  86. };
  87. // Sanitize HTML content using DOMPurify
  88. const sanitizeHtml = (html: string) => {
  89. return DOMPurify.sanitize(html, {
  90. ALLOWED_TAGS: ['p', 'br', 'b', 'strong', 'i', 'em', 'u', 'a', 'ul', 'ol', 'li', 'figure', 'img'],
  91. ALLOWED_ATTR: ['href', 'src', 'target', 'rel', 'style'],
  92. ADD_ATTR: ['target'],
  93. });
  94. };
  95. const hasContent = projectPage && (
  96. projectPage.title ||
  97. projectPage.description ||
  98. projectPage.designer ||
  99. projectPage.profile_title ||
  100. allImages.length > 0
  101. );
  102. // Handle backdrop click to close modal
  103. const handleBackdropClick = (e: React.MouseEvent) => {
  104. if (e.target === e.currentTarget) {
  105. onClose();
  106. }
  107. };
  108. return (
  109. <div
  110. className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
  111. onClick={handleBackdropClick}
  112. >
  113. <div className="bg-bambu-dark-secondary rounded-xl max-w-4xl w-full max-h-[90vh] overflow-hidden flex flex-col">
  114. {/* Header */}
  115. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  116. <div className="flex items-center gap-3">
  117. <FileText className="w-5 h-5 text-bambu-green" />
  118. <h2 className="text-lg font-semibold text-white">
  119. Project Page
  120. {archiveName && <span className="text-bambu-gray ml-2">- {archiveName}</span>}
  121. </h2>
  122. </div>
  123. <div className="flex items-center gap-2">
  124. {!isEditing && hasContent && (
  125. <Button variant="ghost" size="sm" onClick={handleStartEdit}>
  126. <Edit3 className="w-4 h-4 mr-1" />
  127. Edit
  128. </Button>
  129. )}
  130. {isEditing && (
  131. <>
  132. <Button variant="ghost" size="sm" onClick={handleCancelEdit}>
  133. Cancel
  134. </Button>
  135. <Button
  136. variant="primary"
  137. size="sm"
  138. onClick={handleSave}
  139. disabled={updateMutation.isPending}
  140. >
  141. <Save className="w-4 h-4 mr-1" />
  142. Save
  143. </Button>
  144. </>
  145. )}
  146. <button
  147. onClick={onClose}
  148. className="p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors"
  149. >
  150. <X className="w-5 h-5 text-bambu-gray" />
  151. </button>
  152. </div>
  153. </div>
  154. {/* Content */}
  155. <div className="flex-1 overflow-y-auto p-6">
  156. {isLoading && (
  157. <div className="flex items-center justify-center py-12">
  158. <div className="animate-spin rounded-full h-8 w-8 border-2 border-bambu-green border-t-transparent" />
  159. </div>
  160. )}
  161. {error && (
  162. <div className="text-red-400 text-center py-12">
  163. Failed to load project page data
  164. </div>
  165. )}
  166. {projectPage && !hasContent && (
  167. <div className="text-bambu-gray text-center py-12">
  168. <FileText className="w-12 h-12 mx-auto mb-4 opacity-50" />
  169. <p>No project page data found in this 3MF file.</p>
  170. <p className="text-sm mt-2">
  171. Project pages are typically included in files downloaded from MakerWorld.
  172. </p>
  173. </div>
  174. )}
  175. {projectPage && hasContent && (
  176. <div className="space-y-6">
  177. {/* Title & Designer */}
  178. <div className="space-y-4">
  179. {isEditing ? (
  180. <input
  181. type="text"
  182. value={editData.title || ''}
  183. onChange={(e) => setEditData({ ...editData, title: e.target.value })}
  184. placeholder="Title"
  185. className="w-full bg-bambu-dark border border-bambu-dark-tertiary rounded-lg px-4 py-2 text-white text-xl font-semibold"
  186. />
  187. ) : (
  188. projectPage.title && (
  189. <h3 className="text-xl font-semibold text-white">{projectPage.title}</h3>
  190. )
  191. )}
  192. <div className="flex flex-wrap gap-4 text-sm">
  193. {isEditing ? (
  194. <div className="flex items-center gap-2">
  195. <User className="w-4 h-4 text-bambu-gray" />
  196. <input
  197. type="text"
  198. value={editData.designer || ''}
  199. onChange={(e) => setEditData({ ...editData, designer: e.target.value })}
  200. placeholder="Designer"
  201. className="bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1 text-white"
  202. />
  203. </div>
  204. ) : (
  205. projectPage.designer && (
  206. <div className="flex items-center gap-2 text-bambu-gray">
  207. <User className="w-4 h-4" />
  208. <span>{projectPage.designer}</span>
  209. {projectPage.designer_user_id && (
  210. <a
  211. href={`https://makerworld.com/en/@${projectPage.designer_user_id}`}
  212. target="_blank"
  213. rel="noopener noreferrer"
  214. className="text-bambu-green hover:underline"
  215. >
  216. <ExternalLink className="w-3 h-3" />
  217. </a>
  218. )}
  219. </div>
  220. )
  221. )}
  222. {projectPage.creation_date && (
  223. <div className="flex items-center gap-2 text-bambu-gray">
  224. <Calendar className="w-4 h-4" />
  225. <span>{projectPage.creation_date}</span>
  226. </div>
  227. )}
  228. {isEditing ? (
  229. <div className="flex items-center gap-2">
  230. <FileText className="w-4 h-4 text-bambu-gray" />
  231. <input
  232. type="text"
  233. value={editData.license || ''}
  234. onChange={(e) => setEditData({ ...editData, license: e.target.value })}
  235. placeholder="License"
  236. className="bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1 text-white"
  237. />
  238. </div>
  239. ) : (
  240. projectPage.license && (
  241. <div className="flex items-center gap-2 text-bambu-gray">
  242. <FileText className="w-4 h-4" />
  243. <span>{projectPage.license}</span>
  244. </div>
  245. )
  246. )}
  247. {projectPage.origin && (
  248. <span className="px-2 py-0.5 bg-bambu-dark rounded text-bambu-gray">
  249. {projectPage.origin}
  250. </span>
  251. )}
  252. </div>
  253. </div>
  254. {/* Description */}
  255. {(projectPage.description || isEditing) && (
  256. <div className="space-y-2">
  257. <h4 className="text-sm font-medium text-bambu-gray uppercase tracking-wide">
  258. Description
  259. </h4>
  260. {isEditing ? (
  261. <RichTextEditor
  262. content={editData.description || ''}
  263. onChange={(html) => setEditData({ ...editData, description: html })}
  264. placeholder="Enter description..."
  265. />
  266. ) : (
  267. <div
  268. className="prose prose-invert prose-sm max-w-none text-bambu-gray-light"
  269. dangerouslySetInnerHTML={{
  270. __html: sanitizeHtml(projectPage.description || ''),
  271. }}
  272. />
  273. )}
  274. </div>
  275. )}
  276. {/* Profile Info */}
  277. {(projectPage.profile_title || projectPage.profile_description || isEditing) && (
  278. <div className="space-y-2 p-4 bg-bambu-dark rounded-lg">
  279. <h4 className="text-sm font-medium text-bambu-gray uppercase tracking-wide">
  280. Print Profile
  281. </h4>
  282. {isEditing ? (
  283. <div className="space-y-2">
  284. <input
  285. type="text"
  286. value={editData.profile_title || ''}
  287. onChange={(e) => setEditData({ ...editData, profile_title: e.target.value })}
  288. placeholder="Profile Title"
  289. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-white"
  290. />
  291. <RichTextEditor
  292. content={editData.profile_description || ''}
  293. onChange={(html) => setEditData({ ...editData, profile_description: html })}
  294. placeholder="Profile description..."
  295. />
  296. </div>
  297. ) : (
  298. <>
  299. {projectPage.profile_title && (
  300. <p className="text-white font-medium">{projectPage.profile_title}</p>
  301. )}
  302. {projectPage.profile_description && (
  303. <div
  304. className="prose prose-invert prose-sm max-w-none text-bambu-gray-light"
  305. dangerouslySetInnerHTML={{
  306. __html: sanitizeHtml(projectPage.profile_description),
  307. }}
  308. />
  309. )}
  310. {projectPage.profile_user_name && (
  311. <p className="text-sm text-bambu-gray">
  312. by {projectPage.profile_user_name}
  313. </p>
  314. )}
  315. </>
  316. )}
  317. </div>
  318. )}
  319. {/* Image Gallery */}
  320. {allImages.length > 0 && (
  321. <div className="space-y-2">
  322. <h4 className="text-sm font-medium text-bambu-gray uppercase tracking-wide flex items-center gap-2">
  323. <Image className="w-4 h-4" />
  324. Images ({allImages.length})
  325. </h4>
  326. <div className="grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 gap-2">
  327. {allImages.map((img, index) => (
  328. <button
  329. key={img.path}
  330. onClick={() => setSelectedImageIndex(index)}
  331. className="aspect-square rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors"
  332. >
  333. <img
  334. src={img.url}
  335. alt={img.name}
  336. className="w-full h-full object-cover"
  337. />
  338. </button>
  339. ))}
  340. </div>
  341. </div>
  342. )}
  343. {/* MakerWorld Link */}
  344. {projectPage.design_model_id && (
  345. <div className="pt-4 border-t border-bambu-dark-tertiary">
  346. <a
  347. href={`https://makerworld.com/en/models/${projectPage.design_model_id}`}
  348. target="_blank"
  349. rel="noopener noreferrer"
  350. className="inline-flex items-center gap-2 text-bambu-green hover:underline"
  351. >
  352. <ExternalLink className="w-4 h-4" />
  353. View on MakerWorld
  354. </a>
  355. </div>
  356. )}
  357. </div>
  358. )}
  359. </div>
  360. </div>
  361. {/* Image Lightbox */}
  362. {selectedImageIndex !== null && allImages[selectedImageIndex] && (
  363. <div
  364. className="fixed inset-0 bg-black/90 flex items-center justify-center z-60"
  365. onClick={() => setSelectedImageIndex(null)}
  366. >
  367. <button
  368. onClick={(e) => {
  369. e.stopPropagation();
  370. setSelectedImageIndex(Math.max(0, selectedImageIndex - 1));
  371. }}
  372. disabled={selectedImageIndex === 0}
  373. className="absolute left-4 p-2 bg-bambu-dark-secondary rounded-full hover:bg-bambu-dark-tertiary disabled:opacity-30"
  374. >
  375. <ChevronLeft className="w-6 h-6 text-white" />
  376. </button>
  377. <img
  378. src={allImages[selectedImageIndex].url}
  379. alt={allImages[selectedImageIndex].name}
  380. className="max-w-[90vw] max-h-[90vh] object-contain"
  381. onClick={(e) => e.stopPropagation()}
  382. />
  383. <button
  384. onClick={(e) => {
  385. e.stopPropagation();
  386. setSelectedImageIndex(Math.min(allImages.length - 1, selectedImageIndex + 1));
  387. }}
  388. disabled={selectedImageIndex === allImages.length - 1}
  389. className="absolute right-4 p-2 bg-bambu-dark-secondary rounded-full hover:bg-bambu-dark-tertiary disabled:opacity-30"
  390. >
  391. <ChevronRight className="w-6 h-6 text-white" />
  392. </button>
  393. <button
  394. onClick={() => setSelectedImageIndex(null)}
  395. className="absolute top-4 right-4 p-2 bg-bambu-dark-secondary rounded-full hover:bg-bambu-dark-tertiary"
  396. >
  397. <X className="w-6 h-6 text-white" />
  398. </button>
  399. <div className="absolute bottom-4 text-white text-sm">
  400. {selectedImageIndex + 1} / {allImages.length}
  401. </div>
  402. </div>
  403. )}
  404. </div>
  405. );
  406. }