Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

1185 строки
60 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. import { CARD_LIMITS, PORTAL_LIMITS } from '@/lib/validation';
  6. type CharCounterProps = { value: string | undefined; limit: number };
  7. function CharCounter({ value, limit }: CharCounterProps) {
  8. const len = (value ?? '').length;
  9. if (len < limit * 0.8) return null;
  10. const overflow = len > limit;
  11. return (
  12. <p className={`text-xs mt-1 text-right ${overflow ? 'text-red-600 font-semibold' : 'text-gray-500'}`}>
  13. {len} / {limit}
  14. </p>
  15. );
  16. }
  17. function stripTags(html: string): string {
  18. if (typeof window === 'undefined' || !html) return '';
  19. return new DOMParser().parseFromString(html, 'text/html').body.textContent ?? '';
  20. }
  21. type RichTextMiniProps = {
  22. value: string;
  23. onChange: (html: string) => void;
  24. limit: number;
  25. className?: string;
  26. };
  27. function RichTextMini({ value, onChange, limit, className }: RichTextMiniProps) {
  28. const ref = useRef<HTMLDivElement>(null);
  29. // Sync iniziale soltanto. Aggiornare innerHTML durante l'editing perderebbe la
  30. // posizione del cursore, quindi confidiamo che onInput tenga value e DOM allineati.
  31. useEffect(() => {
  32. if (ref.current && ref.current.innerHTML !== value) {
  33. ref.current.innerHTML = value || '';
  34. }
  35. // eslint-disable-next-line react-hooks/exhaustive-deps
  36. }, []);
  37. const exec = (cmd: 'bold' | 'italic') => {
  38. ref.current?.focus();
  39. document.execCommand(cmd);
  40. onChange(ref.current?.innerHTML || '');
  41. };
  42. return (
  43. <div>
  44. <div className="flex gap-1 mb-1">
  45. <button
  46. type="button"
  47. onClick={() => exec('bold')}
  48. className="font-bold w-8 h-8 border border-gray-300 rounded hover:bg-gray-100"
  49. title="Grassetto"
  50. >B</button>
  51. <button
  52. type="button"
  53. onClick={() => exec('italic')}
  54. className="italic w-8 h-8 border border-gray-300 rounded hover:bg-gray-100"
  55. title="Corsivo"
  56. >I</button>
  57. </div>
  58. <div
  59. ref={ref}
  60. contentEditable
  61. suppressContentEditableWarning
  62. onInput={(e) => onChange((e.target as HTMLDivElement).innerHTML)}
  63. className={className ?? 'w-full border border-gray-300 rounded-lg p-2.5 min-h-[8rem] bg-white text-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500'}
  64. />
  65. <CharCounter value={stripTags(value)} limit={limit} />
  66. </div>
  67. );
  68. }
  69. function StyledSelect<T extends string>({
  70. value,
  71. onChange,
  72. options,
  73. }: {
  74. value: T;
  75. onChange: (v: T) => void;
  76. options: { value: T; label: string; style?: React.CSSProperties }[];
  77. }) {
  78. const [open, setOpen] = useState(false);
  79. const ref = useRef<HTMLDivElement>(null);
  80. useEffect(() => {
  81. if (!open) return;
  82. const onClick = (e: MouseEvent) => {
  83. if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
  84. };
  85. const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); };
  86. document.addEventListener('mousedown', onClick);
  87. document.addEventListener('keydown', onKey);
  88. return () => {
  89. document.removeEventListener('mousedown', onClick);
  90. document.removeEventListener('keydown', onKey);
  91. };
  92. }, [open]);
  93. const current = options.find(o => o.value === value);
  94. // Fallback: se il value non matcha nessuna opzione (es. tipo disattivato dalla flag), mostra il valore raw prettificato
  95. const displayLabel = current?.label
  96. ?? (typeof value === 'string' && value.length > 0
  97. ? value.replace(/_/g, ' ').toLowerCase().replace(/\b\w/g, c => c.toUpperCase())
  98. : '');
  99. 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";
  100. return (
  101. <div ref={ref} className="relative">
  102. <button
  103. type="button"
  104. onClick={() => setOpen(o => !o)}
  105. className={`${inputBase} text-left flex items-center justify-between cursor-pointer`}
  106. >
  107. <span className={displayLabel ? '' : 'text-gray-400'} style={current?.style}>{displayLabel || 'Seleziona…'}</span>
  108. <span className={`text-gray-500 transition-transform ${open ? 'rotate-180' : ''}`}>▾</span>
  109. </button>
  110. {open && (
  111. <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">
  112. {options.map(o => (
  113. <button
  114. key={o.value}
  115. type="button"
  116. onClick={() => { onChange(o.value); setOpen(false); }}
  117. 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'}`}
  118. style={o.style}
  119. >
  120. {o.label}
  121. </button>
  122. ))}
  123. </div>
  124. )}
  125. </div>
  126. );
  127. }
  128. 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';
  129. // Sottoinsieme di formati video davvero riproducibili dai browser moderni
  130. const PLAYBACK_SUPPORTED_VIDEO = 'mp4|m4v|webm|mov|qt|ogv|ogg';
  131. const PLAYBACK_SUPPORTED_LABEL = 'MP4, M4V, WebM, MOV, OGV';
  132. const isVideoUrl = (url: string) => new RegExp(`\\.(${VIDEO_EXTENSIONS})(\\?|$)`, 'i').test(url);
  133. const isPdfFile = (file: File) =>
  134. file.type === 'application/pdf' || /\.pdf$/i.test(file.name);
  135. const isVideoFile = (file: File) =>
  136. file.type.startsWith('video/') || new RegExp(`\\.(${VIDEO_EXTENSIONS})$`, 'i').test(file.name);
  137. const isPlayableVideoFile = (file: File) =>
  138. new RegExp(`\\.(${PLAYBACK_SUPPORTED_VIDEO})$`, 'i').test(file.name);
  139. const previewFontFamily = (filename: string): string =>
  140. `PortalPreview-${filename.replace(/[^A-Za-z0-9]/g, '_')}`;
  141. const fontFormatFromName = (filename: string): string => {
  142. const ext = filename.match(/\.([^.]+)$/)?.[1].toLowerCase() ?? 'woff2';
  143. return ({ woff2: 'woff2', woff: 'woff', ttf: 'truetype', otf: 'opentype' } as Record<string, string>)[ext] ?? 'woff2';
  144. };
  145. const extractFileName = (url: string): string => {
  146. const match = url.match(/[?&]name=([^&]+)/);
  147. if (match) return decodeURIComponent(match[1]);
  148. const seg = url.split('/').pop() || 'download';
  149. return seg.split('?')[0];
  150. };
  151. async function uploadBlobAsImage(blob: Blob, name: string): Promise<string | null> {
  152. const formData = new FormData();
  153. formData.append('file', new File([blob], name, { type: blob.type || 'image/png' }));
  154. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  155. const data = await res.json();
  156. return data.url || null;
  157. }
  158. async function extractVideoFrame(file: File): Promise<Blob | null> {
  159. const url = URL.createObjectURL(file);
  160. try {
  161. const video = document.createElement('video');
  162. video.muted = true;
  163. video.playsInline = true;
  164. video.preload = 'metadata';
  165. video.src = url;
  166. await new Promise<void>((resolve, reject) => {
  167. video.addEventListener('loadedmetadata', () => resolve(), { once: true });
  168. video.addEventListener('error', () => reject(new Error('video load error')), { once: true });
  169. });
  170. // Seek slightly past 0 — at exactly 0 some codecs return a black frame
  171. video.currentTime = Math.min(0.1, Math.max(0, video.duration / 10));
  172. await new Promise<void>((resolve, reject) => {
  173. video.addEventListener('seeked', () => resolve(), { once: true });
  174. video.addEventListener('error', () => reject(new Error('video seek error')), { once: true });
  175. });
  176. const canvas = document.createElement('canvas');
  177. canvas.width = video.videoWidth;
  178. canvas.height = video.videoHeight;
  179. const ctx = canvas.getContext('2d');
  180. if (!ctx) return null;
  181. ctx.drawImage(video, 0, 0);
  182. return await new Promise<Blob | null>((resolve) =>
  183. canvas.toBlob((b) => resolve(b), 'image/jpeg', 0.85)
  184. );
  185. } finally {
  186. URL.revokeObjectURL(url);
  187. }
  188. }
  189. async function pdfToImageItems(
  190. file: File,
  191. onProgress: (page: number, total: number) => void
  192. ): Promise<MediaItem[]> {
  193. const pdfjs = await import('pdfjs-dist');
  194. // Worker file is copied to /public via the postinstall script
  195. pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.mjs';
  196. const arrayBuffer = await file.arrayBuffer();
  197. const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
  198. const baseName = file.name.replace(/\.pdf$/i, '').replace(/[^a-zA-Z0-9-_]/g, '_');
  199. const items: MediaItem[] = [];
  200. for (let i = 1; i <= pdf.numPages; i++) {
  201. onProgress(i, pdf.numPages);
  202. const page = await pdf.getPage(i);
  203. const viewport = page.getViewport({ scale: 1.5 });
  204. const canvas = document.createElement('canvas');
  205. canvas.width = viewport.width;
  206. canvas.height = viewport.height;
  207. const ctx = canvas.getContext('2d');
  208. if (!ctx) continue;
  209. await page.render({ canvasContext: ctx, viewport }).promise;
  210. const blob: Blob = await new Promise((resolve, reject) => {
  211. canvas.toBlob(b => b ? resolve(b) : reject(new Error('toBlob failed')), 'image/png');
  212. });
  213. const url = await uploadBlobAsImage(blob, `${baseName}-page${i}.png`);
  214. if (url) items.push({ url });
  215. }
  216. return items;
  217. }
  218. export default function AdminDashboard() {
  219. const [activeTab, setActiveTab] = useState<'cards' | 'settings'>('cards');
  220. // Card State
  221. const [cards, setCards] = useState<Card[]>([]);
  222. const [isEditing, setIsEditing] = useState<Partial<Card> | null>(null);
  223. // Portal State
  224. const [portal, setPortal] = useState<Partial<Portal>>({});
  225. const [savingPortal, setSavingPortal] = useState(false);
  226. const [uploading, setUploading] = useState<{ [key: string]: boolean }>({});
  227. // NEW UI STATES: Toast and Confirm Dialog
  228. const [toast, setToast] = useState<{ message: string; type: 'success' | 'error' } | null>(null);
  229. const [confirmDialog, setConfirmDialog] = useState<{ message: string, onConfirm: () => void } | null>(null);
  230. const [pdfProgress, setPdfProgress] = useState<{ name: string; page: number; total: number } | null>(null);
  231. const [availableFonts, setAvailableFonts] = useState<string[]>([]);
  232. // Map: expected URL of the future-transcoded file → job state.
  233. // We key by URL (not jobId) so the rendering layer can look it up cheaply.
  234. const [transcodeJobs, setTranscodeJobs] = useState<Record<string, { jobId: string; status: string; progress: number }>>({});
  235. // External Link feature flag: priorità al setting del portale, fallback alla costante in lib/config.
  236. const externalLinksOn = portal.externalLinkEnabled ?? EXTERNAL_LINK_DEFAULT;
  237. // Helper to show auto-dismissing toast
  238. const showToast = (message: string, type: 'success' | 'error' = 'success') => {
  239. setToast({ message, type });
  240. setTimeout(() => setToast(null), type === 'error' ? 6000 : 3000);
  241. };
  242. useEffect(() => {
  243. fetch('/api/cards').then(res => res.json()).then(setCards);
  244. fetch('/api/portals').then(res => res.json()).then(data => data && setPortal(data));
  245. fetch('/api/fonts').then(res => res.json()).then(setAvailableFonts).catch(() => setAvailableFonts([]));
  246. }, []);
  247. // Poll pending transcode jobs every 2s. On 'done' we drop the entry from the
  248. // map; on 'failed' we additionally pull the media URL out of the editor so the
  249. // admin doesn't try to save a broken reference.
  250. useEffect(() => {
  251. const pendingEntries = Object.entries(transcodeJobs).filter(
  252. ([, j]) => j.status === 'queued' || j.status === 'running'
  253. );
  254. if (pendingEntries.length === 0) return;
  255. let cancelled = false;
  256. const tick = async () => {
  257. for (const [url, j] of pendingEntries) {
  258. if (cancelled) return;
  259. try {
  260. const res = await fetch(`/api/transcode/${j.jobId}`);
  261. if (!res.ok) continue;
  262. const data = await res.json();
  263. if (cancelled) return;
  264. if (data.status === 'done') {
  265. setTranscodeJobs(prev => {
  266. const next = { ...prev };
  267. delete next[url];
  268. return next;
  269. });
  270. } else if (data.status === 'failed' || data.status === 'cancelled') {
  271. setTranscodeJobs(prev => {
  272. const next = { ...prev };
  273. delete next[url];
  274. return next;
  275. });
  276. setIsEditing(prev => prev ? {
  277. ...prev,
  278. extraMedia: (prev.extraMedia || []).filter(m => m.url !== url),
  279. imageUrl: prev.imageUrl === url ? '' : prev.imageUrl,
  280. } : prev);
  281. const msg = data.status === 'failed'
  282. ? `Trascodifica fallita${data.error ? `: ${String(data.error).split('\n')[0]}` : ''}`
  283. : 'Trascodifica annullata';
  284. showToast(msg, 'error');
  285. } else {
  286. setTranscodeJobs(prev => prev[url] ? ({ ...prev, [url]: { ...prev[url], status: data.status, progress: data.progress ?? 0 } }) : prev);
  287. }
  288. } catch {
  289. // ignore network glitches; will retry next tick
  290. }
  291. }
  292. };
  293. void tick();
  294. const id = window.setInterval(() => { void tick(); }, 2000);
  295. return () => { cancelled = true; window.clearInterval(id); };
  296. // eslint-disable-next-line react-hooks/exhaustive-deps
  297. }, [Object.keys(transcodeJobs).join('|')]);
  298. const handleUpload = async (e: React.ChangeEvent<HTMLInputElement>, field: string, isPortal = false) => {
  299. if (!e.target.files?.[0]) return;
  300. setUploading(prev => ({ ...prev, [field]: true }));
  301. const formData = new FormData();
  302. formData.append('file', e.target.files[0]);
  303. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  304. const data = await res.json();
  305. if (data.url) {
  306. if (isPortal) {
  307. setPortal(prev => ({ ...prev, [field]: data.url }));
  308. } else {
  309. setIsEditing(prev => ({ ...prev, [field]: data.url }));
  310. }
  311. }
  312. setUploading(prev => ({ ...prev, [field]: false }));
  313. };
  314. const handleUploadExtraMedia = async (e: React.ChangeEvent<HTMLInputElement>) => {
  315. const files = e.target.files;
  316. if (!files || files.length === 0) return;
  317. setUploading(prev => ({ ...prev, extraMedia: true }));
  318. const startedWithoutCover = !isEditing?.imageUrl;
  319. let pendingCover: string | null = null;
  320. const canPromote = () => startedWithoutCover && !pendingCover;
  321. // Pre-filtro: scarta video con formati non riproducibili nei browser
  322. const rejected: string[] = [];
  323. const acceptedFiles: File[] = [];
  324. for (const file of Array.from(files)) {
  325. if (isVideoFile(file) && !isPlayableVideoFile(file)) {
  326. rejected.push(file.name);
  327. } else {
  328. acceptedFiles.push(file);
  329. }
  330. }
  331. if (rejected.length > 0) {
  332. const list = rejected.length <= 3
  333. ? rejected.join(', ')
  334. : `${rejected.slice(0, 3).join(', ')} e altri ${rejected.length - 3}`;
  335. showToast(
  336. `Formato non supportato! I formati supportati sono: ${PLAYBACK_SUPPORTED_LABEL}. File ignorati: ${list}`,
  337. 'error'
  338. );
  339. }
  340. if (acceptedFiles.length === 0) {
  341. setUploading(prev => ({ ...prev, extraMedia: false }));
  342. e.target.value = '';
  343. return;
  344. }
  345. const uploaded: MediaItem[] = [];
  346. for (const file of acceptedFiles) {
  347. try {
  348. if (isPdfFile(file)) {
  349. const items = await pdfToImageItems(file, (page, total) =>
  350. setPdfProgress({ name: file.name, page, total })
  351. );
  352. setPdfProgress(null);
  353. if (items.length > 0 && canPromote()) {
  354. // Promote the first PDF page to cover; skip it from the gallery to avoid duplication.
  355. pendingCover = items[0].url;
  356. uploaded.push(...items.slice(1));
  357. } else {
  358. uploaded.push(...items);
  359. }
  360. } else {
  361. const formData = new FormData();
  362. formData.append('file', file);
  363. const res = await fetch('/api/upload', { method: 'POST', body: formData });
  364. const data = await res.json();
  365. if (!data.url) continue;
  366. if (data?.transcoding?.jobId) {
  367. const { jobId, status } = data.transcoding;
  368. setTranscodeJobs(prev => ({ ...prev, [data.url]: { jobId, status, progress: 0 } }));
  369. }
  370. if (isVideoFile(file)) {
  371. // Video always goes to the gallery so users can play it.
  372. uploaded.push({ url: data.url });
  373. // If no cover yet, extract the first frame and use it as the cover.
  374. if (canPromote()) {
  375. try {
  376. const blob = await extractVideoFrame(file);
  377. if (blob) {
  378. const baseName = file.name.replace(/\.[^.]+$/, '').replace(/[^a-zA-Z0-9-_]/g, '_');
  379. const posterUrl = await uploadBlobAsImage(blob, `${baseName}-poster.jpg`);
  380. if (posterUrl) pendingCover = posterUrl;
  381. }
  382. } catch (err) {
  383. console.warn('Could not extract video poster for', file.name, err);
  384. }
  385. }
  386. } else {
  387. // Plain image
  388. if (canPromote()) {
  389. // Promote to cover; skip the gallery to avoid duplication.
  390. pendingCover = data.url;
  391. } else {
  392. uploaded.push({ url: data.url });
  393. }
  394. }
  395. }
  396. } catch (err) {
  397. console.error('Upload failed for', file.name, err);
  398. showToast(`Failed to process "${file.name}".`);
  399. setPdfProgress(null);
  400. }
  401. }
  402. setIsEditing(prev => ({
  403. ...prev,
  404. imageUrl: (startedWithoutCover && pendingCover) ? pendingCover : (prev?.imageUrl || ''),
  405. extraMedia: [...(prev?.extraMedia || []), ...uploaded],
  406. }));
  407. setUploading(prev => ({ ...prev, extraMedia: false }));
  408. e.target.value = '';
  409. };
  410. const removeExtraMedia = (index: number) => {
  411. setIsEditing(prev => ({
  412. ...prev,
  413. extraMedia: (prev?.extraMedia || []).filter((_, i) => i !== index),
  414. }));
  415. };
  416. const moveExtraMedia = (index: number, direction: 'up' | 'down') => {
  417. setIsEditing(prev => {
  418. const items = [...(prev?.extraMedia || [])];
  419. if (direction === 'up' && index > 0) {
  420. [items[index - 1], items[index]] = [items[index], items[index - 1]];
  421. } else if (direction === 'down' && index < items.length - 1) {
  422. [items[index + 1], items[index]] = [items[index], items[index + 1]];
  423. } else {
  424. return prev;
  425. }
  426. return { ...prev, extraMedia: items };
  427. });
  428. };
  429. const toggleAutoplay = (index: number) => {
  430. setIsEditing(prev => ({
  431. ...prev,
  432. extraMedia: (prev?.extraMedia || []).map((m, i) =>
  433. i === index ? { ...m, autoplay: !m.autoplay } : m
  434. ),
  435. }));
  436. };
  437. const toggleMuted = (index: number) => {
  438. setIsEditing(prev => ({
  439. ...prev,
  440. extraMedia: (prev?.extraMedia || []).map((m, i) =>
  441. i === index ? { ...m, muted: !m.muted } : m
  442. ),
  443. }));
  444. };
  445. const handleSaveCard = async () => {
  446. if (!isEditing) return;
  447. const generateSafeId = () => 'card-' + Date.now().toString(36) + Math.random().toString(36).substring(2);
  448. const newCard = { ...isEditing, id: isEditing.id || generateSafeId() } as Card;
  449. const res = await fetch('/api/cards', {
  450. method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(newCard)
  451. });
  452. if (!res.ok) {
  453. let message = 'Errore di salvataggio';
  454. try {
  455. const body = await res.json();
  456. if (res.status === 400 && Array.isArray(body?.errors) && body.errors.length > 0) {
  457. const first = body.errors[0];
  458. message = first.limit != null
  459. ? `${first.field}: ${first.message} (${first.actual} / ${first.limit})`
  460. : `${first.field}: ${first.message}`;
  461. } else if (body?.error) {
  462. message = body.error;
  463. }
  464. } catch {}
  465. showToast(message, 'error');
  466. return; // keep the editor open so the admin can fix
  467. }
  468. setCards(prev => {
  469. const exists = prev.find(c => c.id === newCard.id);
  470. return exists ? prev.map(c => c.id === newCard.id ? newCard : c) : [...prev, newCard];
  471. });
  472. setIsEditing(null);
  473. };
  474. const handleDeleteCard = (id: string) => {
  475. // Replace window.confirm with our custom dialog
  476. setConfirmDialog({
  477. message: 'Are you sure you want to delete this card? This action cannot be undone.',
  478. onConfirm: async () => {
  479. await fetch(`/api/cards?id=${id}`, { method: 'DELETE' });
  480. setCards(prev => prev.filter(c => c.id !== id));
  481. setConfirmDialog(null);
  482. showToast('Card successfully deleted.');
  483. }
  484. });
  485. };
  486. const moveCard = async (index: number, direction: 'up' | 'down') => {
  487. const newCards = [...cards];
  488. if (direction === 'up' && index > 0) {
  489. [newCards[index - 1], newCards[index]] = [newCards[index], newCards[index - 1]];
  490. } else if (direction === 'down' && index < newCards.length - 1) {
  491. [newCards[index + 1], newCards[index]] = [newCards[index], newCards[index + 1]];
  492. } else {
  493. return; // Do nothing if trying to move out of bounds
  494. }
  495. // Recalculate displayOrder for the whole array
  496. const updatedCards = newCards.map((c, i) => ({ ...c, displayOrder: i }));
  497. // Optimistically update the UI
  498. setCards(updatedCards);
  499. // Persist the new order to the backend
  500. await fetch('/api/cards', {
  501. method: 'PUT',
  502. headers: { 'Content-Type': 'application/json' },
  503. body: JSON.stringify(updatedCards)
  504. });
  505. };
  506. const handleSavePortal = async () => {
  507. setSavingPortal(true);
  508. await fetch('/api/portals', {
  509. method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(portal)
  510. });
  511. setSavingPortal(false);
  512. showToast('Portal settings saved successfully!'); // Replaced window.alert
  513. };
  514. const handleBackupDownload = () => {
  515. window.location.href = '/api/admin/backup';
  516. };
  517. const [restoring, setRestoring] = useState(false);
  518. const handleRestoreUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
  519. const file = e.target.files?.[0];
  520. e.target.value = '';
  521. if (!file) return;
  522. if (!window.confirm('Il ripristino sovrascriverà tutti i dati attuali (card, portale, media, font). Continuare?')) return;
  523. setRestoring(true);
  524. try {
  525. const fd = new FormData();
  526. fd.append('file', file);
  527. const res = await fetch('/api/admin/restore', { method: 'POST', body: fd });
  528. const data = await res.json().catch(() => ({}));
  529. if (!res.ok) {
  530. showToast(data?.error || `Errore ripristino (${res.status})`, 'error');
  531. return;
  532. }
  533. showToast(`Ripristino completato: ${data.restored?.cards ?? 0} card, ${data.restored?.portals ?? 0} portali. Ricarico…`);
  534. setTimeout(() => window.location.reload(), 1200);
  535. } catch (err) {
  536. showToast(`Errore di rete: ${(err as Error).message}`, 'error');
  537. } finally {
  538. setRestoring(false);
  539. }
  540. };
  541. // Shared Input Classes for high contrast
  542. 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";
  543. return (
  544. <div className="min-h-screen bg-gray-50 font-sans pb-12">
  545. {/* Top Header */}
  546. <div className="bg-blue-900 text-white shadow-md py-6 px-4">
  547. <div className="max-w-5xl mx-auto flex justify-between items-center">
  548. <div>
  549. <h1 className="text-2xl font-bold">Captive Portal CMS</h1>
  550. <p className="text-sm text-blue-200">Local Administration</p>
  551. </div>
  552. <a href="/" target="_blank" className="bg-blue-800 hover:bg-blue-700 text-white px-4 py-2 rounded text-sm transition-colors">
  553. View Live Portal ↗
  554. </a>
  555. </div>
  556. </div>
  557. <div className="max-w-5xl mx-auto mt-8 px-4">
  558. {/* Tab Navigation */}
  559. <div className="flex space-x-2 mb-6">
  560. <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'}`}>
  561. Manage Cards
  562. </button>
  563. <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'}`}>
  564. Portal Settings
  565. </button>
  566. </div>
  567. <div className="bg-white rounded-b-xl rounded-tr-xl shadow-sm border border-gray-200 overflow-hidden min-h-[500px]">
  568. {/* TAB: CARDS */}
  569. {activeTab === 'cards' && (
  570. <div className="p-6 md:p-8">
  571. <div className="flex justify-between items-center mb-8 border-b pb-4">
  572. <h2 className="text-xl font-bold text-gray-800">Card Grid</h2>
  573. <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">
  574. + Add New Card
  575. </button>
  576. </div>
  577. <div className="space-y-3 mb-8">
  578. {cards.length === 0 && <p className="text-gray-500 italic text-center py-8">No cards available. Create one to get started.</p>}
  579. {cards.map((card, idx) => (
  580. // CHANGED: flex-col on mobile, flex-row on sm+, added gap-4 for mobile spacing
  581. <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">
  582. <div className="flex items-center gap-4">
  583. {(() => {
  584. const previewUrl = card.imageUrl || card.extraMedia?.[0]?.url || '';
  585. if (!previewUrl) {
  586. 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>;
  587. }
  588. return isVideoUrl(previewUrl)
  589. ? <video src={previewUrl} className="w-16 h-16 object-cover rounded-md shadow-sm shrink-0" muted playsInline preload="metadata" />
  590. : <img src={previewUrl} className="w-16 h-16 object-cover rounded-md shadow-sm shrink-0" alt="" />;
  591. })()}
  592. <div>
  593. <span className="font-semibold text-gray-800 block">{card.title}</span>
  594. <span className="text-xs text-gray-500 uppercase tracking-wider">
  595. {card.cardType}
  596. {card.extraMedia && card.extraMedia.length > 0 && (
  597. <span className="text-gray-400 normal-case tracking-normal ml-2">[{card.extraMedia.length}]</span>
  598. )}
  599. {card.cardType === 'FULLSCREEN_LOCK' && (
  600. <span className="ml-2 bg-red-100 text-red-700 px-2 py-0.5 rounded font-bold text-[10px] tracking-wider">LOCK ATTIVA</span>
  601. )}
  602. </span>
  603. </div>
  604. </div>
  605. {/* CHANGED: flex-wrap to ensure buttons don't overflow on small screens, w-full on mobile */}
  606. <div className="flex flex-wrap items-center gap-2 w-full sm:w-auto justify-end">
  607. <button onClick={() => moveCard(idx, 'up')} className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded" title="Move Up">↑</button>
  608. <button onClick={() => moveCard(idx, 'down')} className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded" title="Move Down">↓</button>
  609. <div className="w-px h-6 bg-gray-300 mx-1 hidden sm:block"></div>
  610. <button onClick={() => setIsEditing(card)} className="px-4 py-2 text-blue-600 hover:bg-blue-50 rounded font-medium">Edit</button>
  611. <button onClick={() => handleDeleteCard(card.id)} className="px-4 py-2 text-red-600 hover:bg-red-50 rounded font-medium">Delete</button>
  612. </div>
  613. </div>
  614. ))}
  615. </div>
  616. </div>
  617. )}
  618. {/* TAB: SETTINGS */}
  619. {activeTab === 'settings' && (
  620. <div className="p-6 md:p-8">
  621. <h2 className="text-xl font-bold text-gray-800 mb-8 border-b pb-4">Global Portal Settings</h2>
  622. <div className="grid grid-cols-1 md:grid-cols-2 gap-10">
  623. <div className="space-y-6">
  624. <div>
  625. <label className="block text-sm font-semibold text-gray-700 mb-1">Portal Title</label>
  626. <input type="text" maxLength={PORTAL_LIMITS.title} value={portal.title || ''} onChange={e => setPortal({...portal, title: e.target.value})} className={inputClasses} />
  627. <CharCounter value={portal.title} limit={PORTAL_LIMITS.title} />
  628. </div>
  629. <div>
  630. <label className="block text-sm font-semibold text-gray-700 mb-1">Welcome Text</label>
  631. <RichTextMini
  632. value={portal.welcomeText || ''}
  633. onChange={html => setPortal({ ...portal, welcomeText: html })}
  634. limit={PORTAL_LIMITS.welcomeText}
  635. />
  636. </div>
  637. <div className="flex gap-8">
  638. <div>
  639. <label className="block text-sm font-semibold text-gray-700 mb-1">Theme Color</label>
  640. <div className="flex items-center gap-4">
  641. <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" />
  642. <span className="text-gray-900 font-mono font-medium">{portal.themeColor || '#1e3a8a'}</span>
  643. </div>
  644. </div>
  645. {/* NEW: Max Columns Setting updated for 3 */}
  646. <div className="flex-1">
  647. <label className="block text-sm font-semibold text-gray-700 mb-1">Grid Max Columns: {portal.maxGridColumns || 5}</label>
  648. <input
  649. type="range"
  650. min="3"
  651. max="8"
  652. value={portal.maxGridColumns || 5}
  653. onChange={e => setPortal({...portal, maxGridColumns: parseInt(e.target.value)})}
  654. className="w-full mt-3 accent-blue-600"
  655. />
  656. <div className="flex justify-between text-xs text-gray-400 mt-1">
  657. <span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span>
  658. </div>
  659. </div>
  660. </div>
  661. <div>
  662. <label className="block text-sm font-semibold text-gray-700 mb-1">Font del portale</label>
  663. <style dangerouslySetInnerHTML={{ __html: availableFonts.map(f => `
  664. @font-face {
  665. font-family: '${previewFontFamily(f)}';
  666. src: url('/api/fonts?name=${encodeURIComponent(f)}') format('${fontFormatFromName(f)}');
  667. font-display: swap;
  668. }`).join('') }} />
  669. <StyledSelect<string>
  670. value={portal.fontFamily ?? ''}
  671. onChange={(v) => setPortal({ ...portal, fontFamily: v })}
  672. options={[
  673. { value: '', label: 'Sistema (Arial)' },
  674. ...availableFonts.map(f => ({
  675. value: f,
  676. label: f.replace(/\.(woff2?|ttf|otf)$/i, ''),
  677. style: { fontFamily: `'${previewFontFamily(f)}', Arial, Helvetica, sans-serif` },
  678. })),
  679. ]}
  680. />
  681. </div>
  682. </div>
  683. <div className="space-y-6">
  684. {/* Logo Upload with Remove Button */}
  685. <div>
  686. <label className="block text-sm font-semibold text-gray-700 mb-1">Logo Image</label>
  687. <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" />
  688. {uploading['logoUrl'] && <span className="text-xs text-blue-500">Uploading...</span>}
  689. {portal.logoUrl && (
  690. <div className="mt-2 bg-gray-100 p-4 rounded inline-block relative border">
  691. <img src={portal.logoUrl} className="h-16 object-contain" alt="Logo Preview" />
  692. <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>
  693. <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>
  694. </div>
  695. )}
  696. </div>
  697. {/* Hero Upload with Remove Button */}
  698. <div>
  699. <label className="block text-sm font-semibold text-gray-700 mb-1">Hero Background Image</label>
  700. <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" />
  701. {uploading['heroImageUrl'] && <span className="text-xs text-blue-500">Uploading...</span>}
  702. {portal.heroImageUrl && (
  703. <div className="mt-2 relative rounded shadow border inline-block w-full">
  704. <img src={portal.heroImageUrl} className="h-32 w-full object-cover rounded" alt="Hero Preview" />
  705. <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>
  706. <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>
  707. </div>
  708. )}
  709. </div>
  710. <div className="bg-gray-50 p-4 rounded-lg border border-gray-200 space-y-3">
  711. <label className="flex items-center gap-3 cursor-pointer">
  712. <input type="checkbox" checked={!!portal.fadeHeroImage} onChange={e => setPortal({...portal, fadeHeroImage: e.target.checked})} className="w-5 h-5 text-blue-600 rounded" />
  713. <div>
  714. <span className="block text-sm font-semibold text-gray-900">Fade Image into Background Color</span>
  715. <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>
  716. </div>
  717. </label>
  718. <label className="flex items-center gap-3 cursor-pointer">
  719. <input
  720. type="checkbox"
  721. checked={portal.externalLinkEnabled ?? EXTERNAL_LINK_DEFAULT}
  722. onChange={e => setPortal({...portal, externalLinkEnabled: e.target.checked})}
  723. className="w-5 h-5 text-blue-600 rounded"
  724. />
  725. <div>
  726. <span className="block text-sm font-semibold text-gray-900">Abilita &ldquo;External Link&rdquo;</span>
  727. <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>
  728. </div>
  729. </label>
  730. </div>
  731. </div>
  732. </div>
  733. <div className="mt-10 pt-6 border-t border-gray-200">
  734. <h3 className="text-sm font-bold uppercase tracking-wider text-gray-600 mb-3">Backup &amp; Restore</h3>
  735. <p className="text-xs text-gray-500 mb-4">
  736. Il backup contiene card, configurazione portale, media (immagini, video, PDF) e font caricati. Il ripristino sovrascrive lo stato attuale; la cartella precedente viene conservata come <code>data.bak-&lt;timestamp&gt;</code> per sicurezza.
  737. </p>
  738. <div className="flex flex-wrap gap-3">
  739. <button
  740. type="button"
  741. onClick={handleBackupDownload}
  742. className="bg-gray-800 text-white px-5 py-2.5 rounded-lg hover:bg-gray-900 font-medium shadow-sm"
  743. >
  744. ⬇ Scarica backup ZIP
  745. </button>
  746. <label className={`cursor-pointer inline-flex items-center bg-amber-600 text-white px-5 py-2.5 rounded-lg hover:bg-amber-700 font-medium shadow-sm ${restoring ? 'opacity-60 cursor-not-allowed' : ''}`}>
  747. <input
  748. type="file"
  749. accept=".zip,application/zip,application/x-zip-compressed"
  750. onChange={handleRestoreUpload}
  751. disabled={restoring}
  752. hidden
  753. />
  754. {restoring ? 'Ripristino in corso…' : '⤴ Ripristina da ZIP…'}
  755. </label>
  756. </div>
  757. </div>
  758. <div className="mt-10 pt-6 border-t border-gray-200 flex justify-end">
  759. <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">
  760. {savingPortal ? 'Saving...' : 'Save Portal Settings'}
  761. </button>
  762. </div>
  763. </div>
  764. )}
  765. </div>
  766. </div>
  767. {/* MODAL FOR EDITING/CREATING CARDS */}
  768. {isEditing && (
  769. <div
  770. className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm p-4 transition-opacity"
  771. onClick={() => setIsEditing(null)} // Click outside to close
  772. >
  773. <div
  774. 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"
  775. onClick={(e) => e.stopPropagation()} // Prevent inside clicks from closing
  776. >
  777. <button
  778. onClick={() => setIsEditing(null)}
  779. 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"
  780. >
  781. </button>
  782. <h3 className="text-2xl font-bold mb-6 text-gray-900 border-b pb-4">
  783. {isEditing.id ? 'Edit Card' : 'Create New Card'}
  784. </h3>
  785. <div className="grid grid-cols-1 md:grid-cols-2 gap-8">
  786. <div className="space-y-5">
  787. <div>
  788. <label className="block text-sm font-semibold text-gray-800 mb-1">Title</label>
  789. <input type="text" maxLength={CARD_LIMITS.title} value={isEditing.title || ''} onChange={e => setIsEditing({...isEditing, title: e.target.value})} className={inputClasses} placeholder="e.g., Local History" />
  790. <CharCounter value={isEditing.title} limit={CARD_LIMITS.title} />
  791. </div>
  792. <div>
  793. <label className="block text-sm font-semibold text-gray-800 mb-1">Card Type</label>
  794. <StyledSelect<CardType>
  795. value={(isEditing.cardType || 'INFO_PAGE') as CardType}
  796. onChange={(v) => setIsEditing({ ...isEditing, cardType: v })}
  797. options={[
  798. { value: 'INFO_PAGE', label: 'Info Page' },
  799. { value: 'IMAGE_GALLERY', label: 'Image Gallery' },
  800. { value: 'BOOK', label: 'Flip-Book' },
  801. { value: 'FULLSCREEN_LOCK', label: 'Fullscreen Lock (kiosk)' },
  802. ...(externalLinksOn ? [{ value: 'EXTERNAL_LINK' as CardType, label: 'External Link' }] : []),
  803. ]}
  804. />
  805. </div>
  806. {isEditing.cardType === 'FULLSCREEN_LOCK' && (
  807. <div className="bg-red-50 border border-red-200 rounded-lg p-4">
  808. <p className="text-sm font-semibold text-red-800">⚠ Modalità Kiosk Lock</p>
  809. <p className="text-xs text-red-700 mt-1">
  810. Questa card prenderà il controllo totale del portale pubblico. Tutte le altre card saranno nascoste finché non rimuovi questa.
  811. Carica un&apos;immagine o un video come &quot;Contenuto a schermo intero&quot; nella sezione a destra.
  812. </p>
  813. </div>
  814. )}
  815. {isEditing.cardType !== 'FULLSCREEN_LOCK' && (isEditing.cardType === 'EXTERNAL_LINK' ? (
  816. <>
  817. <div>
  818. <label className="block text-sm font-semibold text-gray-800 mb-1">URL</label>
  819. <input
  820. type="url"
  821. maxLength={CARD_LIMITS.actionUrl}
  822. value={isEditing.actionUrl || ''}
  823. onChange={e => setIsEditing({ ...isEditing, actionUrl: e.target.value })}
  824. className={inputClasses}
  825. placeholder="https://esempio.it/pagina"
  826. />
  827. <CharCounter value={isEditing.actionUrl} limit={CARD_LIMITS.actionUrl} />
  828. </div>
  829. <div>
  830. <label className="block text-sm font-semibold text-gray-800 mb-1">Testo del link</label>
  831. <input
  832. type="text"
  833. maxLength={CARD_LIMITS.shortDescription}
  834. value={isEditing.shortDescription || ''}
  835. onChange={e => setIsEditing({ ...isEditing, shortDescription: e.target.value })}
  836. className={inputClasses}
  837. placeholder="es. Visita il sito ufficiale"
  838. />
  839. <CharCounter value={isEditing.shortDescription} limit={CARD_LIMITS.shortDescription} />
  840. <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>
  841. </div>
  842. <div className="bg-gray-50 p-3 rounded-lg border border-gray-200">
  843. <label className="flex items-start gap-3 cursor-pointer">
  844. <input
  845. type="checkbox"
  846. checked={!!isEditing.redirectOnClick}
  847. onChange={e => setIsEditing({ ...isEditing, redirectOnClick: e.target.checked })}
  848. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  849. />
  850. <div>
  851. <span className="block text-sm font-semibold text-gray-900">Redirect on click</span>
  852. <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>
  853. </div>
  854. </label>
  855. </div>
  856. </>
  857. ) : (
  858. <div>
  859. <label className="block text-sm font-semibold text-gray-800 mb-1">Short Description</label>
  860. <textarea maxLength={CARD_LIMITS.shortDescription} value={isEditing.shortDescription || ''} onChange={e => setIsEditing({ ...isEditing, shortDescription: e.target.value })} className={`${inputClasses} h-24 resize-none`} placeholder="Brief summary..." />
  861. <CharCounter value={isEditing.shortDescription} limit={CARD_LIMITS.shortDescription} />
  862. </div>
  863. ))}
  864. {isEditing.cardType !== 'BOOK' && isEditing.cardType !== 'FULLSCREEN_LOCK' && (
  865. <div className="bg-gray-50 p-3 rounded-lg border border-gray-200 space-y-3">
  866. <label className="flex items-start gap-3 cursor-pointer">
  867. <input
  868. type="checkbox"
  869. checked={!!isEditing.autoFullscreen}
  870. onChange={e => setIsEditing({ ...isEditing, autoFullscreen: e.target.checked })}
  871. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  872. />
  873. <div>
  874. <span className="block text-sm font-semibold text-gray-900">Auto fullscreen</span>
  875. <span className="block text-xs text-gray-600">Open the gallery in fullscreen immediately when the user clicks this card.</span>
  876. </div>
  877. </label>
  878. <label className="flex items-start gap-3 cursor-pointer">
  879. <input
  880. type="checkbox"
  881. checked={!!isEditing.skipPreview}
  882. onChange={e => setIsEditing({ ...isEditing, skipPreview: e.target.checked })}
  883. className="w-5 h-5 text-blue-600 rounded mt-0.5"
  884. />
  885. <div>
  886. <span className="block text-sm font-semibold text-gray-900">Cover not in the gallery</span>
  887. <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>
  888. </div>
  889. </label>
  890. </div>
  891. )}
  892. </div>
  893. <div className="space-y-5">
  894. {/* Cover Image — per FULLSCREEN_LOCK è il contenuto kiosk a tutto schermo e accetta anche video */}
  895. <div>
  896. <label className="block text-sm font-semibold text-gray-800 mb-1">
  897. {isEditing.cardType === 'FULLSCREEN_LOCK'
  898. ? <>Contenuto a schermo intero <span className="text-gray-400 font-normal text-xs">(immagine o video)</span></>
  899. : <>Cover Image <span className="text-gray-400 font-normal text-xs">(shown in grid)</span></>}
  900. </label>
  901. <div className="border-2 border-dashed border-gray-300 rounded-lg p-3 hover:bg-gray-50 transition-colors">
  902. <input
  903. type="file"
  904. accept={isEditing.cardType === 'FULLSCREEN_LOCK' ? 'image/*,video/mp4,video/webm,.mp4,.webm,.mov,.m4v' : 'image/*'}
  905. onChange={e => handleUpload(e, 'imageUrl')}
  906. 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"
  907. />
  908. {uploading['imageUrl'] && <p className="mt-2 text-sm text-blue-600 font-medium">Uploading...</p>}
  909. </div>
  910. {isEditing.imageUrl && (
  911. <div className="mt-3 relative rounded-lg overflow-hidden border border-gray-200 group">
  912. {isVideoUrl(isEditing.imageUrl) ? (
  913. <video src={isEditing.imageUrl} className="w-full h-32 object-cover" muted playsInline />
  914. ) : (
  915. <img src={isEditing.imageUrl} className="w-full h-32 object-cover" alt="Cover preview" />
  916. )}
  917. <a
  918. href={isEditing.imageUrl}
  919. download={extractFileName(isEditing.imageUrl)}
  920. 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"
  921. title="Scarica"
  922. aria-label="Scarica cover"
  923. >⬇</a>
  924. <button
  925. onClick={() => setIsEditing({...isEditing, imageUrl: ''})}
  926. 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"
  927. title="Remove cover image"
  928. >✕</button>
  929. </div>
  930. )}
  931. </div>
  932. {/* Gallery Media (images + videos + PDFs) — nascosta per INFO_PAGE (solo cover ammessa) e FULLSCREEN_LOCK (solo contenuto kiosk) */}
  933. {isEditing.cardType !== 'INFO_PAGE' && isEditing.cardType !== 'FULLSCREEN_LOCK' && (
  934. <div>
  935. <label className="block text-sm font-semibold text-gray-800 mb-1">
  936. Gallery Media <span className="text-gray-400 font-normal text-xs">(images, videos or PDFs — PDF pages become images)</span>
  937. </label>
  938. <div className="border-2 border-dashed border-gray-300 rounded-lg p-3 hover:bg-gray-50 transition-colors">
  939. <input
  940. type="file"
  941. 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"
  942. multiple
  943. onChange={handleUploadExtraMedia}
  944. 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"
  945. />
  946. {uploading['extraMedia'] && !pdfProgress && <p className="mt-2 text-sm text-purple-600 font-medium">Uploading...</p>}
  947. {pdfProgress && (
  948. <p className="mt-2 text-sm text-purple-600 font-medium">
  949. Processing &ldquo;{pdfProgress.name}&rdquo;: page {pdfProgress.page} of {pdfProgress.total}
  950. </p>
  951. )}
  952. </div>
  953. {(isEditing.extraMedia || []).length > 0 && (
  954. <div className="mt-3 space-y-2">
  955. {(isEditing.extraMedia || []).map((item, i) => {
  956. const video = isVideoUrl(item.url);
  957. const tc = transcodeJobs[item.url];
  958. const isTranscoding = !!tc && (tc.status === 'queued' || tc.status === 'running');
  959. return (
  960. <div key={item.url + i} className="flex items-center gap-3 p-2 bg-gray-50 border border-gray-200 rounded-lg">
  961. <div className="relative w-16 h-16 rounded-md overflow-hidden bg-black shrink-0">
  962. {video ? (
  963. <>
  964. <video src={item.url} className="w-full h-full object-cover" muted preload="metadata" />
  965. <div className="absolute inset-0 flex items-center justify-center bg-black/30 text-white text-xl">▶</div>
  966. </>
  967. ) : (
  968. <img src={item.url} className="w-full h-full object-cover" alt="" />
  969. )}
  970. {isTranscoding && (
  971. <div className="absolute inset-0 flex flex-col items-center justify-center bg-black/75 text-white text-[10px] font-semibold leading-tight gap-0.5">
  972. <span>Transcoding</span>
  973. <span>{Math.round((tc.progress || 0) * 100)}%</span>
  974. </div>
  975. )}
  976. <span className="absolute bottom-0 left-0 right-0 text-center text-white text-[10px] bg-black/60">{i + 1}</span>
  977. </div>
  978. <div className="flex-1 min-w-0">
  979. <div className="text-xs font-semibold text-gray-700 uppercase tracking-wider">
  980. {video ? 'Video' : 'Image'}
  981. </div>
  982. {video && (
  983. <div className="mt-1 flex flex-wrap gap-x-4 gap-y-1">
  984. <label className="flex items-center gap-2 cursor-pointer">
  985. <input
  986. type="checkbox"
  987. checked={!!item.autoplay}
  988. onChange={() => toggleAutoplay(i)}
  989. className="w-4 h-4 text-blue-600 rounded"
  990. />
  991. <span className="text-sm text-gray-700">Autoplay</span>
  992. </label>
  993. <label className="flex items-center gap-2 cursor-pointer">
  994. <input
  995. type="checkbox"
  996. checked={!!item.muted}
  997. onChange={() => toggleMuted(i)}
  998. className="w-4 h-4 text-blue-600 rounded"
  999. />
  1000. <span className="text-sm text-gray-700">Muted</span>
  1001. </label>
  1002. </div>
  1003. )}
  1004. </div>
  1005. <button
  1006. onClick={() => moveExtraMedia(i, 'up')}
  1007. className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded shrink-0 disabled:opacity-40 disabled:cursor-not-allowed focus:outline-none focus-visible:outline-none"
  1008. title="Sposta su"
  1009. aria-label="Sposta su"
  1010. disabled={i === 0}
  1011. >↑</button>
  1012. <button
  1013. onClick={() => moveExtraMedia(i, 'down')}
  1014. className="p-2 text-gray-500 hover:text-gray-800 hover:bg-gray-200 rounded shrink-0 disabled:opacity-40 disabled:cursor-not-allowed focus:outline-none focus-visible:outline-none"
  1015. title="Sposta giù"
  1016. aria-label="Sposta giù"
  1017. disabled={i === (isEditing.extraMedia || []).length - 1}
  1018. >↓</button>
  1019. <a
  1020. href={item.url}
  1021. download={extractFileName(item.url)}
  1022. 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"
  1023. title="Scarica"
  1024. aria-label="Scarica"
  1025. >⬇</a>
  1026. <button
  1027. onClick={() => removeExtraMedia(i)}
  1028. className="bg-red-500 hover:bg-red-600 text-white w-8 h-8 rounded-full text-sm font-bold shrink-0"
  1029. title="Remove"
  1030. >✕</button>
  1031. </div>
  1032. );
  1033. })}
  1034. </div>
  1035. )}
  1036. </div>
  1037. )}
  1038. </div>
  1039. </div>
  1040. <div className="flex gap-3 pt-8 mt-6 border-t border-gray-200 justify-end">
  1041. <button onClick={() => setIsEditing(null)} className="px-5 py-2.5 text-gray-700 hover:bg-gray-100 rounded-lg font-medium transition-colors">
  1042. Cancel
  1043. </button>
  1044. <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">
  1045. Save Card
  1046. </button>
  1047. </div>
  1048. </div>
  1049. </div>
  1050. )}
  1051. {/* CUSTOM CONFIRM DIALOG */}
  1052. {confirmDialog && (
  1053. <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">
  1054. <div className="bg-white rounded-xl shadow-2xl p-6 max-w-sm w-full animate-in zoom-in-95">
  1055. <h3 className="text-xl font-bold text-gray-900 mb-2">Confirm Action</h3>
  1056. <p className="text-gray-600 mb-6 leading-relaxed">{confirmDialog.message}</p>
  1057. <div className="flex justify-end gap-3">
  1058. <button
  1059. onClick={() => setConfirmDialog(null)}
  1060. className="px-4 py-2.5 text-gray-700 hover:bg-gray-100 rounded-lg font-medium transition-colors"
  1061. >
  1062. Cancel
  1063. </button>
  1064. <button
  1065. onClick={confirmDialog.onConfirm}
  1066. className="px-6 py-2.5 bg-red-600 text-white rounded-lg hover:bg-red-700 font-medium transition-colors shadow-sm"
  1067. >
  1068. Delete
  1069. </button>
  1070. </div>
  1071. </div>
  1072. </div>
  1073. )}
  1074. {/* CUSTOM TOAST NOTIFICATION */}
  1075. {toast && (
  1076. <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 ${
  1077. toast.type === 'error' ? 'bg-red-700' : 'bg-gray-900'
  1078. }`}>
  1079. <div className={`w-6 h-6 rounded-full flex items-center justify-center font-bold text-sm shrink-0 mt-0.5 ${
  1080. toast.type === 'error' ? 'bg-white text-red-700' : 'bg-green-500 text-gray-900'
  1081. }`}>
  1082. {toast.type === 'error' ? '!' : '✓'}
  1083. </div>
  1084. <span className="font-medium leading-snug">{toast.message}</span>
  1085. </div>
  1086. )}
  1087. </div>
  1088. );
  1089. }