2026-10-05 18:10:18 -04:00
import { useCallback , useEffect , useRef } from 'react' ;
2026-10-20 15:30:15 -04:00
import { toast } from 'sonner' ;
2026-11-11 18:34:47 +01:00
import { useCurrentServer , useCurrentServerUuid , type Server } from '@/api/server/queries' ;
2026-10-05 18:10:18 -04:00
import { useUploadFiles } from '@/api/server/files/queries' ;
import { useFileUploadActions , useServerDirectory } from '@/state/server' ;
2026-10-20 15:30:15 -04:00
import type { UploadSource } from '@/views/server/files' ;
2026-10-05 18:10:18 -04:00
const UPLOADED_DISPLAY_MS = 500 ;
2026-10-20 15:30:15 -04:00
// A dropped folder arrives as a File too; only its entry says it is a directory.
const holdsFolder = ( source : DataTransfer ) =>
[... source . items ]. some (( item ) => item . kind === 'file' && item . webkitGetAsEntry () ? . isDirectory );
2026-11-11 18:34:47 +01:00
const selectUploadSize = ( server : Server ) => server . attributes . upload_size ;
/** Uploads into the current directory; rejects when a folder was dropped, a file is over the node's upload limit or any file failed, after its error toast. */
2026-10-20 15:30:15 -04:00
export default function useFileUploader () : ( source : UploadSource ) => Promise < void > {
2026-10-05 18:10:18 -04:00
const removals = useRef ( new Map < string , ReturnType < typeof setTimeout >>());
const { mutateAsync } = useUploadFiles ();
const uuid = useCurrentServerUuid () ! ;
2026-11-11 18:34:47 +01:00
const uploadSize = useCurrentServer ( selectUploadSize ) ! ;
2026-10-05 18:10:18 -04:00
const directory = useServerDirectory ();
const { removeFileUpload , pushFileUpload , setUploadProgress } = useFileUploadActions ();
useEffect (() => {
const pending = removals . current ;
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return () => {
2026-10-08 16:53:20 -04:00
for ( const [ id , timeout ] of pending ) {
2026-10-05 18:10:18 -04:00
clearTimeout ( timeout );
removeFileUpload ( id );
2026-10-08 16:53:20 -04:00
}
2026-10-05 18:10:18 -04:00
pending . clear ();
};
}, [ removeFileUpload ]);
return useCallback (
2026-10-20 15:30:15 -04:00
async ( source ) => {
const dropped = 'items' in source ;
if ( dropped && holdsFolder ( source )) {
toast . error ( 'Folder uploads are not supported.' );
throw new Error ( 'Folder uploads are not supported.' );
}
const files = dropped ? [... source . files ] : [... source ];
if ( files . length === 0 ) {
return ;
}
2026-11-11 18:34:47 +01:00
const oversized = files . filter (( file ) => file . size > uploadSize * 1024 * 1024 );
if ( oversized . length > 0 ) {
const message =
oversized . length === 1
? ` ${ oversized [ 0 ]. name } is larger than the ${ uploadSize } MiB upload limit.`
: ` ${ oversized . length } files are larger than the ${ uploadSize } MiB upload limit.` ;
toast . error ( message );
throw new Error ( message );
}
2026-10-05 18:10:18 -04:00
const { failed } = await mutateAsync ({
uuid ,
directory ,
2026-10-20 15:30:15 -04:00
files ,
2026-10-05 18:10:18 -04:00
callbacks : {
onFileQueued : ( id , file , abort ) => {
pushFileUpload ({ id , data : { name : file.name , abort , loaded : 0 , total : file.size } });
},
onFileProgress : ( id , progress ) => setUploadProgress ({ id , loaded : progress.loaded }),
onFileSettled : ( id , outcome ) => {
if ( outcome !== 'uploaded' ) {
removeFileUpload ( id );
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return ;
}
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
const timeout = setTimeout (() => {
removals . current . delete ( id );
removeFileUpload ( id );
}, UPLOADED_DISPLAY_MS );
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
removals . current . set ( id , timeout );
},
},
});
if ( failed . length > 0 ) {
throw new Error (
failed . length === 1
? `Unable to upload ${ failed [ 0 ]. name } .`
: `Unable to upload ${ failed . length } files.`
);
}
},
2026-11-11 18:34:47 +01:00
[ mutateAsync , uuid , uploadSize , directory , removeFileUpload , pushFileUpload , setUploadProgress ]
2026-10-05 18:10:18 -04:00
);
}