Files
next-template/src/lib/hooks/use-file-upload.ts

83 lines
2.1 KiB
TypeScript

'use client';
import { useState, useRef } from 'react';
import { uploadFile, resizeImage } from '@/lib/queries';
import { toast } from 'sonner';
import { useAuth } from '@/lib/hooks/context';
import { type SupabaseClient } from '@/utils/supabase';
type UploadToStorageProps = {
client: SupabaseClient;
file: File;
bucket: string;
resize?: false | {
maxWidth?: number;
maxHeight?: number;
quality?: number;
},
replace?: false | string,
};
const useFileUpload = () => {
const [isUploading, setIsUploading] = useState(false);
const fileInputRef = useRef<HTMLInputElement | null>(null);
const { profile, isAuthenticated } = useAuth();
const uploadToStorage = async ({
client,
file,
bucket,
resize = false,
replace = false,
}: UploadToStorageProps) => {
try {
if (!isAuthenticated)
throw new Error('User is not authenticated!');
setIsUploading(true);
let fileToUpload = file;
if (resize && file.type.startsWith('image/'))
fileToUpload = await resizeImage({file, options: resize});
if (replace) {
const { data, error} = await uploadFile({
client,
bucket,
path: replace,
file: fileToUpload,
options: {
contentType: file.type,
upsert: true,
},
});
if (error) throw error;
return data
} else {
const fileExt = file.name.split('.').pop();
const fileName = `${Date.now()}-${profile?.id}.${fileExt}`;
const { data, error } = await uploadFile({
client,
bucket,
path: fileName,
file: fileToUpload,
options: {
contentType: file.type,
},
});
if (error) throw error;
return data;
}
} catch (error) {
toast.error(`Error uploading file: ${error as string}`);
return error;
} finally {
setIsUploading(false);
if (fileInputRef.current) fileInputRef.current.value = '';
}
};
return {
isUploading,
fileInputRef,
uploadToStorage,
};
};
export { useFileUpload };