@@ -63,9 +63,14 @@ export const FileInput = ({
6363 ] ;
6464 const [ selectedFiles , setSelectedFiles ] = useState < SelectedFile [ ] > ( [ ] ) ;
6565 const [ uploadedVideoUrl , setUploadedVideoUrl ] = useState ( videoUrl ?? "" ) ;
66- const [ activeTab , setActiveTab ] = useState ( videoUrl ? "video" : "image" ) ;
66+ const [ activeTab , setActiveTab ] = useState ( isVideoAllowed && videoUrl ? "video" : "image" ) ;
6767 const [ imageUrlTemp , setImageUrlTemp ] = useState ( fileUrl ?? "" ) ;
6868 const [ videoUrlTemp , setVideoUrlTemp ] = useState ( videoUrl ?? "" ) ;
69+ const fileType = isVideoAllowed && activeTab === "video" ? "video" : "image" ;
70+
71+ useEffect ( ( ) => {
72+ setActiveTab ( isVideoAllowed && videoUrl ? "video" : "image" ) ;
73+ } , [ isVideoAllowed , videoUrl ] ) ;
6974
7075 const handleUpload = async ( files : File [ ] ) => {
7176 if ( ! isStorageConfigured ) {
@@ -115,7 +120,7 @@ export const FileInput = ({
115120 return ;
116121 }
117122
118- onFileUpload ( uploadedUrls , activeTab === "video" ? "video" : "image" ) ;
123+ onFileUpload ( uploadedUrls , fileType ) ;
119124 } ;
120125
121126 const handleDragOver = ( e : React . DragEvent < HTMLLabelElement > ) => {
@@ -134,7 +139,7 @@ export const FileInput = ({
134139
135140 const handleRemove = async ( idx : number ) => {
136141 const newFileUrl = selectedFiles . filter ( ( _ , i ) => i !== idx ) . map ( ( file ) => file . url ) ;
137- onFileUpload ( newFileUrl , activeTab === "video" ? "video" : "image" ) ;
142+ onFileUpload ( newFileUrl , fileType ) ;
138143 setImageUrlTemp ( "" ) ;
139144 } ;
140145
@@ -185,7 +190,7 @@ export const FileInput = ({
185190 } ) ;
186191
187192 const prevUrls = Array . isArray ( fileUrl ) ? fileUrl : fileUrl ? [ fileUrl ] : [ ] ;
188- onFileUpload ( [ ...prevUrls , ...uploadedUrls ] , activeTab === "video" ? "video" : "image" ) ;
193+ onFileUpload ( [ ...prevUrls , ...uploadedUrls ] , fileType ) ;
189194 } ;
190195
191196 useEffect ( ( ) => {
@@ -202,23 +207,23 @@ export const FileInput = ({
202207 } , [ fileUrl ] ) ;
203208
204209 useEffect ( ( ) => {
205- if ( activeTab === "image" && typeof imageUrlTemp === "string" ) {
210+ if ( fileType === "image" && typeof imageUrlTemp === "string" ) {
206211 // Temporarily store the current video URL before switching tabs.
207212 setVideoUrlTemp ( videoUrl ?? "" ) ;
208213
209214 if ( imageUrlTemp ) {
210215 onFileUpload ( [ imageUrlTemp ] , "image" ) ;
211216 }
212- } else if ( activeTab === "video" ) {
217+ } else if ( fileType === "video" ) {
213218 // Temporarily store the current image URL before switching tabs.
214219 setImageUrlTemp ( fileUrl ?? "" ) ;
215220
216221 if ( videoUrlTemp ) {
217222 onFileUpload ( [ videoUrlTemp ] , "video" ) ;
218223 }
219224 }
220- // eslint-disable-next-line react-hooks/exhaustive-deps -- Only run when activeTab changes to avoid infinite loops
221- } , [ activeTab ] ) ;
225+ // eslint-disable-next-line react-hooks/exhaustive-deps -- Only run when file type changes to avoid loops
226+ } , [ fileType ] ) ;
222227
223228 return (
224229 < div className = "w-full cursor-default" >
@@ -227,7 +232,7 @@ export const FileInput = ({
227232 < OptionsSwitch options = { options } currentOption = { activeTab } handleOptionChange = { setActiveTab } />
228233 ) }
229234 < div >
230- { activeTab === "video" && (
235+ { fileType === "video" && (
231236 < div className = { cn ( isVideoAllowed && "rounded-b-lg border-x border-b border-slate-200 p-4" ) } >
232237 < VideoSettings
233238 uploadedVideoUrl = { uploadedVideoUrl }
@@ -239,7 +244,7 @@ export const FileInput = ({
239244 </ div >
240245 ) }
241246
242- { activeTab === "image" && (
247+ { fileType === "image" && (
243248 < div className = { cn ( isVideoAllowed && "rounded-b-lg border-x border-b border-slate-200 p-4" ) } >
244249 { selectedFiles . length > 0 ? (
245250 multiple ? (
0 commit comments