@aws-amplify/ui-react-core 3.4.1 → 3.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +39 -0
  2. package/dist/esm/Authenticator/hooks/useAuthenticator/useAuthenticator.mjs +1 -1
  3. package/dist/esm/Authenticator/hooks/useAuthenticator/utils.mjs +1 -1
  4. package/dist/esm/hooks/useAsyncReducer.mjs +78 -0
  5. package/dist/esm/index.mjs +1 -1
  6. package/dist/index.js +75 -45
  7. package/dist/types/Authenticator/context/AuthenticatorContext.d.ts +1 -1
  8. package/dist/types/Authenticator/context/AuthenticatorProvider.d.ts +2 -1
  9. package/dist/types/Authenticator/hooks/constants.d.ts +1 -1
  10. package/dist/types/Authenticator/hooks/types.d.ts +3 -3
  11. package/dist/types/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.d.ts +2 -2
  12. package/dist/types/Authenticator/hooks/useAuthenticator/index.d.ts +1 -1
  13. package/dist/types/Authenticator/hooks/useAuthenticator/types.d.ts +1 -1
  14. package/dist/types/Authenticator/hooks/useAuthenticator/useAuthenticator.d.ts +1 -1
  15. package/dist/types/Authenticator/hooks/useAuthenticator/utils.d.ts +3 -3
  16. package/dist/types/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.d.ts +2 -2
  17. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/constants.d.ts +2 -2
  18. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/types.d.ts +1 -1
  19. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.d.ts +1 -1
  20. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/utils.d.ts +4 -4
  21. package/dist/types/Authenticator/hooks/utils.d.ts +2 -2
  22. package/dist/types/Authenticator/index.d.ts +4 -2
  23. package/dist/types/components/FormCore/FormProvider.d.ts +1 -1
  24. package/dist/types/components/FormCore/index.d.ts +1 -1
  25. package/dist/types/components/FormCore/types.d.ts +2 -2
  26. package/dist/types/components/FormCore/useControlledField.d.ts +1 -1
  27. package/dist/types/components/FormCore/useField.d.ts +1 -1
  28. package/dist/types/components/FormCore/useForm.d.ts +1 -1
  29. package/dist/types/components/FormCore/withFormProvider.d.ts +2 -2
  30. package/dist/types/components/index.d.ts +2 -1
  31. package/dist/types/elements/ElementsContext.d.ts +1 -1
  32. package/dist/types/elements/defineBaseElement.d.ts +1 -1
  33. package/dist/types/elements/types.d.ts +1 -1
  34. package/dist/types/elements/utils.d.ts +1 -1
  35. package/dist/types/elements/withBaseElementProps.d.ts +1 -1
  36. package/dist/types/hooks/index.d.ts +6 -3
  37. package/dist/types/hooks/useAsyncReducer.d.ts +55 -0
  38. package/dist/types/hooks/useGetUrl.d.ts +1 -1
  39. package/dist/types/hooks/useSetUserAgent.d.ts +1 -1
  40. package/dist/types/index.d.ts +7 -4
  41. package/dist/types/types/index.d.ts +1 -1
  42. package/package.json +3 -3
  43. package/src/Authenticator/context/AuthenticatorContext.tsx +1 -1
  44. package/src/Authenticator/context/AuthenticatorProvider.tsx +3 -3
  45. package/src/Authenticator/hooks/constants.ts +1 -1
  46. package/src/Authenticator/hooks/types.ts +3 -3
  47. package/src/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.ts +2 -2
  48. package/src/Authenticator/hooks/useAuthenticator/index.ts +1 -1
  49. package/src/Authenticator/hooks/useAuthenticator/types.ts +1 -1
  50. package/src/Authenticator/hooks/useAuthenticator/useAuthenticator.ts +3 -2
  51. package/src/Authenticator/hooks/useAuthenticator/utils.ts +12 -7
  52. package/src/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.tsx +3 -5
  53. package/src/Authenticator/hooks/useAuthenticatorRoute/constants.ts +2 -2
  54. package/src/Authenticator/hooks/useAuthenticatorRoute/types.ts +1 -1
  55. package/src/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.ts +1 -1
  56. package/src/Authenticator/hooks/useAuthenticatorRoute/utils.ts +4 -4
  57. package/src/Authenticator/hooks/utils.ts +6 -2
  58. package/src/Authenticator/index.ts +3 -4
  59. package/src/components/FormCore/FormProvider.tsx +1 -1
  60. package/src/components/FormCore/index.ts +1 -1
  61. package/src/components/FormCore/types.ts +5 -5
  62. package/src/components/FormCore/useControlledField.ts +1 -1
  63. package/src/components/FormCore/useField.ts +7 -2
  64. package/src/components/FormCore/useForm.ts +2 -2
  65. package/src/components/FormCore/withFormProvider.tsx +2 -2
  66. package/src/components/index.ts +2 -5
  67. package/src/elements/ElementsContext.tsx +1 -1
  68. package/src/elements/defineBaseElement.tsx +1 -1
  69. package/src/elements/types.ts +1 -1
  70. package/src/elements/utils.ts +1 -1
  71. package/src/elements/withBaseElementProps.tsx +1 -1
  72. package/src/hooks/index.ts +6 -12
  73. package/src/hooks/useAsyncReducer.ts +111 -0
  74. package/src/hooks/useGetUrl.ts +2 -2
  75. package/src/hooks/useSetUserAgent.ts +2 -1
  76. package/src/index.ts +22 -17
  77. package/src/types/index.ts +1 -1
  78. package/dist/esm/hooks/useDataState/useDataState.mjs +0 -48
  79. package/dist/types/hooks/useDataState/index.d.ts +0 -2
  80. package/dist/types/hooks/useDataState/types.d.ts +0 -8
  81. package/dist/types/hooks/useDataState/useDataState.d.ts +0 -8
  82. package/src/hooks/useDataState/index.ts +0 -2
  83. package/src/hooks/useDataState/types.ts +0 -13
  84. package/src/hooks/useDataState/useDataState.ts +0 -70
