11import { useState , useMemo , useEffect } from "react" ;
22import { useAssets , Asset } from "@/hooks/use-assets" ;
33import { UploadZone } from "./upload-zone" ;
4+ import { convertImageToWebP } from "@/lib/image-utils" ;
45import { Button } from "@/components/ui/button" ;
56import { Input } from "@/components/ui/input" ;
67import {
@@ -199,13 +200,13 @@ const formatBytes = (bytes: number, decimals = 2) => {
199200 return `${ parseFloat ( ( bytes / Math . pow ( k , i ) ) . toFixed ( dm ) ) } ${ sizes [ i ] } ` ;
200201} ;
201202
202- export function AssetManager ( ) {
203+ export function AssetManager ( { rootPath = "" } : { rootPath ?: string } ) {
203204 // Root level management for Tabs
204205 const {
205206 folders : rootFolders ,
206207 loading : rootLoading ,
207208 refresh : refreshRoot ,
208- } = useAssets ( "" ) ;
209+ } = useAssets ( rootPath ) ;
209210 const [ activeTab , setActiveTab ] = useState ( "overview" ) ;
210211
211212 // Navigation state within a tab
@@ -216,6 +217,7 @@ export function AssetManager() {
216217 // If specific tab, path is tabName + subPath joined
217218 const getPath = ( ) => {
218219 const parts = [ ] ;
220+ if ( rootPath ) parts . push ( rootPath ) ;
219221 if ( activeTab !== "overview" ) parts . push ( activeTab ) ;
220222 if ( subPath . length > 0 ) parts . push ( ...subPath ) ;
221223 return parts . join ( "/" ) ;
@@ -464,9 +466,29 @@ export function AssetManager() {
464466 return folders ;
465467 } , [ folders , searchTerm ] ) ;
466468
467- const handleUpload = ( files : File [ ] ) => {
468- files . forEach ( ( file ) => uploadAsset ( file ) ) ;
469- if ( currentPath === "" ) {
469+ const handleUpload = async ( files : File [ ] ) => {
470+ // Convert images to WebP
471+ const processedFiles = await Promise . all (
472+ files . map ( async ( file ) => {
473+ if ( file . type . startsWith ( "image/" ) ) {
474+ try {
475+ return await convertImageToWebP ( file ) ;
476+ } catch ( e ) {
477+ console . error ( "Failed to convert image to WebP" , e ) ;
478+ toast ( {
479+ title : "Image conversion failed" ,
480+ description : `Could not convert ${ file . name } to WebP. Uploading original.` ,
481+ variant : "destructive" ,
482+ } ) ;
483+ return file ;
484+ }
485+ }
486+ return file ;
487+ } )
488+ ) ;
489+
490+ processedFiles . forEach ( ( file ) => uploadAsset ( file ) ) ;
491+ if ( activeTab === "overview" && subPath . length === 0 ) {
470492 setTimeout ( refreshRoot , 2000 ) ;
471493 }
472494 } ;
@@ -499,7 +521,7 @@ export function AssetManager() {
499521 }
500522
501523 return (
502- < div className = "grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4" >
524+ < div className = "grid grid-cols-[repeat(auto-fill,minmax(130px,1fr))] gap-4" >
503525 { filteredAssets . map ( ( asset ) => (
504526 < div
505527 key = { asset . id }
@@ -940,7 +962,7 @@ export function AssetManager() {
940962 < h3 className = "text-sm font-medium text-white/50 mb-4 uppercase tracking-wider" >
941963 Folders
942964 </ h3 >
943- < div className = "grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4" >
965+ < div className = "grid grid-cols-[repeat(auto-fill,minmax(130px,1fr))] gap-4" >
944966 { filteredFolders . map ( ( folder ) => (
945967 < div
946968 key = { folder . id }
0 commit comments