Skip to content

Commit

Permalink
chore(liveness): type Liveness DisplayText with DisplayTextTemplate (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpollman authored Jan 9, 2024
1 parent aa00b25 commit 7b06660
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/neat-shrimps-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@aws-amplify/ui-react-liveness": patch
"@aws-amplify/ui": patch
---

chore(liveness): type Liveness DisplayText with DisplayTextTemplate
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DisplayTextTemplate } from '@aws-amplify/ui';

export type HintDisplayText = {
hintMoveFaceFrontOfCameraText?: string;
hintTooManyFacesText?: string;
Expand Down Expand Up @@ -31,14 +33,27 @@ export type InstructionDisplayText = {
photosensitivityWarningHeadingText?: string;
photosensitivityWarningInfoText?: string;
photosensitivityWarningLabelText?: string;
startScreenBeginCheckText?: string;
tooFarCaptionText?: string;
tooFarAltText?: string;

// TODO remove this typo in next MV bump
/**
* @deprecated `photosensitivyWarningBodyText` has been replaced with `photosensitivityWarningBodyText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness`
*/
photosensitivyWarningBodyText?: string;
/**
* @deprecated `photosensitivyWarningHeadingText` has been replaced with `photosensitivityWarningHeadingText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness`
*/
photosensitivyWarningHeadingText?: string;
/**
* @deprecated `photosensitivyWarningInfoText` has been replaced with `photosensitivityWarningInfoText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness`
*/
photosensitivyWarningInfoText?: string;
/**
* @deprecated `photosensitivyWarningLabelText` has been replaced with `photosensitivityWarningLabelText` amd will be removed in a future major version of `@aws-amplify/ui-react-liveness`
*/
photosensitivyWarningLabelText?: string;
startScreenBeginCheckText?: string;
tooFarCaptionText?: string;
tooFarAltText?: string;
};

export type StreamDisplayText = {
Expand Down Expand Up @@ -68,8 +83,7 @@ export const defaultErrorDisplayText = {
tryAgainText: 'Try again',
};

export type ErrorDisplayTextFoo = typeof defaultErrorDisplayText;
export type ErrorDisplayText = Partial<ErrorDisplayTextFoo>;
export type ErrorDisplayText = Partial<typeof defaultErrorDisplayText>;

export const defaultLivenessDisplayText: Required<LivenessDisplayText> = {
cameraMinSpecificationsHeadingText:
Expand Down Expand Up @@ -119,9 +133,10 @@ export const defaultLivenessDisplayText: Required<LivenessDisplayText> = {
...defaultErrorDisplayText,
};

export interface LivenessDisplayText
extends HintDisplayText,
CameraDisplayText,
InstructionDisplayText,
ErrorDisplayText,
StreamDisplayText {}
export type LivenessDisplayText = DisplayTextTemplate<
HintDisplayText &
CameraDisplayText &
InstructionDisplayText &
ErrorDisplayText &
StreamDisplayText
>;
51 changes: 39 additions & 12 deletions packages/ui/src/types/displayText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,38 @@ type DisplayTextBody = string;
// aggregate display text suffixes
type DisplayTextSuffix = FieldSuffix | TextSuffix;

type GetDisplayTextKey = `${GetPrefix}${DisplayTextBody}${DisplayTextSuffix}`;
type DisplayTextKey = `${DisplayTextBody}${DisplayTextSuffix}`;
// example: `getSomeText`
type DisplayTextFunctionKey =
`${GetPrefix}${DisplayTextBody}${DisplayTextSuffix}`;

type DisplayTextBase =
// Keys matching `GetDisplayTextKey` must be a function returning
// a display `string` or `undefined` for flexibility
| Record<GetDisplayTextKey, (...args: any) => string | undefined>
// Keys matching `DisplayTextKey` must be a `string``
| Record<DisplayTextKey, string>;
// example: `someText`
type DisplayTextStringKey = `${DisplayTextBody}${DisplayTextSuffix}`;

// example: `(isCopied: boolean) => string`
type DisplayTextFunction = (...args: any) => string | undefined;

// keys matching `DisplayTextFunctionKey` must be a function returning
// a display `string` or `undefined` for flexibility
type DisplayTextFunctions = Record<DisplayTextFunctionKey, DisplayTextFunction>;

// keys matching `DisplayTextStringKey` must be a `string``
type DisplayTextStrings = Record<DisplayTextStringKey, string>;

type IsDisplayTextFunction<T, K> = T extends DisplayTextFunctionKey
? K extends DisplayTextFunction
? T
: never
: never;

type IsDisplayTextString<T, K> = T extends DisplayTextStringKey
? K extends string
? T
: never
: never;

type FilterDisplayText<T, K> =
| IsDisplayTextFunction<T, K>
| IsDisplayTextString<T, K>;

/**
* Display Text type utility for creating standardized `DisplayText` interfaces
Expand All @@ -81,6 +104,7 @@ type DisplayTextBase =
* type SomeComponentDisplayText = DisplayTextTemplate<{
* getCopyButtonText?: GetCopyButtonText;
* submitButtonText?: string;
* usernameFieldLabel?: string;
* }>;
*
* // default interface
Expand All @@ -89,11 +113,14 @@ type DisplayTextBase =
* // default values
* const someComponentDisplayText: SomeComponentDisplayTextDefault = {
* getCopyButtonText: (hasCopied) => (hasCopied ? 'Copied' : 'Copy'),
* submitButtonText: 'Submit'
* submitButtonText: 'Submit',
* usernameFieldLabel: 'Username',
* };
* ```
*/
export type DisplayTextTemplate<T extends DisplayTextBase> = {
// filter out disallowed keys
[K in keyof T]: K extends GetDisplayTextKey | DisplayTextKey ? T[K] : never;
export type DisplayTextTemplate<
T extends DisplayTextFunctions | DisplayTextStrings,
> = {
// remove disallowed `displayText` keys
[K in keyof T as FilterDisplayText<K, T[K]>]: T[K];
};

0 comments on commit 7b06660

Please sign in to comment.