Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate test breakage more #4898

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion editor/src/components/editor/store/project-server-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { updateProjectServerState } from '../actions/action-creators'
import { checkProjectOwned, projectIsStoredLocally } from '../persistence/persistence-backend'
import type { ProjectOwnership } from '../persistence/generic/persistence-types'
import { CollaborationEndpoints } from '../collaborative-endpoints'
import { IS_TEST_ENVIRONMENT } from '../../../common/env-vars'

export interface ProjectMetadataFromServer {
title: string
Expand Down Expand Up @@ -163,7 +164,7 @@ export interface ProjectServerStateUpdaterProps {
}

let serverStateWatcherInstance: number | null = null
const baseWatcherIntervalTime: number = 10 * 1000
const baseWatcherIntervalTime: number = (IS_TEST_ENVIRONMENT ? 100 : 10) * 1000
let currentWatcherIntervalMultiplier: number = 1

function restartServerStateWatcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DataPickerPopupTestId,
VariableFromScopeOptionTestId,
} from './component-section'
import { ImagePreviewTestId } from './property-control-controls'

describe('Set element prop via the data picker', () => {
it('can pick from the property data picker', async () => {
Expand Down Expand Up @@ -320,3 +321,72 @@ registerExternalComponent(
],
},
)`

const projectWithImage = (imageUrl: string) => `import * as React from 'react'
import {
Storyboard,
Scene,
registerInternalComponent,
} from 'utopia-api'

function Image({ url }) {
return <img src={url} />
}

var Playground = ({ style }) => {
return (
<div style={style} data-uid='root'>
<Image url='${imageUrl}' data-uid='image' />
</div>
)
}

export var storyboard = (
<Storyboard data-uid='sb'>
<Scene
style={{
width: 521,
height: 266,
position: 'absolute',
left: 554,
top: 247,
backgroundColor: 'white',
}}
data-uid='scene'
data-testid='scene'
commentId='120'
>
<Playground
style={{
width: 454,
height: 177,
position: 'absolute',
left: 34,
top: 44,
backgroundColor: 'white',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
title='Hello Utopia'
data-uid='pg'
/>
</Scene>
</Storyboard>
)

registerInternalComponent(Image, {
supportsChildren: false,
properties: {
url: {
control: 'string-input',
},
},
variants: [
{
code: '<Image />',
},
],
})

`
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
normalisePathToUnderlyingTarget,
} from '../../../custom-code/code-file'
import { useDispatch } from '../../../editor/store/dispatch-context'
import { isImage } from '../../../../core/shared/utils'

export interface ControlForPropProps<T extends BaseControlDescription> {
propPath: PropertyPath
Expand Down Expand Up @@ -437,28 +438,68 @@ const NumberWithSliderControl = React.memo(
},
)

export const ImagePreviewTestId = 'image-preview'

export const StringInputPropertyControl = React.memo(
(props: ControlForPropProps<StringInputControlDescription>) => {
const { propName, propMetadata, controlDescription } = props

const controlId = `${propName}-string-input-property-control`
const value = propMetadata.propertyStatus.set ? propMetadata.value : undefined

const safeValue = typeof value === 'string' ? value : ''

return (
<StringControl
key={controlId}
id={controlId}
testId={controlId}
value={value ?? ''}
onSubmitValue={propMetadata.onSubmitValue}
controlStatus={propMetadata.controlStatus}
controlStyles={propMetadata.controlStyles}
focus={props.focusOnMount}
/>
<FlexColumn style={{ gap: 5 }}>
<StringControl
key={controlId}
id={controlId}
testId={controlId}
value={safeValue}
onSubmitValue={propMetadata.onSubmitValue}
controlStatus={propMetadata.controlStatus}
controlStyles={propMetadata.controlStyles}
focus={props.focusOnMount}
/>
<ImagePreview url={safeValue} />
</FlexColumn>
)
},
)

interface ImagePreviewProps {
url: string
}
const ImagePreview = React.memo(({ url }: ImagePreviewProps) => {
const [imageCanBeLoaded, setImageCanBeLoaded] = React.useState(isImage(url))

// we need to track if the url has changed so we retry loading the image even if it failed before
const urlRef = React.useRef<string>(url)
if (urlRef.current !== url) {
setImageCanBeLoaded(isImage(url))
urlRef.current = url
}

// don't render the img when it can not be loaded
const onImageError = React.useCallback(() => {
setImageCanBeLoaded(false)
}, [setImageCanBeLoaded])

if (!imageCanBeLoaded) {
return null
}

return (
<img
data-testid={ImagePreviewTestId}
src={url}
style={{ width: '100%' }}
onError={onImageError}
/>
)
})
ImagePreview.displayName = 'ImagePreview'

function keysForVectorOfType(vectorType: 'vector2' | 'vector3' | 'vector4'): Array<string> {
switch (vectorType) {
case 'vector2':
Expand Down
Loading