fix(ui): local image src changing when title changes (#13442)

When changing the title of a newly uploaded image, the image would
flicker because the `createObjectURL` was running and creating a new
local file url. This change allows `handleFileChange` to run and not
affect the url if the file being added is not a new file.

### Before


https://github.com/user-attachments/assets/9e21101e-c4cc-4fc3-b510-18f1a0d9fb3a



### After


https://github.com/user-attachments/assets/9f310e10-d29c-49a9-bd28-cb6da6c5651a
This commit is contained in:
Jarrod Flesch
2025-08-19 15:39:03 -04:00
committed by GitHub
parent 4f6d0d8ed2
commit 406a09f4bf

View File

@@ -185,19 +185,19 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
typeof uploadConfig?.pasteURL === 'object' && uploadConfig.pasteURL.allowList?.length > 0
const handleFileChange = useCallback(
(newFile: File) => {
if (newFile instanceof File) {
setFileSrc(URL.createObjectURL(newFile))
({ file, isNewFile = true }: { file: File | null; isNewFile?: boolean }) => {
if (isNewFile && file instanceof File) {
setFileSrc(URL.createObjectURL(file))
}
setValue(newFile)
setValue(file)
setShowUrlInput(false)
setUploadControlFileUrl('')
setUploadControlFileName(null)
setUploadControlFile(null)
if (typeof onChange === 'function') {
onChange(newFile)
onChange(file)
}
},
[onChange, setValue, setUploadControlFile, setUploadControlFileName, setUploadControlFileUrl],
@@ -217,7 +217,7 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
const updatedFileName = e.target.value
if (value) {
handleFileChange(renameFile(value, updatedFileName))
handleFileChange({ file: renameFile(value, updatedFileName), isNewFile: false })
setFilename(updatedFileName)
}
},
@@ -227,14 +227,14 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
const handleFileSelection = useCallback(
(files: FileList) => {
const fileToUpload = files?.[0]
handleFileChange(fileToUpload)
handleFileChange({ file: fileToUpload })
},
[handleFileChange],
)
const handleFileRemoval = useCallback(() => {
setRemovedFile(true)
handleFileChange(null)
handleFileChange({ file: null })
setFileSrc('')
setFileUrl('')
resetUploadEdits()
@@ -276,7 +276,7 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
const fileName = uploadControlFileName || decodeURIComponent(fileUrl.split('/').pop() || '')
const file = new File([blob], fileName, { type: blob.type })
handleFileChange(file)
handleFileChange({ file })
setUploadStatus('idle')
return // Exit if client-side fetch succeeds
} catch (_clientError) {
@@ -301,7 +301,7 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
const fileName = decodeURIComponent(fileUrl.split('/').pop() || '')
const file = new File([blob], fileName, { type: blob.type })
handleFileChange(file)
handleFileChange({ file })
setUploadStatus('idle')
} catch (_serverError) {
toast.error('The provided URL is not allowed.')
@@ -368,7 +368,7 @@ export const Upload_v4: React.FC<UploadProps_v4> = (props) => {
useEffect(() => {
const handleControlFile = () => {
if (uploadControlFile) {
handleFileChange(uploadControlFile)
handleFileChange({ file: uploadControlFile })
}
}