You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReactfrom'react';import{Grid,Box,FormControl}from'@contentful/f36-components';import{EditorAppSDK,FieldAppSDK}from'@contentful/app-sdk';import{useSDK}from'@contentful/react-apps-toolkit';import{Field,FieldWrapper}from'@contentful/default-field-editors';import{JsonEditor}from'@contentful/field-editor-json';// Patches the sdk to include the help textconstgetHelpText=(fieldId: string,sdk: EditorAppSDK)=>{returnsdk?.editor?.editorInterface?.controls?.find((field)=>field.fieldId===fieldId)?.settings?.helpText||'';}// Converts a field into <FieldAPI> data type, which is the expected data type for many API methodsconstgetFieldAPI=(fieldId: string,sdk: EditorAppSDK)=>{returnsdk.entry.fields[fieldId].getForLocale(sdk.locales.default);}// Creates a <FieldExtensionSDK> type that can be passed to components from the default-field-editors packageconstgetFieldExtensionSdk=(fieldId: string,sdk: EditorAppSDK): FieldAppSDK=>{return{
...sdk,ids: {extension: '',app: '',environmentAlias: '',field: '',
...sdk.ids,},field: getFieldAPI(fieldId,sdk),window: {updateHeight: (height?: number)=>null,startAutoResizer: ()=>null,stopAutoResizer: ()=>null,},// TODO: missing some invocation property, not sure what this does...// @ts-ignoreparameters: {installation: {},instance: {helpText: getHelpText(fieldId,sdk)},}};}// we need to patch the sdk to include the widget idconstgetWidgetId=(fieldId: string,sdk: EditorAppSDK)=>{returnsdk?.editor?.editorInterface?.controls?.find((field)=>field.fieldId===fieldId)?.widgetId||'';}// we need to patch the sdk to include the field nameconstgetFieldName=(fieldId: string,sdk: EditorAppSDK)=>{returnsdk?.contentType?.fields?.find((field)=>field.id===fieldId)?.name||'';}// helper function to make generating a field configuration easierconstgetFieldProps=({ fieldId, sdk }: {fieldId: string,sdk: EditorAppSDK})=>{return{sdk: getFieldExtensionSdk(fieldId,sdk),widgetId: getWidgetId(fieldId,sdk),name: getFieldName(fieldId,sdk),};}constTYPES={NONE: undefined,DEFAULT: 'Default',CARDS_ROW: 'Cards Row',LOGO_WALL: 'Logo Wall',};constEntry=()=>{constsdk=useSDK<EditorAppSDK>();consttriggerElement=getFieldProps({fieldId: 'type', sdk });const[currentType,setCurrentType]=React.useState(triggerElement.sdk.field.getValue());React.useEffect(()=>{triggerElement.sdk.field.onValueChanged((value)=>{setCurrentType(value);});});const[isFocused,setIsFocused]=React.useState({styles: false});constfields=[{shouldRenderWhenType: [TYPES.NONE,TYPES.DEFAULT,TYPES.CARDS_ROW,TYPES.LOGO_WALL],props: getFieldProps({fieldId: 'internalTitle', sdk }),},{shouldRenderWhenType: [TYPES.NONE,TYPES.DEFAULT,TYPES.CARDS_ROW,TYPES.LOGO_WALL],props: getFieldProps({fieldId: 'type', sdk }),},{shouldRenderWhenType: [TYPES.DEFAULT],props: getFieldProps({fieldId: 'defaultLayout', sdk }),},{shouldRenderWhenType: [TYPES.DEFAULT],props: getFieldProps({fieldId: 'defaultBody', sdk }),},{shouldRenderWhenType: [TYPES.DEFAULT],props: getFieldProps({fieldId: 'defaultMedia', sdk }),},{shouldRenderWhenType: [TYPES.CARDS_ROW],props: getFieldProps({fieldId: 'cardsRowheader', sdk }),},{shouldRenderWhenType: [TYPES.CARDS_ROW],props: getFieldProps({fieldId: 'components', sdk }),},{shouldRenderWhenType: [TYPES.LOGO_WALL],props: getFieldProps({fieldId: 'logos', sdk }),},{shouldRenderWhenType: [TYPES.DEFAULT,TYPES.CARDS_ROW,TYPES.LOGO_WALL],props: getFieldProps({fieldId: 'backgroundColor', sdk }),},{shouldRenderWhenType: [TYPES.DEFAULT,TYPES.CARDS_ROW,TYPES.LOGO_WALL],props: getFieldProps({fieldId: 'styles', sdk }),},];return(<Gridstyle={{maxWidth: '816px',marginLeft: 'auto',marginRight: 'auto'}}marginTop="spacingXl"columns="1fr"rowGap="spacingM"columnGap="spacingM"><Grid.Item>{fields.map((field)=>{if(field.shouldRenderWhenType.includes(currentType)){switch(field.props.widgetId){case'objectEditor':
// NOTE: we're using the json editor because the default json field seems to be throwing an errorreturn(<BoxpaddingLeft="spacingL"paddingRight="spacingL"onFocus={()=>{setIsFocused({ ...isFocused,styles: true});}}onBlur={()=>{setIsFocused({ ...isFocused,styles: false});}}><Boxstyle={{borderLeft: isFocused.styles ? '3px solid #0059C8' : '3px solid #CFD9E0'}}paddingLeft='spacingM'><FormControl><FormControl.Labelstyle={{color: '#67728A',fontFamily: 'var(--font-stack-primary)',fontWeight: 'var(--font-weight-normal)'}}>{field.props.name}</FormControl.Label><JsonEditorfield={field.props.sdk.field}isInitiallyDisabled={false}/><FormControl.HelpText>{field.props.sdk.parameters.instance.helpText}</FormControl.HelpText></FormControl></Box></Box>);default:
return(<FieldWrappersdk={field.props.sdk}name={field.props.name}key={field.props.sdk.field.id}><Fieldsdk={field.props.sdk}widgetId={field.props.widgetId}/></FieldWrapper>);}}else{returnnull;}})}</Grid.Item></Grid>);};exportdefaultEntry;
Maybe it's related to this?
The text was updated successfully, but these errors were encountered: