Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions components/users/ProfilePictureUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ export function ProfilePictureUpload({
}
}

const deleteOldAvatar = async (oldAvatarUrl: string) => {
try {
const supabase = createClient()

// Extract file path from URL
// URL format: https://.../storage/v1/object/public/profile-pictures/avatars/filename.jpg
const urlParts = oldAvatarUrl.split('/storage/v1/object/public/profile-pictures/')
if (urlParts.length < 2) return

const filePath = urlParts[1]

// Delete the old file
const { error } = await supabase.storage
.from('profile-pictures')
.remove([filePath])

if (error) {
console.error('Error deleting old avatar:', error)
// Don't throw - we still want the upload to succeed even if deletion fails
}
} catch (err) {
console.error('Error in deleteOldAvatar:', err)
// Don't throw - non-critical error
}
}

const handleCropComplete = async (croppedImageBlob: Blob) => {
try {
setUploading(true)
Expand All @@ -85,6 +111,11 @@ export function ProfilePictureUpload({

const supabase = createClient()

// Delete old avatar if it exists
if (previewUrl) {
await deleteOldAvatar(previewUrl)
}

// Create a unique file name
const fileName = `${userId}-${Date.now()}.jpg`
const filePath = `avatars/${fileName}`
Expand Down Expand Up @@ -143,6 +174,11 @@ export function ProfilePictureUpload({

const supabase = createClient()

// Delete the avatar file from storage
if (previewUrl) {
await deleteOldAvatar(previewUrl)
}

// Update profile to remove avatar
const { error: updateError } = await supabase
.from('profiles')
Expand Down
Loading