-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathAssetLibrary.tsx
More file actions
179 lines (158 loc) · 5.44 KB
/
Copy pathAssetLibrary.tsx
File metadata and controls
179 lines (158 loc) · 5.44 KB
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
'use client';
import React, { useEffect, useState } from 'react';
import Image from 'next/image';
import { assetsApi } from '../lib/api';
import { formatFileSize, isAssetOfType, ASSET_CATEGORIES } from '@/lib/asset-utils';
import type { Asset } from '../types';
import AssetUpload from './AssetUpload';
interface AssetLibraryProps {
onAssetSelect?: (asset: Asset) => void;
className?: string;
accept?: string;
maxSize?: number;
}
export default function AssetLibrary({
onAssetSelect,
className = '',
accept = '*/*',
maxSize = 50,
}: AssetLibraryProps) {
const [assets, setAssets] = useState<Asset[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
loadAssets();
}, []);
const loadAssets = async () => {
try {
setLoading(true);
setError(null);
const response = await assetsApi.getAll();
if (response.error) {
throw new Error(response.error);
}
setAssets(response.data || []);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load assets');
} finally {
setLoading(false);
}
};
const handleUploadSuccess = (asset: Asset) => {
setAssets((prev) => [asset, ...prev]);
};
const handleUploadError = (error: string) => {
setError(error);
};
const handleDelete = async (asset: Asset) => {
if (!confirm('Are you sure you want to delete this asset?')) {
return;
}
try {
const response = await assetsApi.delete(asset.id);
if (response.error) {
throw new Error(response.error);
}
setAssets((prev) => prev.filter((a) => a.id !== asset.id));
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to delete asset');
}
};
if (loading) {
return (
<div className={`p-4 ${className}`}>
<div className="flex items-center justify-center h-32">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
</div>
</div>
);
}
return (
<div className={`p-4 ${className}`}>
<div className="mb-4">
<h3 className="text-lg font-medium text-gray-900 mb-2">Asset Library</h3>
<AssetUpload
onUploadSuccess={handleUploadSuccess}
onUploadError={handleUploadError}
accept={accept}
maxSize={maxSize}
/>
</div>
{error && (
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded text-red-700 text-sm">
{error}
</div>
)}
<div className="grid grid-cols-2 gap-4">
{assets.map((asset) => (
<div
key={asset.id}
className="group relative bg-white border border-gray-200 rounded-lg overflow-hidden hover:shadow-md transition-shadow"
>
<div className="aspect-square bg-gray-100 flex items-center justify-center relative">
{isAssetOfType(asset.mime_type, ASSET_CATEGORIES.IMAGES) && asset.public_url ? (
<Image
src={asset.public_url}
alt={asset.filename}
fill
className="object-cover"
sizes="(max-width: 768px) 50vw, 33vw"
/>
) : (
<div className="text-gray-400">
<svg
className="w-8 h-8" fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z"
clipRule="evenodd"
/>
</svg>
</div>
)}
</div>
<div className="p-2">
<p className="text-xs text-gray-600 truncate" title={asset.filename}>
{asset.filename}
</p>
<p className="text-xs text-gray-500">
{asset.width && asset.height ? `${asset.width}×${asset.height}` : ''}
{asset.file_size && (
<span className="ml-1">
({formatFileSize(asset.file_size)})
</span>
)}
</p>
</div>
<div className="absolute inset-0 bg-black bg-opacity-0 group-hover:bg-opacity-20 transition-all flex items-center justify-center opacity-0 group-hover:opacity-100">
<div className="flex gap-2">
{onAssetSelect && (
<button
onClick={() => onAssetSelect(asset)}
className="px-3 py-1 bg-blue-600 text-white text-xs rounded hover:bg-blue-700"
>
Use
</button>
)}
<button
onClick={() => handleDelete(asset)}
className="px-3 py-1 bg-red-600 text-white text-xs rounded hover:bg-red-700"
>
Delete
</button>
</div>
</div>
</div>
))}
</div>
{assets.length === 0 && (
<div className="text-center py-8 text-gray-500">
<p>No assets uploaded yet.</p>
<p className="text-sm">Upload your first asset above.</p>
</div>
)}
</div>
);
}