@@ -4,7 +4,7 @@ import type { StorageContext } from '@/lib/uploads/shared/types'
44
55const logger = createLogger ( 'ProfilePictureUpload' )
66const MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB
7- const ACCEPTED_IMAGE_TYPES = [ 'image/png' , 'image/jpeg' , 'image/jpg' , 'image/svg+xml' ]
7+ const ACCEPTED_IMAGE_TYPES = [ 'image/png' , 'image/jpeg' , 'image/jpg' , 'image/svg+xml' , 'image/webp' ]
88
99interface UseProfilePictureUploadProps {
1010 onUpload ?: ( url : string | null ) => void
@@ -27,10 +27,19 @@ export function useProfilePictureUpload({
2727} : UseProfilePictureUploadProps = { } ) {
2828 const previewRef = useRef < string | null > ( null )
2929 const fileInputRef = useRef < HTMLInputElement > ( null )
30+ const onUploadRef = useRef ( onUpload )
31+ const onErrorRef = useRef ( onError )
32+ const currentImageRef = useRef ( currentImage )
3033 const [ previewUrl , setPreviewUrl ] = useState < string | null > ( currentImage || null )
3134 const [ fileName , setFileName ] = useState < string | null > ( null )
3235 const [ isUploading , setIsUploading ] = useState ( false )
3336
37+ useEffect ( ( ) => {
38+ onUploadRef . current = onUpload
39+ onErrorRef . current = onError
40+ currentImageRef . current = currentImage
41+ } , [ onUpload , onError , currentImage ] )
42+
3443 useEffect ( ( ) => {
3544 if ( previewRef . current && previewRef . current !== currentImage ) {
3645 URL . revokeObjectURL ( previewRef . current )
@@ -44,7 +53,7 @@ export function useProfilePictureUpload({
4453 return `File "${ file . name } " is too large. Maximum size is 5MB.`
4554 }
4655 if ( ! ACCEPTED_IMAGE_TYPES . includes ( file . type ) ) {
47- return `File "${ file . name } " is not a supported image format. Please use PNG, JPEG, or SVG .`
56+ return `File "${ file . name } " is not a supported image format. Please use PNG, JPEG, SVG, or WebP .`
4857 }
4958 return null
5059 } , [ ] )
@@ -88,7 +97,7 @@ export function useProfilePictureUpload({
8897 async ( file : File ) => {
8998 const validationError = validateFile ( file )
9099 if ( validationError ) {
91- onError ?.( validationError )
100+ onErrorRef . current ?.( validationError )
92101 return
93102 }
94103
@@ -105,19 +114,19 @@ export function useProfilePictureUpload({
105114 URL . revokeObjectURL ( newPreviewUrl )
106115 previewRef . current = null
107116 setPreviewUrl ( serverUrl )
108- onUpload ?.( serverUrl )
117+ onUploadRef . current ?.( serverUrl )
109118 } catch ( error ) {
110119 const errorMessage =
111120 error instanceof Error ? error . message : 'Failed to upload profile picture'
112- onError ?.( errorMessage )
121+ onErrorRef . current ?.( errorMessage )
113122 URL . revokeObjectURL ( newPreviewUrl )
114123 previewRef . current = null
115- setPreviewUrl ( currentImage || null )
124+ setPreviewUrl ( currentImageRef . current || null )
116125 } finally {
117126 setIsUploading ( false )
118127 }
119128 } ,
120- [ onUpload , onError , uploadFileToServer , validateFile , currentImage ]
129+ [ uploadFileToServer , validateFile ]
121130 )
122131
123132 const handleFileChange = useCallback (
@@ -147,8 +156,8 @@ export function useProfilePictureUpload({
147156 if ( fileInputRef . current ) {
148157 fileInputRef . current . value = ''
149158 }
150- onUpload ?.( null )
151- } , [ onUpload ] )
159+ onUploadRef . current ?.( null )
160+ } , [ ] )
152161
153162 useEffect ( ( ) => {
154163 return ( ) => {
0 commit comments