11import { DialogAppSDK } from '@contentful/app-sdk' ;
2- import { useAutoResizer , useSDK } from '@contentful/react-apps-toolkit' ;
2+ import { useSDK } from '@contentful/react-apps-toolkit' ;
33import { injectGlobal } from '@emotion/css' ;
44import { css } from '@emotion/react' ;
5- import { Button } from '@contentful/f36-components' ;
6- import { useCallback , useRef , useState } from 'react' ;
5+ import { useCallback } from 'react' ;
76import { APP_ENV , APP_VERSION } from '../constants' ;
8- import { AppInstallationParameters , MediaLibraryResultAsset } from '../types' ;
7+ import { AppInstallationParameters } from '../types' ;
98import { loadScript } from '../utils' ;
109import { ShowOptions } from './types' ;
1110
1211const styles = {
1312 container : css ( {
1413 height : '100%' ,
1514 } ) ,
16- doneBar : css ( {
17- position : 'fixed' ,
18- bottom : 0 ,
19- left : 0 ,
20- right : 0 ,
21- display : 'flex' ,
22- alignItems : 'center' ,
23- justifyContent : 'flex-end' ,
24- gap : '8px' ,
25- padding : '12px 16px' ,
26- background : 'white' ,
27- borderTop : '1px solid #e5ebed' ,
28- zIndex : 10 ,
29- } ) ,
30- doneLabel : css ( {
31- fontSize : '14px' ,
32- color : '#536171' ,
33- } ) ,
3415} ;
3516
3617const AssetPickerDialog = ( ) => {
3718 const sdk = useSDK < DialogAppSDK < AppInstallationParameters > > ( ) ;
38- useAutoResizer ( ) ;
3919 const invocationParams = sdk . parameters . invocation as Record < string , unknown > ;
4020 const expression = invocationParams . expression as string ;
41- const [ pendingAssets , setPendingAssets ] = useState < MediaLibraryResultAsset [ ] > ( [ ] ) ;
42- const pendingRef = useRef < MediaLibraryResultAsset [ ] > ( [ ] ) ;
43-
44- const handleDone = useCallback ( ( ) => {
45- sdk . close ( pendingRef . current . length > 0 ? { assets : pendingRef . current , mlId : '' } : undefined ) ;
46- } , [ sdk ] ) ;
4721
4822 /**
4923 * Initialization is triggered using the div's ref to ensure the container exists in the DOM
@@ -100,19 +74,7 @@ const AssetPickerDialog = () => {
10074 } ;
10175
10276 const instance = window . cloudinary . createMediaLibrary ( options , {
103- insertHandler : ( data ) => {
104- if ( maxFiles <= 1 ) {
105- // Single-asset field: close immediately (original behavior)
106- sdk . close ( data ) ;
107- } else {
108- // Multi-asset field: accumulate up to maxFiles limit and keep widget open
109- const remaining = maxFiles - pendingRef . current . length ;
110- if ( remaining <= 0 ) return ;
111- const next = [ ...pendingRef . current , ...data . assets . slice ( 0 , remaining ) ] ;
112- pendingRef . current = next ;
113- setPendingAssets ( [ ...next ] ) ;
114- }
115- } ,
77+ insertHandler : ( data ) => sdk . close ( data ) ,
11678 } ) ;
11779
11880 const showOptions : ShowOptions = {
@@ -124,35 +86,10 @@ const AssetPickerDialog = () => {
12486 instance . show ( showOptions ) ;
12587 } ) ( ) ;
12688 } ,
127- [ sdk , invocationParams , expression ] ,
89+ [ sdk ] ,
12890 ) ;
12991
130- const maxFiles = 'maxFiles' in invocationParams ? Number ( invocationParams . maxFiles ) : sdk . parameters . installation . maxFiles ;
131- const isMulti = maxFiles > 1 ;
132-
133- return (
134- < >
135- < div ref = { init } id = "container" css = { styles . container } />
136- { isMulti && (
137- < div css = { styles . doneBar } >
138- { pendingAssets . length > 0 && (
139- < span css = { styles . doneLabel } > { pendingAssets . length } selected</ span >
140- ) }
141- < Button variant = "secondary" size = "small" onClick = { ( ) => sdk . close ( undefined ) } >
142- Cancel
143- </ Button >
144- < Button
145- variant = "primary"
146- size = "small"
147- isDisabled = { pendingAssets . length === 0 }
148- onClick = { handleDone }
149- >
150- Add to field { pendingAssets . length > 0 ? `(${ pendingAssets . length } )` : '' }
151- </ Button >
152- </ div >
153- ) }
154- </ >
155- ) ;
92+ return < div ref = { init } id = "container" css = { styles . container } /> ;
15693} ;
15794
15895export default AssetPickerDialog ;
0 commit comments