|
1 | 1 | import * as React from 'react';
|
2 | 2 |
|
3 |
| -import { classNames, ComponentClassName, isUndefined } from '@aws-amplify/ui'; |
| 3 | +import { classNames, ComponentClassName } from '@aws-amplify/ui'; |
4 | 4 | import { Image } from '@aws-amplify/ui-react';
|
5 |
| -import { useStorageURL } from '@aws-amplify/ui-react/internal'; |
6 |
| -import { useSetUserAgent } from '@aws-amplify/ui-react-core'; |
| 5 | +import { useDeprecationWarning } from '@aws-amplify/ui-react/internal'; |
| 6 | +import { useGetUrl, useSetUserAgent } from '@aws-amplify/ui-react-core'; |
7 | 7 |
|
8 | 8 | import { VERSION } from '../../version';
|
9 |
| -import type { StorageImageProps } from './types'; |
| 9 | +import type { StorageImageProps, StorageImagePathProps } from './types'; |
| 10 | + |
| 11 | +export const MISSING_REQUIRED_PROP_MESSAGE = |
| 12 | + '`StorageImage` requires either an `imgKey` or `path` prop.'; |
| 13 | + |
| 14 | +export const HAS_DEPRECATED_PROPS_MESSAGE = |
| 15 | + '`imgKey`, `accessLevel`, and `identityId` will be replaced with `path` in a future major version.'; |
| 16 | + |
| 17 | +export const HAS_PATH_AND_KEY_MESSAGE = |
| 18 | + '`imgKey` is ignored when both `imgKey` and `path` props are provided.'; |
| 19 | + |
| 20 | +export const HAS_PATH_AND_UNSUPPORTED_OPTIONS_MESSAGE = |
| 21 | + '``accessLevel` and `identityId` are ignored when the `path` prop is provided.'; |
| 22 | + |
| 23 | +const getDeprecationMessage = ({ |
| 24 | + hasImgkey, |
| 25 | + hasPath, |
| 26 | + hasDeprecatedOptions, |
| 27 | +}: { |
| 28 | + hasImgkey: boolean; |
| 29 | + hasPath: boolean; |
| 30 | + hasDeprecatedOptions: boolean; |
| 31 | +}): string => { |
| 32 | + let message = ''; |
| 33 | + |
| 34 | + if (hasPath && hasImgkey) { |
| 35 | + message = HAS_PATH_AND_KEY_MESSAGE; |
| 36 | + } else if (hasPath && hasDeprecatedOptions) { |
| 37 | + message = HAS_PATH_AND_UNSUPPORTED_OPTIONS_MESSAGE; |
| 38 | + } else if (hasImgkey) { |
| 39 | + message = HAS_DEPRECATED_PROPS_MESSAGE; |
| 40 | + } |
| 41 | + |
| 42 | + return message; |
| 43 | +}; |
10 | 44 |
|
11 | 45 | export const StorageImage = ({
|
12 | 46 | accessLevel,
|
13 | 47 | className,
|
14 | 48 | fallbackSrc,
|
15 | 49 | identityId,
|
16 | 50 | imgKey,
|
| 51 | + path, |
17 | 52 | onStorageGetError,
|
18 |
| - validateObjectExistence, |
| 53 | + onGetUrlError, |
| 54 | + validateObjectExistence = true, |
19 | 55 | ...rest
|
20 |
| -}: StorageImageProps): JSX.Element => { |
21 |
| - const resolvedValidateObjectExistence = isUndefined(validateObjectExistence) |
22 |
| - ? true |
23 |
| - : validateObjectExistence; |
24 |
| - const options = React.useMemo( |
25 |
| - () => ({ |
26 |
| - accessLevel, |
27 |
| - targetIdentityId: identityId, |
28 |
| - validateObjectExistence: resolvedValidateObjectExistence, |
29 |
| - }), |
30 |
| - [accessLevel, identityId, resolvedValidateObjectExistence] |
31 |
| - ); |
| 56 | +}: StorageImageProps | StorageImagePathProps): JSX.Element => { |
| 57 | + const hasImgkey = !!imgKey; |
| 58 | + const hasPath = !!path; |
| 59 | + const hasDeprecatedOptions = !!accessLevel || !!identityId; |
| 60 | + |
| 61 | + const message = getDeprecationMessage({ |
| 62 | + hasDeprecatedOptions, |
| 63 | + hasImgkey, |
| 64 | + hasPath, |
| 65 | + }); |
| 66 | + useDeprecationWarning({ message, shouldWarn: !!message }); |
| 67 | + |
| 68 | + if (!hasImgkey && !hasPath) { |
| 69 | + throw new Error(MISSING_REQUIRED_PROP_MESSAGE); |
| 70 | + } |
32 | 71 |
|
33 | 72 | useSetUserAgent({
|
34 | 73 | componentName: 'StorageImage',
|
35 | 74 | packageName: 'react-storage',
|
36 | 75 | version: VERSION,
|
37 | 76 | });
|
38 | 77 |
|
39 |
| - const url = useStorageURL({ |
40 |
| - key: imgKey, |
41 |
| - options, |
42 |
| - fallbackURL: fallbackSrc, |
43 |
| - onStorageGetError, |
44 |
| - }); |
| 78 | + const onError = onGetUrlError ?? onStorageGetError; |
| 79 | + const input = React.useMemo( |
| 80 | + () => ({ |
| 81 | + ...(path ? { path } : { key: imgKey! }), // if `path` is falsy `imgKey` exists |
| 82 | + onError, |
| 83 | + options: { |
| 84 | + accessLevel, |
| 85 | + targetIdentityId: identityId, |
| 86 | + validateObjectExistence, |
| 87 | + }, |
| 88 | + }), |
| 89 | + [accessLevel, imgKey, identityId, onError, path, validateObjectExistence] |
| 90 | + ); |
| 91 | + |
| 92 | + const { url } = useGetUrl(input); |
45 | 93 |
|
46 | 94 | return (
|
47 | 95 | <Image
|
48 | 96 | {...rest}
|
49 | 97 | className={classNames(ComponentClassName.StorageImage, className)}
|
50 |
| - src={url} |
| 98 | + src={url?.toString() ?? fallbackSrc} |
51 | 99 | />
|
52 | 100 | );
|
53 | 101 | };
|
0 commit comments