You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

884 lines
46 KiB

  1. 'use client';
  2. import { useState, useEffect, useRef } from 'react';
  3. import { Card, Portal, MediaItem, CardType } from '@/types';
  4. import { EXTERNAL_LINK_ENABLED as EXTERNAL_LINK_DEFAULT } from '@/lib/config';
  5. function StyledSelect<T extends string>({
  6. value,
  7. onChange,
  8. options,
  9. }: {
  10. value: T;
  11. onChange: (v: T) => void;
  12. options: { value: T; label: string }[];
  13. }) {
  14. const [open, setOpen] = useState(false);
  15. const ref = useRef<HTMLDivElement>(null);
  16. useEffect(() => {
  17. if (!open) return;
  18. const onClick = (e: MouseEvent) => {
  19. if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
  20. };
  21. const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); };
  22. document.addEventListener('mousedown', onClick);
  23. document.addEventListener('keydown', onKey);
  24. return () => {
  25. document.removeEventListener('mousedown', onClick);
  26. document.removeEventListener('keydown', onKey);
  27. };
  28. }, [open]);
  29. const current = options.find(o => o.value === value);
  30. // Fallback: se il value non matcha nessuna opzione (es. tipo disattivato dalla flag), mostra il valore raw prettificato
  31. const displayLabel = current?.label
  32. ?? (typeof value === 'string' && value.length > 0
  33. ? value.replace(/_/g, ' ').toLowerCase().replace(/\b\w/g, c => c.toUpperCase())
  34. : '');
  35. const inputBase = "w-full border border-gray-300 p-2.5 rounded-lg outline-none focus:ring-2 focus:ring-blue-500 bg-white text-gray-900";
  36. return (
  37. <div ref={ref} className="relative">
  38. <button
  39. type="button"
  40. onClick={() => setOpen(o => !o)}
  41. className={`${inputBase} text-left flex items-center justify-between cursor-pointer`}
  42. >
  43. <span className={displayLabel ? '' : 'text-gray-400'}>{displayLabel || 'Seleziona…'}</span>
  44. <span className={`text-gray-500 transition-transform ${open ? 'rotate-180' : ''}`}>▾</span>
  45. </button>
  46. {open && (
  47. <div className="absolute left-0 right-0 top-full mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-30 overflow-hidden">
  48. {options.map(o => (
  49. <button
  50. key={o.value}
  51. type="button"
  52. onClick={() => { onChange(o.value); setOpen(false); }}
  53. className={`w-full text-left px-3 py-2.5 hover:bg-blue-50 transition-colors ${o.value === value ? 'bg-blue-100 font-semibold text-blue-700' : 'text-gray-800'}`}
  54. >
  55. {o.label}
  56. </button>
  57. ))}
  58. </div>
  59. )}
  60. </div>
  61. );
  62. }
  63. const VIDEO_EXTENSIONS = 'mp4|m4v|webm|mov|qt|mkv|avi|divx|wmv|asf|flv|f4v|3gp|3gpp|3g2|mts|m2ts|ts|mpg|mpeg|vob|mxf|ogv|ogg';
  64. // Sottoinsieme di formati video davvero riproducibili dai browser moderni
  65. const PLAYBACK_SUPPORTED_VIDEO = 'mp4|m4v|webm|mov|qt|ogv|ogg';
  66. const PLAYBACK_SUPPORTED_LABEL = 'MP4, M4V, WebM, MOV, OGV';
  67. const isVideoUrl = (url: string) => new RegExp(`\\.(${VIDEO_EXTENSIONS})(\\?|$)`, 'i').test(url);
  68. const isPdfFile = (file: File) =>
  69. file.type === 'application/pdf' || /\.pdf$/i.test(file.name);
  70. const isVideoFile = (file: File) =>
  71. file.type.startsWith('video/') || new RegExp(`\\.(${VIDEO_EXTENSIONS})$`, 'i').test(file.name);
  72. const isPlayableVideoFile = (file: File) =>
  73. new RegExp(`\\.(${PLAYBACK_SUPPORTED_VIDEO})$`, 'i').test(file.name);
  74. const extractFileName = (url: string): string => {
  75. const match = url.match(/[?&]name=([^&]+)/);
  76. if (match) return decodeURIComponent(match[1]);
  77. const seg = url.split('/').pop() || 'download';
  78. return seg.split('?')[0];
  79. };
  80. async function uploadBlobAsImage(blob: Blob, name: string): Promise<string | null> {
  81. const formData = new FormData();
  82. formData.append('file', new File([blob], name, { type: blob.type || 'image/png' }));
  83. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  84. const data = await res.json();
  85. return data.url || null;
  86. }
  87. async function extractVideoFrame(file: File): Promise<Blob | null> {
  88. const url = URL.createObjectURL(file);
  89. try {
  90. const video = document.createElement('video');
  91. video.muted = true;
  92. video.playsInline = true;
  93. video.preload = 'metadata';
  94. video.src = url;
  95. await new Promise<void>((resolve, reject) => {
  96. video.addEventListener('loadedmetadata', () => resolve(), { once: true });
  97. video.addEventListener('error', () => reject(new Error('video load error')), { once: true });
  98. });
  99. // Seek slightly past 0 — at exactly 0 some codecs return a black frame
  100. video.currentTime = Math.min(0.1, Math.max(0, video.duration / 10));
  101. await new Promise<void>((resolve, reject) => {
  102. video.addEventListener('seeked', () => resolve(), { once: true });
  103. video.addEventListener('error', () => reject(new Error('video seek error')), { once: true });
  104. });
  105. const canvas = document.createElement('canvas');
  106. canvas.width = video.videoWidth;
  107. canvas.height = video.videoHeight;
  108. const ctx = canvas.getContext('2d');
  109. if (!ctx) return null;
  110. ctx.drawImage(video, 0, 0);
  111. return await new Promise<Blob | null>((resolve) =>
  112. canvas.toBlob((b) => resolve(b), 'image/jpeg', 0.85)
  113. );
  114. } finally {
  115. URL.revokeObjectURL(url);
  116. }
  117. }
  118. async function pdfToImageItems(
  119. file: File,
  120. onProgress: (page: number, total: number) => void
  121. ): Promise<MediaItem[]> {
  122. const pdfjs = await import('pdfjs-dist');
  123. // Worker file is copied to /public via the postinstall script
  124. pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.mjs';
  125. const arrayBuffer = await file.arrayBuffer();
  126. const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
  127. const baseName = file.name.replace(/\.pdf$/i, '').replace(/[^a-zA-Z0-9-_]/g, '_');
  128. const items: MediaItem[] = [];
  129. for (let i = 1; i <= pdf.numPages; i++) {
  130. onProgress(i, pdf.numPages);
  131. const page = await pdf.getPage(i);
  132. const viewport = page.getViewport({ scale: 1.5 });
  133. const canvas = document.createElement('canvas');
  134. canvas.width = viewport.width;
  135. canvas.height = viewport.height;
  136. const ctx = canvas.getContext('2d');
  137. if (!ctx) continue;
  138. await page.render({ canvasContext: ctx, viewport }).promise;
  139. const blob: Blob = await new Promise((resolve, reject) => {
  140. canvas.toBlob(b => b ? resolve(b) : reject(new Error('toBlob failed')), 'image/png');
  141. });
  142. const url = await uploadBlobAsImage(blob, `${baseName}-page${i}.png`);
  143. if (url) items.push({ url });
  144. }
  145. return items;
  146. }
  147. export default function AdminDashboard() {
  148. const [activeTab, setActiveTab] = useState<'cards' | 'settings'>('cards');
  149. // Card State
  150. const [cards, setCards] = useState<Card[]>([]);
  151. const [isEditing, setIsEditing] = useState<Partial<Card> | null>(null);
  152. // Portal State
  153. const [portal, setPortal] = useState<Partial<Portal>>({});
  154. const [savingPortal, setSavingPortal] = useState(false);
  155. const [uploading, setUploading] = useState<{ [key: string]: boolean }>({});
  156. // NEW UI STATES: Toast and Confirm Dialog
  157. const [toast, setToast] = useState<{ message: string; type: 'success' | 'error' } | null>(null);
  158. const [confirmDialog, setConfirmDialog] = useState<{ message: string, onConfirm: () => void } | null>(null);
  159. const [pdfProgress, setPdfProgress] = useState<{ name: string; page: number; total: number } | null>(null);
  160. const [availableFonts, setAvailableFonts] = useState<string[]>([]);
  161. // External Link feature flag: priorità al setting del portale, fallback alla costante in lib/config.
  162. const externalLinksOn = portal.externalLinkEnabled ?? EXTERNAL_LINK_DEFAULT;
  163. // Helper to show auto-dismissing toast
  164. const showToast = (message: string, type: 'success' | 'error' = 'success') => {
  165. setToast({ message, type });
  166. setTimeout(() => setToast(null), type === 'error' ? 6000 : 3000);
  167. };
  168. useEffect(() => {
  169. fetch('/api/cards').then(res => res.json()).then(setCards);
  170. fetch('/api/portals').then(res => res.json()).then(data => data && setPortal(data));
  171. fetch('/api/fonts').then(res => res.json()).then(setAvailableFonts).catch(() => setAvailableFonts([]));
  172. }, []);
  173. const handleUpload = async (e: React.ChangeEvent<HTMLInputElement>, field: string, isPortal = false) => {
  174. if (!e.target.files?.[0]) return;
  175. setUploading(prev => ({ ...prev, [field]: true }));
  176. const formData = new FormData();
  177. formData.append('file', e.target.files[0]);
  178. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  179. const data = await res.json();
  180. if (data.url) {
  181. if (isPortal) {
  182. setPortal(prev => ({ ...prev, [field]: data.url }));
  183. } else {
  184. setIsEditing(prev => ({ ...prev, [field]: data.url }));
  185. }
  186. }
  187. setUploading(prev => ({ ...prev, [field]: false }));
  188. };
  189. const handleUploadExtraMedia = async (e: React.ChangeEvent<HTMLInputElement>) => {
  190. const files = e.target.files;
  191. if (!files || files.length === 0) return;
  192. setUploading(prev => ({ ...prev, extraMedia: true }));
  193. const startedWithoutCover = !isEditing?.imageUrl;
  194. let pendingCover: string | null = null;
  195. const canPromote = () => startedWithoutCover && !pendingCover;
  196. // Pre-filtro: scarta video con formati non riproducibili nei browser
  197. const rejected: string[] = [];
  198. const acceptedFiles: File[] = [];
  199. for (const file of Array.from(files)) {
  200. if (isVideoFile(file) && !isPlayableVideoFile(file)) {
  201. rejected.push(file.name);
  202. } else {
  203. acceptedFiles.push(file);
  204. }
  205. }
  206. if (rejected.length > 0) {
  207. const list = rejected.length <= 3
  208. ? rejected.join(', ')
  209. : `${rejected.slice(0, 3).join(', ')} e altri ${rejected.length - 3}`;
  210. showToast(
  211. `Formato non supportato! I formati supportati sono: ${PLAYBACK_SUPPORTED_LABEL}. File ignorati: ${list}`,
  212. 'error'
  213. );
  214. }
  215. if (acceptedFiles.length === 0) {
  216. setUploading(prev => ({ ...prev, extraMedia: false }));
  217. e.target.value = '';
  218. return;
  219. }
  220. const uploaded: MediaItem[] = [];
  221. for (const file of acceptedFiles) {
  222. try {
  223. if (isPdfFile(file)) {
  224. const items = await pdfToImageItems(file, (page, total) =>
  225. setPdfProgress({ name: file.name, page, total })
  226. );
  227. setPdfProgress(null);
  228. if (items.length > 0 && canPromote()) {
  229. // Promote the first PDF page to cover; skip it from the gallery to avoid duplication.
  230. pendingCover = items[0].url;
  231. uploaded.push(...items.slice(1));
  232. } else {
  233. uploaded.push(...items);
  234. }
  235. } else {
  236. const formData = new FormData();
  237. formData.append('file', file);
  238. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  239. const data = await res.json();
  240. if (!data.url) continue;
  241. if (isVideoFile(file)) {
  242. // Video always goes to the gallery so users can play it.
  243. uploaded.push({ url: data.url });
  244. // If no cover yet, extract the first frame and use it as the cover.
  245. if (canPromote()) {
  246. try {
  247. const blob = await extractVideoFrame(file);
  248. if (blob) {
  249. const baseName = file.name.replace(/\.[^.]+$/, '').replace(/[^a-zA-Z0-9-_]/g, '_');
  250. const posterUrl = await uploadBlobAsImage(blob, `${baseName}-poster.jpg`);
  251. if (posterUrl) pendingCover = posterUrl;
  252. }
  253. } catch (err) {
  254. console.warn('Could not extract video poster for', file.name, err);
  255. }
  256. }
  257. } else {
  258. // Plain image
  259. if (canPromote()) {
  260. // Promote to cover; skip the gallery to avoid duplication.
  261. pendingCover = data.url;
  262. } else {
  263. uploaded.push({ url: data.url });
  264. }
  265. }
  266. }
  267. } catch (err) {
  268. console.error('Upload failed for', file.name, err);
  269. showToast(`Failed to process "${file.name}".`);
  270. setPdfProgress(null);
  271. }
  272. }
  273. setIsEditing(prev => ({
  274. ...prev,
  275. imageUrl: (startedWithoutCover && pendingCover) ? pendingCover : (prev?.imageUrl || ''),
  276. extraMedia: [...(prev?.extraMedia || []), ...uploaded],
  277. }));
  278. setUploading(prev => ({ ...prev, extraMedia: false }));
  279. e.target.value = '';
  280. };
  281. const removeExtraMedia = (index: number) => {
  282. setIsEditing(prev => ({
  283. ...prev,
  284. extraMedia: (prev?.extraMedia || []).filter((_, i) => i !== index),
  285. }));
  286. };
  287. const toggleAutoplay = (index: number) => {
  288. setIsEditing(prev => ({
  289. ...prev,
  290. extraMedia: (prev?.extraMedia || []).map((m, i) =>
  291. i === index ? { ...m, autoplay: !m.autoplay } : m
  292. ),
  293. }));
  294. };
  295. const toggleMuted = (index: number) => {
  296. setIsEditing(prev => ({
  297. ...prev,
  298. extraMedia: (prev?.extraMedia || []).map((m, i) =>
  299. i === index ? { ...m, muted: !m.muted } : m
  300. ),
  301. }));
  302. };
  303. const handleSaveCard = async () => {
  304. if (!isEditing) return;
  305. const generateSafeId = () => 'card-' + Date.now().toString(36) + Math.random().toString(36).substring(2);
  306. const newCard = { ...isEditing, id: isEditing.id || generateSafeId() } as Card;
  307. await fetch('/api/cards', {
  308. method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(newCard)
  309. });
  310. setCards(prev => {
  311. const exists = prev.find(c => c.id === newCard.id);
  312. return exists ? prev.map(c => c.id === newCard.id ? newCard : c) : [...prev, newCard];
  313. });
  314. setIsEditing(null);
  315. };
  316. const handleDeleteCard = (id: string) => {
  317. // Replace window.confirm with our custom dialog
  318. setConfirmDialog({
  319. message: 'Are you sure you want to delete this card? This action cannot be undone.',
  320. onConfirm: async () => {
  321. await fetch(`/api/cards?id=${id}`, { method: 'DELETE' });
  322. setCards(prev => prev.filter(c => c.id !== id));
  323. setConfirmDialog(null);
  324. showToast('Card successfully deleted.');
  325. }
  326. });
  327. };
  328. const moveCard = async (index: number, direction: 'up' | 'down') => {
  329. const newCards = [...cards];
  330. if (direction === 'up' && index > 0) {
  331. [newCards[index - 1], newCards[index]] = [newCards[index], newCards[index - 1]];
  332. } else if (direction === 'down' && index < newCards.length - 1) {
  333. [newCards[index + 1], newCards[index]] = [newCards[index], newCards[index + 1]];
  334. } else {
  335. return; // Do nothing if trying to move out of bounds
  336. }
  337. // Recalculate displayOrder for the whole array
  338. const updatedCards = newCards.map((c, i) => ({ ...c, displayOrder: i }));
  339. // Optimistically update the UI
  340. setCards(updatedCards);
  341. // Persist the new order to the backend
  342. await fetch('/api/cards', {
  343. method: 'PUT',
  344. headers: { 'Content-Type': 'application/json' },
  345. body: JSON.stringify(updatedCards)
  346. });
  347. };
  348. const handleSavePortal = async () => {
  349. setSavingPortal(true);
  350. await fetch('/api/portals', {
  351. method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(portal)
  352. });
  353. setSavingPortal(false);
  354. showToast('Portal settings saved successfully!'); // Replaced window.alert
  355. };
  356. // Shared Input Classes for high contrast
  357. const inputClasses = "w-full border border-gray-300 p-2.5 rounded-lg outline-none focus:ring-2 focus:ring-blue-500 bg-white text-gray-900 placeholder-gray-400";
  358. return (
  359. <div className="min-h-screen bg-gray-50 font-sans pb-12">
  360. {/* Top Header */}
  361. <div className="bg-blue-900 text-white shadow-md py-6 px-4">
  362. <div className="max-w-5xl mx-auto flex justify-between items-center">
  363. <div>
  364. <h1 className="text-2xl font-bold">Captive Portal CMS</h1>
  365. <p className="text-sm text-blue-200">Local Administration</p>
  366. </div>
  367. <a href="/" target="_blank" className="bg-blue-800 hover:bg-blue-700 text-white px-4 py-2 rounded text-sm transition-colors">
  368. View Live Portal ↗
  369. </a>
  370. </div>
  371. </div>
  372. <div className="max-w-5xl mx-auto mt-8 px-4">
  373. {/* Tab Navigation */}
  374. <div className="flex space-x-2 mb-6">
  375. <button onClick={() => setActiveTab('cards')} className={`px-6 py-3 rounded-t-lg font-bold transition-colors ${activeTab === 'cards' ? 'bg-white text-blue-700 border-t-4 border-blue-600 shadow-sm' : 'bg-gray-200 text-gray-600 hover:bg-gray-300'}`}>
  376. Manage Cards
  377. </button>
  378. <button onClick={() => setActiveTab('settings')} className={`px-6 py-3 rounded-t-lg font-bold transition-colors ${activeTab === 'settings' ? 'bg-white text-blue-700 border-t-4 border-blue-600 shadow-sm' : 'bg-gray-200 text-gray-600 hover:bg-gray-300'}`}>
  379. Portal Settings
  380. </button>
  381. </div>
  382. <div className="bg-white rounded-b-xl rounded-tr-xl shadow-sm border border-gray-200 overflow-hidden min-h-[500px]">
  383. {/* TAB: CARDS */}
  384. {activeTab === 'cards' && (
  385. <div className="p-6 md:p-8">
  386. <div className="flex justify-between items-center mb-8 border-b pb-4">
  387. <h2 className="text-xl font-bold text-gray-800">Card Grid</h2>
  388. <button onClick={() => setIsEditing({ title: '', cardType: 'INFO_PAGE', displayOrder: cards.length })} className="bg-blue-600 text-white px-5 py-2.5 rounded-lg shadow-sm hover:bg-blue-700 font-medium">
  389. + Add New Card
  390. </button>
  391. </div>
  392. <div className="space-y-3 mb-8">
  393. {cards.length === 0 && <p className="text-gray-500 italic text-center py-8">No cards available. Create one to get started.</p>}
  394. {cards.map((card, idx) => (
  395. // CHANGED: flex-col on mobile, flex-row on sm+, added gap-4 for mobile spacing
  396. <div key={card.id} className="flex flex-col sm:flex-row sm:items-center justify-between p-4 border rounded-lg bg-gray-50 hover:bg-gray-100 transition-colors gap-4">
  397. <div className="flex items-center gap-4">
  398. {(() => {
  399. const previewUrl = card.imageUrl || card.extraMedia?.[0]?.url || '';
  400. if (!previewUrl) {
  401. return <div className="w-16 h-16 bg-gray-200 rounded-md shadow-sm flex items-center justify-center text-gray-400 text-xs shrink-0">No Image</div>;
  402. }
  403. return isVideoUrl(previewUrl)
  404. ? <video src={previewUrl} className="w-16 h-16 object-cover rounded-md shadow-sm shrink-0" muted playsInline preload="metadata" />
  405. : <img src={previewUrl} className="w-16 h-16 object-cover rounded-md shadow-sm shrink-0" alt="" />;
  406. })()}
  407. <div>
  408. <span className="font-semibold text-gray-800 block">{card.title}</span>
  409. <span className="text-xs text-gray-500 uppercase tracking-wider">{card.cardType}</span>
  410. </div>
  411. </div>
  412. {/* CHANGED: flex-wrap to ensure buttons don't overflow on small screens, w-full on mobile */}
  413. <div className="flex flex-wrap items-center gap-2 w-full sm:w-auto justify-end">
  414. <button onClick={() => moveCard(idx, 'up')} className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded" title="Move Up">↑</button>
  415. <button onClick={() => moveCard(idx, 'down')} className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded" title="Move Down">↓</button>
  416. <div className="w-px h-6 bg-gray-300 mx-1 hidden sm:block"></div>
  417. <button onClick={() => setIsEditing(card)} className="px-4 py-2 text-blue-600 hover:bg-blue-50 rounded font-medium">Edit</button>
  418. <button onClick={() => handleDeleteCard(card.id)} className="px-4 py-2 text-red-600 hover:bg-red-50 rounded font-medium">Delete</button>
  419. </div>
  420. </div>
  421. ))}
  422. </div>
  423. </div>
  424. )}
  425. {/* TAB: SETTINGS */}
  426. {activeTab === 'settings' && (
  427. <div className="p-6 md:p-8">
  428. <h2 className="text-xl font-bold text-gray-800 mb-8 border-b pb-4">Global Portal Settings</h2>
  429. <div className="grid grid-cols-1 md:grid-cols-2 gap-10">
  430. <div className="space-y-6">
  431. <div>
  432. <label className="block text-sm font-semibold text-gray-700 mb-1">Portal Title</label>
  433. <input type="text" value={portal.title || ''} onChange={e => setPortal({...portal, title: e.target.value})} className={inputClasses} />
  434. </div>
  435. <div>
  436. <label className="block text-sm font-semibold text-gray-700 mb-1">Welcome Text</label>
  437. <textarea value={portal.welcomeText || ''} onChange={e => setPortal({...portal, welcomeText: e.target.value})} className={`${inputClasses} h-32 resize-none`} />
  438. </div>
  439. <div className="flex gap-8">
  440. <div>
  441. <label className="block text-sm font-semibold text-gray-700 mb-1">Theme Color</label>
  442. <div className="flex items-center gap-4">
  443. <input type="color" value={portal.themeColor || '#1e3a8a'} onChange={e => setPortal({...portal, themeColor: e.target.value})} className="h-12 w-12 rounded cursor-pointer border-0 p-0" />
  444. <span className="text-gray-900 font-mono font-medium">{portal.themeColor || '#1e3a8a'}</span>
  445. </div>
  446. </div>
  447. {/* NEW: Max Columns Setting updated for 3 */}
  448. <div className="flex-1">
  449. <label className="block text-sm font-semibold text-gray-700 mb-1">Grid Max Columns: {portal.maxGridColumns || 5}</label>
  450. <input
  451. type="range"
  452. min="3"
  453. max="8"
  454. value={portal.maxGridColumns || 5}
  455. onChange={e => setPortal({...portal, maxGridColumns: parseInt(e.target.value)})}
  456. className="w-full mt-3 accent-blue-600"
  457. />
  458. <div className="flex justify-between text-xs text-gray-400 mt-1">
  459. <span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span>
  460. </div>
  461. </div>
  462. </div>
  463. <div>
  464. <label className="block text-sm font-semibold text-gray-700 mb-1">Font del portale</label>
  465. <StyledSelect<string>
  466. value={portal.fontFamily ?? ''}
  467. onChange={(v) => setPortal({ ...portal, fontFamily: v })}
  468. options={[
  469. { value: '', label: 'Sistema (Arial)' },
  470. ...availableFonts.map(f => ({ value: f, label: f.replace(/\.(woff2?|ttf|otf)$/i, '') })),
  471. ]}
  472. />
  473. <p className="text-xs text-gray-500 mt-1">
  474. Per aggiungere altri font, copia file <code className="bg-gray-100 px-1 rounded">.woff2</code>, <code className="bg-gray-100 px-1 rounded">.woff</code>, <code className="bg-gray-100 px-1 rounded">.ttf</code> o <code className="bg-gray-100 px-1 rounded">.otf</code> in <code className="bg-gray-100 px-1 rounded">data/fonts/</code> e ricarica questa pagina.
  475. </p>
  476. </div>
  477. </div>
  478. <div className="space-y-6">
  479. {/* Logo Upload with Remove Button */}
  480. <div>
  481. <label className="block text-sm font-semibold text-gray-700 mb-1">Logo Image</label>
  482. <input type="file" accept="image/*" onChange={e => handleUpload(e, 'logoUrl', true)} className="block w-full text-sm text-gray-900 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:bg-gray-100 cursor-pointer" />
  483. {uploading['logoUrl'] && <span className="text-xs text-blue-500">Uploading...</span>}
  484. {portal.logoUrl && (
  485. <div className="mt-2 bg-gray-100 p-4 rounded inline-block relative border">
  486. <img src={portal.logoUrl} className="h-16 object-contain" alt="Logo Preview" />
  487. <a href={portal.logoUrl} download={extractFileName(portal.logoUrl)} className="absolute -top-2 right-6 bg-gray-700 hover:bg-gray-800 text-white w-6 h-6 rounded-full text-xs font-bold shadow flex items-center justify-center" title="Scarica" aria-label="Scarica logo">⬇</a>
  488. <button onClick={() => setPortal({...portal, logoUrl: ''})} className="absolute -top-2 -right-2 bg-red-500 text-white w-6 h-6 rounded-full text-xs font-bold hover:bg-red-600 shadow">✕</button>
  489. </div>
  490. )}
  491. </div>
  492. {/* Hero Upload with Remove Button */}
  493. <div>
  494. <label className="block text-sm font-semibold text-gray-700 mb-1">Hero Background Image</label>
  495. <input type="file" accept="image/*" onChange={e => handleUpload(e, 'heroImageUrl', true)} className="block w-full text-sm text-gray-900 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:bg-gray-100 cursor-pointer" />
  496. {uploading['heroImageUrl'] && <span className="text-xs text-blue-500">Uploading...</span>}
  497. {portal.heroImageUrl && (
  498. <div className="mt-2 relative rounded shadow border inline-block w-full">
  499. <img src={portal.heroImageUrl} className="h-32 w-full object-cover rounded" alt="Hero Preview" />
  500. <a href={portal.heroImageUrl} download={extractFileName(portal.heroImageUrl)} className="absolute top-2 right-12 bg-gray-700 hover:bg-gray-800 text-white w-8 h-8 flex items-center justify-center rounded-full text-sm font-bold shadow-lg" title="Scarica" aria-label="Scarica hero">⬇</a>
  501. <button onClick={() => setPortal({...portal, heroImageUrl: ''})} className="absolute top-2 right-2 bg-red-500 text-white w-8 h-8 flex items-center justify-center rounded-full text-sm font-bold hover:bg-red-600 shadow-lg">✕</button>
  502. </div>
  503. )}
  504. </div>
  505. <div className="bg-gray-50 p-4 rounded-lg border border-gray-200 space-y-3">
  506. <label className="flex items-center gap-3 cursor-pointer">
  507. <input type="checkbox" checked={!!portal.fadeHeroImage} onChange={e => setPortal({...portal, fadeHeroImage: e.target.checked})} className="w-5 h-5 text-blue-600 rounded" />
  508. <div>
  509. <span className="block text-sm font-semibold text-gray-900">Fade Image into Background Color</span>
  510. <span className="block text-xs text-gray-600">Creates a smooth gradient from the top of the image into the solid theme color at the bottom.</span>
  511. </div>
  512. </label>
  513. <label className="flex items-center gap-3 cursor-pointer">
  514. <input
  515. type="checkbox"
  516. checked={portal.externalLinkEnabled ?? EXTERNAL_LINK_DEFAULT}
  517. onChange={e => setPortal({...portal, externalLinkEnabled: e.target.checked})}
  518. className="w-5 h-5 text-blue-600 rounded"
  519. />
  520. <div>
  521. <span className="block text-sm font-semibold text-gray-900">Abilita &ldquo;External Link&rdquo;</span>
  522. <span className="block text-xs text-gray-600">Mostra il tipo &ldquo;External Link&rdquo; nel dropdown del Card Type. Le card esistenti di quel tipo restano comunque visibili e cliccabili.</span>
  523. </div>
  524. </label>
  525. </div>
  526. </div>
  527. </div>
  528. <div className="mt-10 pt-6 border-t border-gray-200 flex justify-end">
  529. <button onClick={handleSavePortal} disabled={savingPortal} className="bg-blue-600 text-white px-10 py-3 rounded-lg hover:bg-blue-700 font-bold shadow disabled:opacity-50 transition-colors">
  530. {savingPortal ? 'Saving...' : 'Save Portal Settings'}
  531. </button>
  532. </div>
  533. </div>
  534. )}
  535. </div>
  536. </div>
  537. {/* MODAL FOR EDITING/CREATING CARDS */}
  538. {isEditing && (
  539. <div
  540. className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm p-4 transition-opacity"
  541. onClick={() => setIsEditing(null)} // Click outside to close
  542. >
  543. <div
  544. className="bg-white rounded-2xl w-full max-w-3xl max-h-[90vh] overflow-y-auto shadow-2xl p-6 md:p-8 relative animate-in fade-in zoom-in-95 duration-200"
  545. onClick={(e) => e.stopPropagation()} // Prevent inside clicks from closing
  546. >
  547. <button
  548. onClick={() => setIsEditing(null)}
  549. className="absolute top-6 right-6 text-gray-400 hover:text-gray-900 bg-gray-100 hover:bg-gray-200 rounded-full w-8 h-8 flex items-center justify-center transition-colors"
  550. >
  551. </button>
  552. <h3 className="text-2xl font-bold mb-6 text-gray-900 border-b pb-4">
  553. {isEditing.id ? 'Edit Card' : 'Create New Card'}
  554. </h3>
  555. <div className="grid grid-cols-1 md:grid-cols-2 gap-8">
  556. <div className="space-y-5">
  557. <div>
  558. <label className="block text-sm font-semibold text-gray-800 mb-1">Title</label>
  559. <input type="text" value={isEditing.title || ''} onChange={e => setIsEditing({...isEditing, title: e.target.value})} className={inputClasses} placeholder="e.g., Local History" />
  560. </div>
  561. <div>
  562. <label className="block text-sm font-semibold text-gray-800 mb-1">Card Type</label>
  563. <StyledSelect<CardType>
  564. value={(isEditing.cardType || 'INFO_PAGE') as CardType}
  565. onChange={(v) => setIsEditing({ ...isEditing, cardType: v })}
  566. options={[
  567. { value: 'INFO_PAGE', label: 'Info Page' },
  568. { value: 'IMAGE_GALLERY', label: 'Image Gallery' },
  569. ...(externalLinksOn ? [{ value: 'EXTERNAL_LINK' as CardType, label: 'External Link' }] : []),
  570. ]}
  571. />
  572. </div>
  573. {isEditing.cardType === 'EXTERNAL_LINK' ? (
  574. <>
  575. <div>
  576. <label className="block text-sm font-semibold text-gray-800 mb-1">URL</label>
  577. <input
  578. type="url"
  579. value={isEditing.actionUrl || ''}
  580. onChange={e => setIsEditing({ ...isEditing, actionUrl: e.target.value })}
  581. className={inputClasses}
  582. placeholder="https://esempio.it/pagina"
  583. />
  584. </div>
  585. <div>
  586. <label className="block text-sm font-semibold text-gray-800 mb-1">Testo del link</label>
  587. <input
  588. type="text"
  589. value={isEditing.shortDescription || ''}
  590. onChange={e => setIsEditing({ ...isEditing, shortDescription: e.target.value })}
  591. className={inputClasses}
  592. placeholder="es. Visita il sito ufficiale"
  593. />
  594. <p className="text-xs text-gray-500 mt-1">Testo visualizzato come link cliccabile nel modale. Se vuoto, viene mostrata l&rsquo;URL stessa.</p>
  595. </div>
  596. <div className="bg-gray-50 p-3 rounded-lg border border-gray-200">
  597. <label className="flex items-start gap-3 cursor-pointer">
  598. <input
  599. type="checkbox"
  600. checked={!!isEditing.redirectOnClick}
  601. onChange={e => setIsEditing({ ...isEditing, redirectOnClick: e.target.checked })}
  602. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  603. />
  604. <div>
  605. <span className="block text-sm font-semibold text-gray-900">Redirect on click</span>
  606. <span className="block text-xs text-gray-600">Cliccando la card sul portale pubblico, il browser apre direttamente l&rsquo;URL senza mostrare il modale.</span>
  607. </div>
  608. </label>
  609. </div>
  610. </>
  611. ) : (
  612. <div>
  613. <label className="block text-sm font-semibold text-gray-800 mb-1">Short Description</label>
  614. <textarea value={isEditing.shortDescription || ''} onChange={e => setIsEditing({ ...isEditing, shortDescription: e.target.value })} className={`${inputClasses} h-24 resize-none`} placeholder="Brief summary..." />
  615. </div>
  616. )}
  617. <div className="bg-gray-50 p-3 rounded-lg border border-gray-200 space-y-3">
  618. <label className="flex items-start gap-3 cursor-pointer">
  619. <input
  620. type="checkbox"
  621. checked={!!isEditing.autoFullscreen}
  622. onChange={e => setIsEditing({ ...isEditing, autoFullscreen: e.target.checked })}
  623. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  624. />
  625. <div>
  626. <span className="block text-sm font-semibold text-gray-900">Auto fullscreen</span>
  627. <span className="block text-xs text-gray-600">Open the gallery in fullscreen immediately when the user clicks this card.</span>
  628. </div>
  629. </label>
  630. <label className="flex items-start gap-3 cursor-pointer">
  631. <input
  632. type="checkbox"
  633. checked={!!isEditing.skipPreview}
  634. onChange={e => setIsEditing({ ...isEditing, skipPreview: e.target.checked })}
  635. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  636. />
  637. <div>
  638. <span className="block text-sm font-semibold text-gray-900">Don&rsquo;t show the cover as a slide in the gallery.</span>
  639. <span className="block text-xs text-gray-600">The cover stays as the card thumbnail only. Combine with &ldquo;Auto fullscreen&rdquo; to jump straight into the gallery items.</span>
  640. </div>
  641. </label>
  642. </div>
  643. </div>
  644. <div className="space-y-5">
  645. {/* Cover Image */}
  646. <div>
  647. <label className="block text-sm font-semibold text-gray-800 mb-1">
  648. Cover Image <span className="text-gray-400 font-normal text-xs">(shown in grid)</span>
  649. </label>
  650. <div className="border-2 border-dashed border-gray-300 rounded-lg p-3 hover:bg-gray-50 transition-colors">
  651. <input type="file" accept="image/*" onChange={e => handleUpload(e, 'imageUrl')} className="block w-full text-sm text-gray-700 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100 cursor-pointer" />
  652. {uploading['imageUrl'] && <p className="mt-2 text-sm text-blue-600 font-medium">Uploading...</p>}
  653. </div>
  654. {isEditing.imageUrl && (
  655. <div className="mt-3 relative rounded-lg overflow-hidden border border-gray-200 group">
  656. <img src={isEditing.imageUrl} className="w-full h-32 object-cover" alt="Cover preview" />
  657. <a
  658. href={isEditing.imageUrl}
  659. download={extractFileName(isEditing.imageUrl)}
  660. className="absolute top-2 right-12 bg-gray-700 hover:bg-gray-800 text-white w-8 h-8 rounded-full text-sm font-bold shadow opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center"
  661. title="Scarica"
  662. aria-label="Scarica cover"
  663. >⬇</a>
  664. <button
  665. onClick={() => setIsEditing({...isEditing, imageUrl: ''})}
  666. className="absolute top-2 right-2 bg-red-500 text-white w-8 h-8 rounded-full text-sm font-bold shadow opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-600"
  667. title="Remove cover image"
  668. >✕</button>
  669. </div>
  670. )}
  671. </div>
  672. {/* Gallery Media (images + videos + PDFs) — nascosta per INFO_PAGE (solo cover ammessa) */}
  673. {isEditing.cardType !== 'INFO_PAGE' && (
  674. <div>
  675. <label className="block text-sm font-semibold text-gray-800 mb-1">
  676. Gallery Media <span className="text-gray-400 font-normal text-xs">(images, videos or PDFs — PDF pages become images)</span>
  677. </label>
  678. <div className="border-2 border-dashed border-gray-300 rounded-lg p-3 hover:bg-gray-50 transition-colors">
  679. <input
  680. type="file"
  681. accept="image/*,video/*,application/pdf,.pdf,.mov,.qt,.mkv,.avi,.divx,.wmv,.asf,.flv,.f4v,.3gp,.3gpp,.3g2,.mts,.m2ts,.ts,.mpg,.mpeg,.vob,.mxf,.ogv,.ogg"
  682. multiple
  683. onChange={handleUploadExtraMedia}
  684. className="block w-full text-sm text-gray-700 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-purple-50 file:text-purple-700 hover:file:bg-purple-100 cursor-pointer"
  685. />
  686. {uploading['extraMedia'] && !pdfProgress && <p className="mt-2 text-sm text-purple-600 font-medium">Uploading...</p>}
  687. {pdfProgress && (
  688. <p className="mt-2 text-sm text-purple-600 font-medium">
  689. Processing &ldquo;{pdfProgress.name}&rdquo;: page {pdfProgress.page} of {pdfProgress.total}
  690. </p>
  691. )}
  692. </div>
  693. {(isEditing.extraMedia || []).length > 0 && (
  694. <div className="mt-3 space-y-2">
  695. {(isEditing.extraMedia || []).map((item, i) => {
  696. const video = isVideoUrl(item.url);
  697. return (
  698. <div key={item.url + i} className="flex items-center gap-3 p-2 bg-gray-50 border border-gray-200 rounded-lg">
  699. <div className="relative w-16 h-16 rounded-md overflow-hidden bg-black shrink-0">
  700. {video ? (
  701. <>
  702. <video src={item.url} className="w-full h-full object-cover" muted preload="metadata" />
  703. <div className="absolute inset-0 flex items-center justify-center bg-black/30 text-white text-xl">▶</div>
  704. </>
  705. ) : (
  706. <img src={item.url} className="w-full h-full object-cover" alt="" />
  707. )}
  708. <span className="absolute bottom-0 left-0 right-0 text-center text-white text-[10px] bg-black/60">{i + 1}</span>
  709. </div>
  710. <div className="flex-1 min-w-0">
  711. <div className="text-xs font-semibold text-gray-700 uppercase tracking-wider">
  712. {video ? 'Video' : 'Image'}
  713. </div>
  714. {video && (
  715. <div className="mt-1 flex flex-wrap gap-x-4 gap-y-1">
  716. <label className="flex items-center gap-2 cursor-pointer">
  717. <input
  718. type="checkbox"
  719. checked={!!item.autoplay}
  720. onChange={() => toggleAutoplay(i)}
  721. className="w-4 h-4 text-blue-600 rounded"
  722. />
  723. <span className="text-sm text-gray-700">Autoplay</span>
  724. </label>
  725. <label className="flex items-center gap-2 cursor-pointer">
  726. <input
  727. type="checkbox"
  728. checked={!!item.muted}
  729. onChange={() => toggleMuted(i)}
  730. className="w-4 h-4 text-blue-600 rounded"
  731. />
  732. <span className="text-sm text-gray-700">Muted</span>
  733. </label>
  734. </div>
  735. )}
  736. </div>
  737. <a
  738. href={item.url}
  739. download={extractFileName(item.url)}
  740. className="bg-gray-500 hover:bg-gray-600 text-white w-8 h-8 rounded-full text-sm font-bold shrink-0 flex items-center justify-center"
  741. title="Scarica"
  742. aria-label="Scarica"
  743. >⬇</a>
  744. <button
  745. onClick={() => removeExtraMedia(i)}
  746. className="bg-red-500 hover:bg-red-600 text-white w-8 h-8 rounded-full text-sm font-bold shrink-0"
  747. title="Remove"
  748. >✕</button>
  749. </div>
  750. );
  751. })}
  752. </div>
  753. )}
  754. </div>
  755. )}
  756. </div>
  757. </div>
  758. <div className="flex gap-3 pt-8 mt-6 border-t border-gray-200 justify-end">
  759. <button onClick={() => setIsEditing(null)} className="px-5 py-2.5 text-gray-700 hover:bg-gray-100 rounded-lg font-medium transition-colors">
  760. Cancel
  761. </button>
  762. <button onClick={handleSaveCard} className="bg-green-600 text-white px-8 py-2.5 rounded-lg hover:bg-green-700 font-medium shadow-sm transition-colors">
  763. Save Card
  764. </button>
  765. </div>
  766. </div>
  767. </div>
  768. )}
  769. {/* CUSTOM CONFIRM DIALOG */}
  770. {confirmDialog && (
  771. <div className="fixed inset-0 z-[60] flex items-center justify-center bg-black/60 backdrop-blur-sm p-4 animate-in fade-in duration-200">
  772. <div className="bg-white rounded-xl shadow-2xl p-6 max-w-sm w-full animate-in zoom-in-95">
  773. <h3 className="text-xl font-bold text-gray-900 mb-2">Confirm Action</h3>
  774. <p className="text-gray-600 mb-6 leading-relaxed">{confirmDialog.message}</p>
  775. <div className="flex justify-end gap-3">
  776. <button
  777. onClick={() => setConfirmDialog(null)}
  778. className="px-4 py-2.5 text-gray-700 hover:bg-gray-100 rounded-lg font-medium transition-colors"
  779. >
  780. Cancel
  781. </button>
  782. <button
  783. onClick={confirmDialog.onConfirm}
  784. className="px-6 py-2.5 bg-red-600 text-white rounded-lg hover:bg-red-700 font-medium transition-colors shadow-sm"
  785. >
  786. Delete
  787. </button>
  788. </div>
  789. </div>
  790. </div>
  791. )}
  792. {/* CUSTOM TOAST NOTIFICATION */}
  793. {toast && (
  794. <div className={`fixed bottom-6 right-6 z-[70] text-white px-6 py-4 rounded-lg shadow-2xl flex items-start gap-3 animate-in slide-in-from-bottom-5 fade-in duration-300 max-w-md ${
  795. toast.type === 'error' ? 'bg-red-700' : 'bg-gray-900'
  796. }`}>
  797. <div className={`w-6 h-6 rounded-full flex items-center justify-center font-bold text-sm shrink-0 mt-0.5 ${
  798. toast.type === 'error' ? 'bg-white text-red-700' : 'bg-green-500 text-gray-900'
  799. }`}>
  800. {toast.type === 'error' ? '!' : '✓'}
  801. </div>
  802. <span className="font-medium leading-snug">{toast.message}</span>
  803. </div>
  804. )}
  805. </div>
  806. );
  807. }