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

Add support for custom headers in AutoImage #2767

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions boilerplate/app/components/AutoImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface AutoImageProps extends ImageProps {
* How tall should the image be?
*/
maxHeight?: number
headers?: {
[key: string]: string
}
}

/**
Expand All @@ -26,6 +29,9 @@ export interface AutoImageProps extends ImageProps {
*/
export function useAutoImage(
remoteUri: string,
headers?: {
[key: string]: string
},
dimensions?: [maxWidth?: number, maxHeight?: number],
): [width: number, height: number] {
const [[remoteWidth, remoteHeight], setRemoteImageDimensions] = useState([0, 0])
Expand All @@ -35,8 +41,12 @@ export function useAutoImage(
useLayoutEffect(() => {
if (!remoteUri) return

Image.getSize(remoteUri, (w, h) => setRemoteImageDimensions([w, h]))
}, [remoteUri])
if (!headers) {
Image.getSize(remoteUri, (w, h) => setRemoteImageDimensions([w, h]))
} else {
Image.getSizeWithHeaders(remoteUri, headers, (w, h) => setRemoteImageDimensions([w, h]))
}
}, [remoteUri, headers])

if (Number.isNaN(remoteAspectRatio)) return [0, 0]

Expand All @@ -61,12 +71,14 @@ export function useAutoImage(
export function AutoImage(props: AutoImageProps) {
const { maxWidth, maxHeight, ...ImageProps } = props
const source = props.source as ImageURISource
const headers = source?.headers

const [width, height] = useAutoImage(
Platform.select({
web: (source?.uri as string) ?? (source as string),
default: source?.uri as string,
}),
headers,
[maxWidth, maxHeight],
)

Expand Down