1.9 KiB
Plan: Refactor FileUploadSection into generic FileUploadField
Replace the composite, internally-coupled FileUploadSection with a generic, controlled FileUploadField component. Business logic (validation helpers, toast errors, URL generation) moves up into CreateArticleForm, and two FileUploadField instances replace the single FileUploadSection.
Steps
-
Create
file-upload-field.tsxas a controlledFileUploadFieldcomponent acceptingfile: File | null,onFileChange: (file: File | null) => void,accept,validate,label,description, anderrorprops — rendering theFileUpload/FieldUI shell with no internal business logic. -
Move
isImageFile,isContentFile,generateFakeImageUrl, and rejection-toast handlers intocreate-article-form.tsx, addingcoverImageFileandcontentFilestate entries (or keeping them asuseFormvalues directly). -
Replace the
<FileUploadSection>block increate-article-form.tsxwith two<FileUploadField>instances (one for cover image, one for content), wiring each one to the form state viaControlleror directsetValue. -
Delete
file-upload-section.tsxnow that it has no consumers.
Further Considerations
-
onFileChangevssetFile—onFileChangefollows idiomatic React event-prop naming;setFilereads more like a state-setter leak.onFileChangeis recommended but either works. -
State ownership for files — Cover image needs a
Filereference for URL revocation cleanup; should this live in auseStateinside the form or in theFileUploadFielditself via a ref? Keeping it inside the form is cleaner for reset logic. -
Null on clear — The
onFileChange(null)case (user deletes the file) should also clear the related form field value (coverImageUrl→'',content→''), so the handler in the form needs to handle bothFileandnull.