forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: images cropping on different devices (kolplattformen#524)
* feat: add image sizes using Image.getSizeWithHeaders * chore: 🤖 bild i fakeData * add rounded corners to embedded images in content * add debug information * add more debug info Co-authored-by: Kajetan Kazimierczak <kajetan@hotmail.com>
- Loading branch information
1 parent
2919489
commit 7c50988
Showing
5 changed files
with
103 additions
and
25 deletions.
There are no files selected for viewing
99 changes: 84 additions & 15 deletions
99
apps/skolplattformen-sthlm/components/image.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,108 @@ | ||
import { useApi } from '@skolplattformen/hooks' | ||
import React, { useEffect, useState } from 'react' | ||
import { Image as ImageBase, ImageStyle, StyleProp } from 'react-native' | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
import { | ||
Image as ImageBase, | ||
ImageResizeMode, | ||
ImageStyle, | ||
StyleProp, | ||
useWindowDimensions, | ||
View, | ||
} from 'react-native' | ||
|
||
interface ImageProps { | ||
src: string | ||
style: StyleProp<ImageStyle> | ||
/** | ||
* Width of component. Defaults to window width | ||
* Used to automatically calculate width | ||
*/ | ||
componentWidth?: number | ||
accessibilityIgnoresInvertColors: boolean | ||
resizeMode?: ImageResizeMode | ||
width?: number | ||
height?: number | ||
} | ||
|
||
export const Image = ({ | ||
src, | ||
style, | ||
componentWidth = 0, | ||
accessibilityIgnoresInvertColors, | ||
resizeMode = 'contain', | ||
}: ImageProps) => { | ||
const { api } = useApi() | ||
const [headers, setHeaders] = useState() | ||
const { width: windowWidth } = useWindowDimensions() | ||
const [dimensions, setDimensions] = useState({ width: 0, height: 0 }) | ||
|
||
const getHeaders = async (url: string) => { | ||
const { headers: newHeaders } = await api.getSession(url) | ||
setHeaders(newHeaders) | ||
} | ||
const debugImageName = getDebugImageName(src) | ||
|
||
const prefetchImageInformation = useCallback( | ||
async (url: string) => { | ||
if (!url) return | ||
const { headers: newHeaders } = await api.getSession(url) | ||
|
||
console.log('[IMAGE] Getting image dimensions with headers', { | ||
debugImageName, | ||
newHeaders, | ||
}) | ||
|
||
ImageBase.getSizeWithHeaders( | ||
url, | ||
newHeaders, | ||
(w, h) => { | ||
console.log('[IMAGE] Received image dimensions', { | ||
debugImageName, | ||
w, | ||
h, | ||
}) | ||
setDimensions({ width: w, height: h }) | ||
setHeaders(newHeaders) | ||
}, | ||
(error) => { | ||
console.error('[Image] Failed to get image dimensions', { | ||
debugImageName, | ||
error, | ||
}) | ||
} | ||
) | ||
}, | ||
[api, debugImageName] | ||
) | ||
|
||
useEffect(() => { | ||
getHeaders(src) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [src]) | ||
prefetchImageInformation(src) | ||
}, [prefetchImageInformation, src]) | ||
|
||
const compWidth = componentWidth || windowWidth | ||
|
||
return headers ? ( | ||
const scale = compWidth / dimensions.width | ||
const scaledWidth = Math.round(dimensions.width * scale) | ||
const scaledHeight = Math.round(dimensions.height * scale) | ||
|
||
const imageSource = | ||
resizeMode === 'cover' | ||
? { uri: src, headers } | ||
: { uri: src, headers, height: scaledHeight, width: scaledWidth } | ||
|
||
return headers && scaledWidth && scaledHeight ? ( | ||
<ImageBase | ||
accessibilityIgnoresInvertColors={accessibilityIgnoresInvertColors} | ||
source={{ | ||
uri: src, | ||
headers, | ||
}} | ||
source={imageSource} | ||
resizeMode={resizeMode} | ||
style={style} | ||
/> | ||
) : null | ||
) : ( | ||
<View style={style} /> | ||
) | ||
} | ||
|
||
const getDebugImageName = (src: string) => { | ||
try { | ||
const split = src.split('/') | ||
return split[split.length - 1] | ||
} catch (e: any) { | ||
console.log('FAILED', e.message) | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters