-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
651 additions
and
137 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ReactNode } from 'react'; | ||
import { UIAFlow } from 'matrix-js-sdk'; | ||
import { useSupportedUIAFlows } from '../hooks/useUIAFlows'; | ||
|
||
export function SupportedUIAFlowsLoader({ | ||
flows, | ||
supportedStages, | ||
children, | ||
}: { | ||
supportedStages: string[]; | ||
flows: UIAFlow[]; | ||
children: (supportedFlows: UIAFlow[]) => ReactNode; | ||
}) { | ||
const supportedFlows = useSupportedUIAFlows(flows, supportedStages); | ||
|
||
return children(supportedFlows); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { ComponentProps, forwardRef } from 'react'; | ||
import { Icon, IconButton, Input, config, Icons } from 'folds'; | ||
import { UseStateProvider } from '../UseStateProvider'; | ||
|
||
type PasswordInputProps = Omit<ComponentProps<typeof Input>, 'type' | 'size'> & { | ||
size: '400' | '500'; | ||
}; | ||
export const PasswordInput = forwardRef<HTMLInputElement, PasswordInputProps>( | ||
({ variant, size, style, after, ...props }, ref) => { | ||
const btnSize: ComponentProps<typeof IconButton>['size'] = size === '500' ? '400' : '300'; | ||
|
||
return ( | ||
<UseStateProvider initial={false}> | ||
{(visible, setVisible) => ( | ||
<Input | ||
{...props} | ||
ref={ref} | ||
style={{ paddingRight: config.space.S200, ...style }} | ||
type={visible ? 'text' : 'password'} | ||
size={size} | ||
variant={visible ? 'Warning' : variant} | ||
after={ | ||
<> | ||
{after} | ||
<IconButton | ||
onClick={() => setVisible(!visible)} | ||
type="button" | ||
variant={visible ? 'Warning' : variant} | ||
size={btnSize} | ||
radii="300" | ||
> | ||
<Icon | ||
style={{ opacity: config.opacity.P300 }} | ||
size="100" | ||
src={visible ? Icons.Eye : Icons.EyeBlind} | ||
/> | ||
</IconButton> | ||
</> | ||
} | ||
/> | ||
)} | ||
</UseStateProvider> | ||
); | ||
} | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { AuthType, IAuthData, UIAFlow } from 'matrix-js-sdk'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { | ||
getSupportedUIAFlows, | ||
getUIACompleted, | ||
getUIAErrorCode, | ||
getUIAParams, | ||
getUIASession, | ||
} from '../utils/matrix-uia'; | ||
|
||
export const SUPPORTED_FLOW_TYPES = [ | ||
AuthType.Dummy, | ||
AuthType.Password, | ||
AuthType.Email, | ||
AuthType.Terms, | ||
AuthType.Recaptcha, | ||
AuthType.RegistrationToken, | ||
] as const; | ||
|
||
export const useSupportedUIAFlows = (uiaFlows: UIAFlow[], supportedStages: string[]): UIAFlow[] => | ||
useMemo(() => getSupportedUIAFlows(uiaFlows, supportedStages), [uiaFlows, supportedStages]); | ||
|
||
export const useUIACompleted = (authData: IAuthData): string[] => | ||
useMemo(() => getUIACompleted(authData), [authData]); | ||
|
||
export const useUIAParams = (authData: IAuthData) => | ||
useMemo(() => getUIAParams(authData), [authData]); | ||
|
||
export const useUIASession = (authData: IAuthData) => | ||
useMemo(() => getUIASession(authData), [authData]); | ||
|
||
export const useUIAErrorCode = (authData: IAuthData) => | ||
useMemo(() => getUIAErrorCode(authData), [authData]); | ||
|
||
export type StageInfo = Record<string, unknown>; | ||
export type AuthStageData = { | ||
type: string; | ||
info?: StageInfo; | ||
session?: string; | ||
errorCode?: string; | ||
}; | ||
export type AuthStageDataGetter = () => AuthStageData | undefined; | ||
|
||
export type UIAFlowInterface = { | ||
getStageToComplete: AuthStageDataGetter; | ||
hasStage: (stageType: string) => boolean; | ||
getStageInfo: (stageType: string) => StageInfo | undefined; | ||
}; | ||
export const useUIAFlow = (authData: IAuthData, uiaFlow: UIAFlow): UIAFlowInterface => { | ||
const completed = useUIACompleted(authData); | ||
const params = useUIAParams(authData); | ||
const session = useUIASession(authData); | ||
const errorCode = useUIAErrorCode(authData); | ||
|
||
const getPrevCompletedStage = useCallback(() => { | ||
const prevCompletedI = completed.length - 1; | ||
const prevCompletedStage = prevCompletedI !== -1 ? completed[prevCompletedI] : undefined; | ||
return prevCompletedStage; | ||
}, [completed]); | ||
|
||
const getStageToComplete: AuthStageDataGetter = useCallback(() => { | ||
const { stages } = uiaFlow; | ||
const prevCompletedStage = getPrevCompletedStage(); | ||
|
||
const nextStageIndex = stages.findIndex((stage) => stage === prevCompletedStage) + 1; | ||
const nextStage = nextStageIndex < stages.length ? stages[nextStageIndex] : undefined; | ||
if (!nextStage) return undefined; | ||
|
||
const info = params[nextStage]; | ||
|
||
return { | ||
type: nextStage, | ||
info, | ||
session, | ||
errorCode, | ||
}; | ||
}, [uiaFlow, getPrevCompletedStage, params, errorCode, session]); | ||
|
||
const hasStage = useCallback( | ||
(stageType: string): boolean => uiaFlow.stages.includes(stageType), | ||
[uiaFlow] | ||
); | ||
|
||
const getStageInfo = useCallback( | ||
(stageType: string): StageInfo | undefined => { | ||
if (!hasStage(stageType)) return undefined; | ||
|
||
return params[stageType]; | ||
}, | ||
[hasStage, params] | ||
); | ||
|
||
return { | ||
getStageToComplete, | ||
hasStage, | ||
getStageInfo, | ||
}; | ||
}; |
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
Oops, something went wrong.