1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
"use client";
import { Button } from "../ui/button";
import { AspectRatio } from "../ui/aspect-ratio";
import { DragOverlay } from "../ui/drag-overlay";
import { useMediaStore } from "@/stores/media-store";
import { processMediaFiles } from "@/lib/media-processing";
import { Plus, Image, Video, Music, Trash2, Upload } from "lucide-react";
import { useDragDrop } from "@/hooks/use-drag-drop";
import { useRef, useState } from "react";
import { toast } from "sonner";
export function MediaPanel() {
const { mediaItems, addMediaItem, removeMediaItem } = useMediaStore();
const fileInputRef = useRef<HTMLInputElement>(null);
const [isProcessing, setIsProcessing] = useState(false);
const processFiles = async (files: FileList | File[]) => {
if (!files?.length) return;
setIsProcessing(true);
try {
const items = await processMediaFiles(files);
items.forEach(item => {
addMediaItem(item);
});
} catch (error) {
console.error("File processing failed:", error);
toast.error("Failed to process files");
} finally {
setIsProcessing(false);
}
};
const { isDragOver, dragProps } = useDragDrop({
onDrop: processFiles,
});
const handleFileSelect = () => fileInputRef.current?.click();
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files) processFiles(e.target.files);
e.target.value = "";
};
const handleRemove = (e: React.MouseEvent, id: string) => {
e.stopPropagation();
removeMediaItem(id);
};
const formatDuration = (duration: number) => {
const min = Math.floor(duration / 60);
const sec = Math.floor(duration % 60);
return `${min}:${sec.toString().padStart(2, "0")}`;
};
const startDrag = (e: React.DragEvent, item: any) => {
e.dataTransfer.setData("application/x-media-item", JSON.stringify({
id: item.id,
type: item.type,
name: item.name,
}));
e.dataTransfer.effectAllowed = "copy";
};
const renderPreview = (item: any) => {
const baseDragProps = {
draggable: true,
onDragStart: (e: React.DragEvent) => startDrag(e, item),
};
if (item.type === "image") {
return (
<img
src={item.url}
alt={item.name}
className="w-full h-full object-cover rounded cursor-grab active:cursor-grabbing"
loading="lazy"
{...baseDragProps}
/>
);
}
if (item.type === "video") {
if (item.thumbnailUrl) {
return (
<div className="relative w-full h-full cursor-grab active:cursor-grabbing" {...baseDragProps}>
<img
src={item.thumbnailUrl}
alt={item.name}
className="w-full h-full object-cover rounded"
loading="lazy"
/>
<div className="absolute inset-0 flex items-center justify-center bg-black/20 rounded">
<Video className="h-6 w-6 text-white drop-shadow-md" />
</div>
{item.duration && (
<div className="absolute bottom-1 right-1 bg-black/70 text-white text-xs px-1 rounded">
{formatDuration(item.duration)}
</div>
)}
</div>
);
}
return (
<div className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing" {...baseDragProps}>
<Video className="h-6 w-6 mb-1" />
<span className="text-xs">Video</span>
{item.duration && (
<span className="text-xs opacity-70">{formatDuration(item.duration)}</span>
)}
</div>
);
}
if (item.type === "audio") {
return (
<div className="w-full h-full bg-gradient-to-br from-green-500/20 to-emerald-500/20 flex flex-col items-center justify-center text-muted-foreground rounded border border-green-500/20 cursor-grab active:cursor-grabbing" {...baseDragProps}>
<Music className="h-6 w-6 mb-1" />
<span className="text-xs">Audio</span>
{item.duration && (
<span className="text-xs opacity-70">{formatDuration(item.duration)}</span>
)}
</div>
);
}
return (
<div className="w-full h-full bg-muted/30 flex flex-col items-center justify-center text-muted-foreground rounded cursor-grab active:cursor-grabbing" {...baseDragProps}>
<Image className="h-6 w-6" />
<span className="text-xs mt-1">Unknown</span>
</div>
);
};
return (
<>
<input
ref={fileInputRef}
type="file"
accept="image/*,video/*,audio/*"
multiple
className="hidden"
onChange={handleFileChange}
/>
<div
className={`h-full flex flex-col transition-colors relative ${isDragOver ? "bg-accent/30" : ""
}`}
{...dragProps}
>
<DragOverlay isVisible={isDragOver} />
<div className="p-2 border-b">
<Button
variant="outline"
size="sm"
className="w-full"
onClick={handleFileSelect}
disabled={isProcessing}
>
{isProcessing ? (
<>
<Upload className="h-4 w-4 mr-2 animate-spin" />
Processing...
</>
) : (
<>
<Plus className="h-4 w-4 mr-2" />
Add Media
</>
)}
</Button>
</div>
<div className="flex-1 overflow-y-auto p-2">
{mediaItems.length === 0 ? (
<div className="flex flex-col items-center justify-center py-8 text-center h-full">
<div className="w-16 h-16 rounded-full bg-muted/30 flex items-center justify-center mb-4">
<Image className="h-8 w-8 text-muted-foreground" />
</div>
<p className="text-sm text-muted-foreground">No media in project</p>
<p className="text-xs text-muted-foreground/70 mt-1">
Drag files here or use the button above
</p>
</div>
) : (
<div className="grid grid-cols-2 gap-2">
{mediaItems.map((item) => (
<div key={item.id} className="relative group">
<Button
variant="outline"
className="flex flex-col gap-2 p-2 h-auto w-full relative"
>
<AspectRatio ratio={item.aspectRatio}>
{renderPreview(item)}
</AspectRatio>
<span className="text-xs truncate px-1">
{item.name}
</span>
</Button>
<div className="absolute -top-2 -right-2 opacity-0 group-hover:opacity-100 transition-opacity">
<Button
variant="destructive"
size="icon"
className="h-6 w-6"
onClick={(e) => handleRemove(e, item.id)}
>
<Trash2 className="h-3 w-3" />
</Button>
</div>
</div>
))}
</div>
)}
</div>
</div>
</>
);
}
|