Small stuff

This commit is contained in:
2025-05-22 18:31:49 -05:00
parent 8169c719f6
commit 7f78bc7123
9 changed files with 238 additions and 48 deletions

View File

@ -1,24 +1,51 @@
'use client'
import { useState, useRef } from 'react';
import { uploadFile } from '@/lib/actions';
import { toast } from 'sonner';
import { useAuth } from '@/components/context/auth';
import { resizeImage } from '@/lib/hooks';
export type uploadToStorageProps = {
file: File;
bucket: string;
resize: boolean;
options?: {
maxWidth?: number;
maxHeight?: number;
quality?: number;
}
};
export const useFileUpload = () => {
const [isUploading, setIsUploading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const { profile, isAuthenticated } = useAuth();
const uploadToStorage = async (file: File, bucket: string) => {
const uploadToStorage = async ({
file,
bucket,
resize = false,
options = {},
}: 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 });
// Generate a unique filename to avoid collisions
const fileExt = file.name.split('.').pop();
const fileName = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}.${fileExt}`;
const fileName = `${Date.now()}-${profile?.id}.${fileExt}`;
// Upload the file to Supabase storage
const uploadResult = await uploadFile({
bucket,
path: fileName,
file,
file: fileToUpload,
options: {
upsert: true,
contentType: file.type,