|
1 | | -import React, { useState } from 'react'; |
2 | | -import type { MediaItem } from '../types'; |
3 | | - |
4 | | -interface MediaPanelProps { |
5 | | - media: MediaItem[]; |
6 | | - activeMediaId?: string; |
7 | | - onSelect: (id?: string) => void; |
8 | | - onRemove: (id: string) => void; |
9 | | -} |
10 | | - |
11 | | -export const MediaPanel: React.FC<MediaPanelProps> = ({ media, activeMediaId, onSelect, onRemove }) => { |
12 | | - const [focusedId, setFocusedId] = useState<string | null>(activeMediaId || null); |
13 | | - |
14 | | - const handleOpen = (id: string) => { |
15 | | - setFocusedId(id); |
16 | | - onSelect(id); |
17 | | - }; |
18 | | - |
19 | | - const handleClose = () => { |
20 | | - setFocusedId(null); |
21 | | - onSelect(undefined); |
22 | | - }; |
23 | | - |
24 | | - return ( |
25 | | - <section className="bg-gray-800 p-4 rounded-lg shadow-lg mb-6"> |
26 | | - <h3 className="text-white font-semibold mb-3">Media</h3> |
27 | | - {media.length === 0 ? ( |
28 | | - <div className="text-gray-400">No media added yet.</div> |
29 | | - ) : ( |
30 | | - <div className="grid grid-cols-4 gap-3"> |
31 | | - {media.map(item => ( |
32 | | - <div key={item.id} className="relative bg-gray-900 rounded overflow-hidden"> |
33 | | - {item.kind === 'video' ? ( |
34 | | - <video src={item.url} className="w-full h-24 object-cover" muted loop={!!item.loop} playsInline /> |
35 | | - ) : ( |
36 | | - <img src={item.url} alt={item.fileName || 'media'} className="w-full h-24 object-cover" /> |
37 | | - )} |
38 | | - |
39 | | - <div className="absolute right-1 top-1 flex gap-1"> |
40 | | - <button onClick={() => handleOpen(item.id)} className="bg-black/50 text-white px-2 py-1 rounded text-xs">View</button> |
41 | | - <button onClick={() => onRemove(item.id)} className="bg-red-600 text-white px-2 py-1 rounded text-xs">Remove</button> |
42 | | - </div> |
43 | | - </div> |
44 | | - ))} |
45 | | - </div> |
46 | | - )} |
47 | | - |
48 | | - {focusedId && ( |
49 | | - <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70" onClick={handleClose}> |
50 | | - <div className="bg-black rounded-lg p-4 max-w-5xl max-h-[80vh] w-full" onClick={(e) => e.stopPropagation()}> |
51 | | - <div className="flex justify-between items-start mb-2"> |
52 | | - <button onClick={handleClose} className="text-gray-300">Close</button> |
53 | | - </div> |
54 | | - <div className="flex items-center justify-center"> |
55 | | - {(() => { |
56 | | - const item = media.find(m => m.id === focusedId); |
57 | | - if (!item) return null; |
58 | | - if (item.kind === 'video') { |
59 | | - return ( |
60 | | - <video src={item.url} controls autoPlay muted={!!item.muted} loop={!!item.loop} className="max-h-[70vh] w-auto max-w-full" /> |
61 | | - ); |
62 | | - } |
63 | | - // images & gifs |
64 | | - return <img src={item.url} alt={item.fileName || 'media'} className="max-h-[70vh] w-auto max-w-full" />; |
65 | | - })()} |
66 | | - </div> |
67 | | - </div> |
68 | | - </div> |
69 | | - )} |
70 | | - </section> |
71 | | - ); |
72 | | -}; |
73 | | - |
| 1 | +import React, { useState, useMemo } from 'react'; |
| 2 | +import type { MediaItem } from '../types'; |
| 3 | + |
| 4 | +interface MediaPanelProps { |
| 5 | + media: MediaItem[]; |
| 6 | + activeMediaId?: string; |
| 7 | + onSelect: (id?: string) => void; |
| 8 | + onRemove: (id: string) => void; |
| 9 | +} |
| 10 | + |
| 11 | +export const MediaPanel: React.FC<MediaPanelProps> = ({ media, activeMediaId, onSelect, onRemove }) => { |
| 12 | + const [focusedId, setFocusedId] = useState<string | null>(activeMediaId || null); |
| 13 | + |
| 14 | + const mediaMap = useMemo(() => new Map(media.map(item => [item.id, item])), [media]); |
| 15 | + |
| 16 | + const handleOpen = (id: string) => { |
| 17 | + setFocusedId(id); |
| 18 | + onSelect(id); |
| 19 | + }; |
| 20 | + |
| 21 | + const handleClose = () => { |
| 22 | + setFocusedId(null); |
| 23 | + onSelect(undefined); |
| 24 | + }; |
| 25 | + |
| 26 | + return ( |
| 27 | + <section className="bg-gray-800 p-4 rounded-lg shadow-lg mb-6"> |
| 28 | + <h3 className="text-white font-semibold mb-3">Media</h3> |
| 29 | + {media.length === 0 ? ( |
| 30 | + <div className="text-gray-400">No media added yet.</div> |
| 31 | + ) : ( |
| 32 | + <div className="grid grid-cols-4 gap-3"> |
| 33 | + {media.map(item => ( |
| 34 | + <div key={item.id} className="relative bg-gray-900 rounded overflow-hidden"> |
| 35 | + {item.kind === 'video' ? ( |
| 36 | + <video src={item.url} className="w-full h-24 object-cover" muted loop={!!item.loop} playsInline /> |
| 37 | + ) : ( |
| 38 | + <img src={item.url} alt={item.fileName || 'media'} className="w-full h-24 object-cover" /> |
| 39 | + )} |
| 40 | + |
| 41 | + <div className="absolute right-1 top-1 flex gap-1"> |
| 42 | + <button onClick={() => handleOpen(item.id)} className="bg-black/50 text-white px-2 py-1 rounded text-xs">View</button> |
| 43 | + <button onClick={() => onRemove(item.id)} className="bg-red-600 text-white px-2 py-1 rounded text-xs">Remove</button> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + ))} |
| 47 | + </div> |
| 48 | + )} |
| 49 | + |
| 50 | + {focusedId && ( |
| 51 | + <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70" onClick={handleClose}> |
| 52 | + <div className="bg-black rounded-lg p-4 max-w-5xl max-h-[80vh] w-full" onClick={(e) => e.stopPropagation()}> |
| 53 | + <div className="flex justify-between items-start mb-2"> |
| 54 | + <button onClick={handleClose} className="text-gray-300">Close</button> |
| 55 | + </div> |
| 56 | + <div className="flex items-center justify-center"> |
| 57 | + {(() => { |
| 58 | + const item = mediaMap.get(focusedId); |
| 59 | + if (!item) return null; |
| 60 | + if (item.kind === 'video') { |
| 61 | + return ( |
| 62 | + <video src={item.url} controls autoPlay muted={!!item.muted} loop={!!item.loop} className="max-h-[70vh] w-auto max-w-full" /> |
| 63 | + ); |
| 64 | + } |
| 65 | + // images & gifs |
| 66 | + return <img src={item.url} alt={item.fileName || 'media'} className="max-h-[70vh] w-auto max-w-full" />; |
| 67 | + })()} |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + )} |
| 72 | + </section> |
| 73 | + ); |
| 74 | +}; |
0 commit comments