@@ -1 +1 @@
1
- export { AnyComponent, MergeProps } from './types';
1
+ export type { AnyComponent, MergeProps } from './types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/ui-react-core",
3
- "version": "3.4.1",
3
+ "version": "3.4.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/esm/index.mjs",
6
6
  "react-native": "src/index.ts",
@@ -40,14 +40,14 @@
40
40
  "typecheck": "tsc --noEmit"
41
41
  },
42
42
  "dependencies": {
43
- "@aws-amplify/ui": "6.10.1",
43
+ "@aws-amplify/ui": "6.10.3",
44
44
  "@xstate/react": "^3.2.2",
45
45
  "lodash": "4.17.21",
46
46
  "react-hook-form": "^7.53.2",
47
47
  "xstate": "^4.33.6"
48
48
  },
49
49
  "peerDependencies": {
50
- "aws-amplify": "^6.9.0",
50
+ "aws-amplify": "^6.14.3",
51
51
  "react": "^16.14 || ^17 || ^18 || ^19"
52
52
  },
53
53
  "sideEffects": false
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
 
3
- import { AuthInterpreter, AuthStatus } from '@aws-amplify/ui';
3
+ import type { AuthInterpreter, AuthStatus } from '@aws-amplify/ui';
4
4
 
5
5
  /**
6
6
  * Authenticator React.Context type
@@ -1,10 +1,10 @@
1
- import React, { ReactNode, useContext, useEffect, useMemo } from 'react';
1
+ import type { ReactNode } from 'react';
2
+ import React, { useContext, useEffect, useMemo } from 'react';
2
3
  import { useInterpret } from '@xstate/react';
3
4
 
4
5
  import { getCurrentUser } from 'aws-amplify/auth';
6
+ import type { AuthStatus, AuthMachineHubHandler } from '@aws-amplify/ui';
5
7
  import {
6
- AuthStatus,
7
- AuthMachineHubHandler,
8
8
  createAuthenticatorMachine,
9
9
  defaultAuthHubHandler,
10
10
  listenToAuthHub,
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  AuthenticatorRouteComponentKey,
3
3
  AuthenticatorRouteComponentName,
4
4
  } from './types';
@@ -1,12 +1,12 @@
1
- import React from 'react';
1
+ import type React from 'react';
2
2
 
3
- import {
3
+ import type {
4
4
  ChallengeName,
5
5
  AuthenticatorServiceFacade,
6
6
  LegacyFormFieldOptions,
7
7
  } from '@aws-amplify/ui';
8
8
 
9
- import { UseAuthenticator } from './useAuthenticator';
9
+ import type { UseAuthenticator } from './useAuthenticator';
10
10
 
11
11
  export type AuthenticatorRouteComponentKey =
12
12
  | 'confirmResetPassword'
@@ -1,8 +1,8 @@
1
- import {
1
+ import type {
2
2
  AuthenticatorLegacyFields,
3
3
  AuthenticatorMachineContext,
4
4
  } from '../../types';
5
- import { UseAuthenticator } from '../types';
5
+ import type { UseAuthenticator } from '../types';
6
6
 
7
7
  const allowedMfaTypes = [
8
8
  'EMAIL',
@@ -1,2 +1,2 @@
1
1
  export { default as useAuthenticator } from './useAuthenticator';
2
- export { UseAuthenticator, UseAuthenticatorSelector } from './types';
2
+ export type { UseAuthenticator, UseAuthenticatorSelector } from './types';
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  AuthenticatorServiceFacade,
3
3
  LegacyFormFieldOptions,
4
4
  } from '@aws-amplify/ui';
@@ -1,11 +1,12 @@
1
1
  import React, { useCallback } from 'react';
2
2
  import { useSelector } from '@xstate/react';
3
- import { AuthMachineState, getServiceFacade } from '@aws-amplify/ui';
3
+ import type { AuthMachineState } from '@aws-amplify/ui';
4
+ import { getServiceFacade } from '@aws-amplify/ui';
4
5
 
5
6
  import { AuthenticatorContext } from '../../context';
6
7
 
7
8
  import { USE_AUTHENTICATOR_ERROR } from './constants';
8
- import { UseAuthenticatorSelector, UseAuthenticator } from './types';
9
+ import type { UseAuthenticatorSelector, UseAuthenticator } from './types';
9
10
  import {
10
11
  defaultComparator,
11
12
  getComparator,
@@ -1,21 +1,26 @@
1
- import {
2
- areEmptyArrays,
3
- areEmptyObjects,
1
+ import type {
4
2
  AuthenticatorRoute,
5
3
  AuthMachineState,
6
4
  FormFieldsArray,
7
- getSortedFormFields,
8
5
  UnverifiedUserAttributes,
6
+ AuthMFAType,
7
+ } from '@aws-amplify/ui';
8
+ import {
9
+ areEmptyArrays,
10
+ areEmptyObjects,
11
+ getSortedFormFields,
9
12
  getActorContext,
10
13
  isString,
11
- AuthMFAType,
12
14
  authenticatorTextUtil,
13
15
  } from '@aws-amplify/ui';
14
16
 
15
- import { AuthenticatorLegacyField, AuthenticatorLegacyFields } from '../types';
17
+ import type {
18
+ AuthenticatorLegacyField,
19
+ AuthenticatorLegacyFields,
20
+ } from '../types';
16
21
  import { isComponentRouteKey } from '../utils';
17
22
 
18
- import { Comparator, UseAuthenticatorSelector } from './types';
23
+ import type { Comparator, UseAuthenticatorSelector } from './types';
19
24
 
20
25
  export const defaultComparator = (): false => false;
21
26
 
@@ -1,10 +1,8 @@
1
1
  import React from 'react';
2
- import { AuthenticatorMachineOptions } from '@aws-amplify/ui';
2
+ import type { AuthenticatorMachineOptions } from '@aws-amplify/ui';
3
3
 
4
- import {
5
- useAuthenticator,
6
- UseAuthenticatorSelector,
7
- } from '../useAuthenticator';
4
+ import type { UseAuthenticatorSelector } from '../useAuthenticator';
5
+ import { useAuthenticator } from '../useAuthenticator';
8
6
 
9
7
  // only select `route` from machine context
10
8
  export const routeSelector: UseAuthenticatorSelector = ({ route }) => [route];
@@ -1,8 +1,8 @@
1
- import {
1
+ import type {
2
2
  AuthenticatorMachineContextKey,
3
3
  AuthenticatorRouteComponentKey,
4
4
  } from '../types';
5
- import {
5
+ import type {
6
6
  CommonRouteMachineKey,
7
7
  ConfirmResetPasswordMachineKey,
8
8
  ConfirmSignInMachineKey,
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  AuthenticatorMachineContext,
3
3
  AuthenticatorMachineContextKey,
4
4
  AuthenticatorRouteComponentName,
@@ -1,7 +1,7 @@
1
1
  import { useMemo } from 'react';
2
2
  import { useAuthenticator } from '../useAuthenticator';
3
3
 
4
- import {
4
+ import type {
5
5
  UseAuthenticatorRoute,
6
6
  UseAuthenticatorRouteDefault,
7
7
  UseAuthenticatorRouteParams,
@@ -1,7 +1,7 @@
1
- import { AuthenticatorRoute } from '@aws-amplify/ui';
1
+ import type { AuthenticatorRoute } from '@aws-amplify/ui';
2
2
 
3
3
  import { RenderNothing } from '../../../components';
4
- import {
4
+ import type {
5
5
  AuthenticatorMachineContext,
6
6
  AuthenticatorMachineContextKey,
7
7
  AuthenticatorRouteComponentKey,
@@ -10,13 +10,13 @@ import {
10
10
  Defaults,
11
11
  } from '../types';
12
12
 
13
- import {
13
+ import type {
14
14
  UseAuthenticator,
15
15
  UseAuthenticatorSelector,
16
16
  } from '../useAuthenticator';
17
17
  import { isComponentRouteKey } from '../utils';
18
18
  import { MACHINE_PROP_KEYS, EVENT_HANDLER_KEY_MAP } from './constants';
19
- import {
19
+ import type {
20
20
  ConvertedMachineProps,
21
21
  FormEventHandlerMachineKey,
22
22
  FormEventHandlerPropKey,
@@ -1,7 +1,11 @@
1
- import { AuthenticatorRoute } from '@aws-amplify/ui';
1
+ import type { AuthenticatorRoute } from '@aws-amplify/ui';
2
2
 
3
3
  import { COMPONENT_ROUTE_KEYS, COMPONENT_ROUTE_NAMES } from './constants';
4
- import { AuthenticatorRouteComponentKey, Defaults, Overrides } from './types';
4
+ import type {
5
+ AuthenticatorRouteComponentKey,
6
+ Defaults,
7
+ Overrides,
8
+ } from './types';
5
9
 
6
10
  export const isComponentRouteKey = (
7
11
  route: AuthenticatorRoute
@@ -1,23 +1,22 @@
1
1
  export { AuthenticatorProvider, AuthenticatorContext } from './context';
2
+ export type { UseAuthenticator, UseAuthenticatorRoute } from './hooks';
2
3
  export {
3
4
  resolveAuthenticatorComponents,
4
5
  useAuthenticator,
5
6
  useAuthenticatorRoute,
6
- UseAuthenticator,
7
7
  useAuthenticatorInitMachine,
8
- UseAuthenticatorRoute,
9
8
  } from './hooks';
10
- export {
9
+ export type {
11
10
  Overrides as AuthenticatorComponentOverrides,
12
11
  Defaults as AuthenticatorComponentDefaults,
13
12
  DefaultProps as AuthenticatorComponentDefaultProps,
14
13
  FooterComponent as AuthenticatorFooterComponent,
15
14
  FormFieldsComponent as AuthenticatorFormFieldsComponent,
16
15
  HeaderComponent as AuthenticatorHeaderComponent,
17
- isComponentRouteKey as isAuthenticatorComponentRouteKey,
18
16
  AuthenticatorRouteComponentKey,
19
17
  AuthenticatorRouteComponentName,
20
18
  AuthenticatorLegacyField,
21
19
  AuthenticatorMachineContext,
22
20
  FormFieldsComponent,
23
21
  } from './hooks';
22
+ export { isComponentRouteKey as isAuthenticatorComponentRouteKey } from './hooks';
@@ -4,7 +4,7 @@ import {
4
4
  FormProvider as ReactHookFormProvider,
5
5
  } from 'react-hook-form';
6
6
 
7
- import { FormValues, FormHandle, FormProviderProps } from './types';
7
+ import type { FormValues, FormHandle, FormProviderProps } from './types';
8
8
 
9
9
  const DEFAULT_MODE = 'onTouched';
10
10
 
@@ -1,4 +1,4 @@
1
- export {
1
+ export type {
2
2
  FormProviderProps,
3
3
  FormValues,
4
4
  FormHandle,
@@ -1,5 +1,5 @@
1
- import React from 'react';
2
- import { DefaultValues } from 'react-hook-form';
1
+ import type React from 'react';
2
+ import type { DefaultValues } from 'react-hook-form';
3
3
 
4
4
  export interface FormValues {
5
5
  [k: string]: string;
@@ -63,7 +63,7 @@ export interface RegisterFieldParams<Values extends FormValues = FormValues> {
63
63
 
64
64
  export interface UseFormParams<
65
65
  Values extends FormValues = FormValues,
66
- OnSubmit extends SubmitHandler<Values> = SubmitHandler<Values>
66
+ OnSubmit extends SubmitHandler<Values> = SubmitHandler<Values>,
67
67
  > {
68
68
  /**
69
69
  * Custom error message provided to `Error` on call to `useForm` outside `FormProvider`
@@ -193,7 +193,7 @@ export interface UseField<Name extends string = string>
193
193
  export interface UseFieldParams<
194
194
  OnBlur extends FocusHandler | undefined,
195
195
  OnChange extends ChangeHandler | undefined,
196
- Values extends FormValues = FormValues
196
+ Values extends FormValues = FormValues,
197
197
  > {
198
198
  /**
199
199
  * Controlled `disabled` state
@@ -252,7 +252,7 @@ export interface UseFieldParams<
252
252
  */
253
253
  export interface UseControlledFieldParams<
254
254
  OnBlur extends FocusHandler | undefined,
255
- Values extends FormValues = FormValues
255
+ Values extends FormValues = FormValues,
256
256
  > extends Omit<UseFieldParams<OnBlur, undefined, Values>, 'onChange'> {
257
257
  /**
258
258
  * Controlled text change event handler
@@ -1,6 +1,6 @@
1
1
  import { useController } from 'react-hook-form';
2
2
 
3
- import {
3
+ import type {
4
4
  FormValues,
5
5
  FocusHandler,
6
6
  UseControlledField,
@@ -1,5 +1,10 @@
1
1
  import useForm from './useForm';
2
- import { ChangeHandler, FocusHandler, UseField, UseFieldParams } from './types';
2
+ import type {
3
+ ChangeHandler,
4
+ FocusHandler,
5
+ UseField,
6
+ UseFieldParams,
7
+ } from './types';
3
8
 
4
9
  export const DEFAULT_ERROR_MESSAGE =
5
10
  '`useField` must be used within a `FormProvider`';
@@ -12,7 +17,7 @@ export const DEFAULT_ERROR_MESSAGE =
12
17
  */
13
18
  export default function useField<
14
19
  OnBlur extends FocusHandler,
15
- OnChange extends ChangeHandler
20
+ OnChange extends ChangeHandler,
16
21
  >(params: UseFieldParams<OnBlur, OnChange>): UseField {
17
22
  const { getFieldState, registerField } = useForm({
18
23
  errorMessage: DEFAULT_ERROR_MESSAGE,
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { useFormContext } from 'react-hook-form';
3
3
  import { noop } from '@aws-amplify/ui';
4
4
 
5
- import {
5
+ import type {
6
6
  FormValues,
7
7
  RegisterFieldParams,
8
8
  SetFormValueParams,
@@ -25,7 +25,7 @@ export const DEFAULT_ERROR_MESSAGE =
25
25
  */
26
26
  export default function useForm<
27
27
  Values extends FormValues = FormValues,
28
- OnSubmit extends SubmitHandler = SubmitHandler
28
+ OnSubmit extends SubmitHandler = SubmitHandler,
29
29
  >(options: UseFormParams<Values, OnSubmit> = {}): UseForm<Values> {
30
30
  const formContext = useFormContext();
31
31
  const { errorMessage, onSubmit: _onSubmit } = options;
@@ -1,9 +1,9 @@
1
1
  import React from 'react';
2
2
 
3
- import { AnyComponent } from '../../types';
3
+ import type { AnyComponent } from '../../types';
4
4
 
5
5
  import FormProvider from './FormProvider';
6
- import { FormHandle, FormProviderProps } from './types';
6
+ import type { FormHandle, FormProviderProps } from './types';
7
7
 
8
8
  /**
9
9
  * @param Child `Form` base component wrapped inside `FormProvider`
@@ -1,15 +1,12 @@
1
- export {
2
- FormProvider,
1
+ export type {
3
2
  FormProviderProps,
4
3
  FormValues,
5
4
  FormHandle,
6
5
  SubmitHandler,
7
- useField,
8
- useForm,
9
6
  UseForm,
10
7
  Validate,
11
8
  Validator,
12
- withFormProvider,
13
9
  } from './FormCore';
10
+ export { FormProvider, useField, useForm, withFormProvider } from './FormCore';
14
11
 
15
12
  export { RenderNothing } from './RenderNothing';
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { ElementDisplayName } from './types';
2
+ import type { ElementDisplayName } from './types';
3
3
 
4
4
  /**
5
5
  * @internal @unstable
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { ElementsContext } from './ElementsContext';
3
- import {
3
+ import type {
4
4
  BaseElement,
5
5
  BaseElementProps,
6
6
  BaseElementWithRef,
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import type React from 'react';
2
2
 
3
3
  /**
4
4
  * @internal @unstable
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import type React from 'react';
2
2
 
3
3
  export function isComponent<T>(
4
4
  component?: React.ComponentType<T> | React.ForwardRefExoticComponent<T>
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { BaseElementWithRef, ElementRefType } from './types';
2
+ import type { BaseElementWithRef, ElementRefType } from './types';
3
3
 
4
4
  /**
5
5
  * @internal @unstable
@@ -1,16 +1,10 @@
1
- export {
2
- useDataState,
3
- AsyncDataAction,
4
- DataAction,
5
- DataState,
6
- } from './useDataState';
7
-
8
1
  export { default as useControlledReducer } from './useControlledReducer';
9
- export { default as useDropZone, UseDropZoneParams } from './useDropZone';
10
- export {
11
- default as useDeprecationWarning,
12
- UseDeprecationWarning,
13
- } from './useDeprecationWarning';
2
+ export type { UseDropZoneParams } from './useDropZone';
3
+ export { default as useDropZone } from './useDropZone';
4
+ export type { AsyncReducer, AsyncReducerState } from './useAsyncReducer';
5
+ export { default as useAsyncReducer } from './useAsyncReducer';
6
+ export type { UseDeprecationWarning } from './useDeprecationWarning';
7
+ export { default as useDeprecationWarning } from './useDeprecationWarning';
14
8
  export { default as useGetUrl } from './useGetUrl';
15
9
  export { default as useHasValueUpdated } from './useHasValueUpdated';
16
10
  export { default as usePreviousValue } from './usePreviousValue';
@@ -0,0 +1,111 @@
1
+ import React from 'react';
2
+
3
+ import { isFunction } from '@aws-amplify/ui';
4
+
5
+ // async state constants
6
+ const INITIAL = { hasError: false, isLoading: false, message: undefined };
7
+ const LOADING = { hasError: false, isLoading: true, message: undefined };
8
+ const ERROR = { hasError: true, isLoading: false };
9
+
10
+ export interface AsyncReducerState<T> {
11
+ /**
12
+ * current value
13
+ */
14
+ value: T;
15
+ hasError: boolean;
16
+ isLoading: boolean;
17
+ /**
18
+ * error message, if any
19
+ */
20
+ message: string | undefined;
21
+ }
22
+
23
+ export type AsyncReducer<S, A> = (prevValue: S, action: A) => Promise<S>;
24
+
25
+ /**
26
+ * @internal may be updated in future versions
27
+ *
28
+ * @description like `useReducer` but make it async
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import fetchData from './fetchData';
33
+ *
34
+ * type MyState = { data: string[] | undefined }
35
+ * const initialState: MyState = { data: undefined }
36
+ *
37
+ * type MyAction = { type: 'fetch' | 'clear' }
38
+ *
39
+ * const asyncReducer = async (state: MyState, action: MyAction): Promise<MyState> => {
40
+ * switch(action.type) {
41
+ * case 'fetch':
42
+ * const data = await fetchData();
43
+ * return { data: state.data ? state.data.concat(data) : data }
44
+ * case 'clear':
45
+ * return { data: undefined }
46
+ * }
47
+ * }
48
+ *
49
+ * const FetchDataButton = () => {
50
+ * const [state, dispatch] = useAsyncReducer(asyncReducer, initialState);
51
+ *
52
+ * const { value: { data }, isLoading } = state;
53
+ *
54
+ * return (
55
+ * <button onClick={() => isLoading ? null : dispatch({ type: 'fetch'})}>
56
+ * Fetch Data
57
+ * </button>
58
+ * )
59
+ * }
60
+ * ```
61
+ */
62
+ export default function useAsyncReducer<T, K>(
63
+ reducer: AsyncReducer<T, K>,
64
+ initialValue: T,
65
+ options?: {
66
+ onSuccess?: (data: T) => void;
67
+ onError?: (error: Error) => void;
68
+ }
69
+ ): [AsyncReducerState<T>, React.Dispatch<K>] {
70
+ const [state, setAsyncState] = React.useState<AsyncReducerState<T>>(() => ({
71
+ ...INITIAL,
72
+ value: initialValue,
73
+ }));
74
+
75
+ const prevValue = React.useRef(initialValue);
76
+ const pendingId = React.useRef<Symbol | undefined>();
77
+
78
+ const { onSuccess, onError } = options ?? {};
79
+
80
+ const dispatch: React.Dispatch<K> = React.useCallback(
81
+ (input) => {
82
+ const id = Symbol();
83
+ pendingId.current = id;
84
+
85
+ setAsyncState(({ value }) => ({ ...LOADING, value }));
86
+
87
+ reducer(prevValue.current, input)
88
+ .then((value: T) => {
89
+ if (pendingId.current !== id) return;
90
+
91
+ prevValue.current = value;
92
+
93
+ if (isFunction(onSuccess)) onSuccess(value);
94
+
95
+ setAsyncState({ ...INITIAL, value });
96
+ })
97
+ .catch((error: Error) => {
98
+ if (pendingId.current !== id) return;
99
+
100
+ if (isFunction(onError)) onError(error);
101
+
102
+ const { message } = error;
103
+
104
+ setAsyncState(({ value }) => ({ ...ERROR, value, message }));
105
+ });
106
+ },
107
+ [onError, onSuccess, reducer]
108
+ );
109
+
110
+ return [state, dispatch];
111
+ }
@@ -1,11 +1,11 @@
1
1
  import * as React from 'react';
2
2
 
3
- import {
4
- getUrl,
3
+ import type {
5
4
  GetUrlInput,
6
5
  GetUrlOutput,
7
6
  GetUrlWithPathInput,
8
7
  } from 'aws-amplify/storage';
8
+ import { getUrl } from 'aws-amplify/storage';
9
9
 
10
10
  import { isFunction } from '@aws-amplify/ui';
11
11
 
@@ -1,5 +1,6 @@
1
1
  import { useEffect } from 'react';
2
- import { setUserAgent, SetUserAgentOptions } from '@aws-amplify/ui';
2
+ import type { SetUserAgentOptions } from '@aws-amplify/ui';
3
+ import { setUserAgent } from '@aws-amplify/ui';
3
4
 
4
5
  export default function useSetUserAgent({
5
6
  componentName,