mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-03-13 14:40:01 +01:00
add UIA action component
This commit is contained in:
parent
d512fc06c8
commit
a2103a301a
3 changed files with 66 additions and 2 deletions
64
src/app/components/ActionUIA.tsx
Normal file
64
src/app/components/ActionUIA.tsx
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
import React, { ReactNode } from 'react';
|
||||||
|
import { AuthDict, AuthType, IAuthData, UIAFlow } from 'matrix-js-sdk';
|
||||||
|
import { getUIAFlowForStages } from '../utils/matrix-uia';
|
||||||
|
import { useSupportedUIAFlows, useUIACompleted, useUIAFlow } from '../hooks/useUIAFlows';
|
||||||
|
import { UIAFlowOverlay } from './UIAFlowOverlay';
|
||||||
|
import { PasswordStage } from './uia-stages';
|
||||||
|
|
||||||
|
export const SUPPORTED_IN_APP_UIA_STAGES = [AuthType.Password, AuthType.Sso];
|
||||||
|
|
||||||
|
export const pickUIAFlow = (uiaFlows: UIAFlow[]): UIAFlow | undefined => {
|
||||||
|
const passwordFlow = getUIAFlowForStages(uiaFlows, [AuthType.Password]);
|
||||||
|
if (passwordFlow) return passwordFlow;
|
||||||
|
return getUIAFlowForStages(uiaFlows, [AuthType.Sso]);
|
||||||
|
};
|
||||||
|
|
||||||
|
type ActionUIAProps = {
|
||||||
|
userId: string;
|
||||||
|
authData: IAuthData;
|
||||||
|
ongoingFlow: UIAFlow;
|
||||||
|
action: (authDict: AuthDict) => void;
|
||||||
|
onCancel: () => void;
|
||||||
|
};
|
||||||
|
export function ActionUIA({ userId, authData, ongoingFlow, action, onCancel }: ActionUIAProps) {
|
||||||
|
const completed = useUIACompleted(authData);
|
||||||
|
const { getStageToComplete } = useUIAFlow(authData, ongoingFlow);
|
||||||
|
|
||||||
|
const stageToComplete = getStageToComplete();
|
||||||
|
|
||||||
|
if (!stageToComplete) return null;
|
||||||
|
return (
|
||||||
|
<UIAFlowOverlay
|
||||||
|
currentStep={completed.length + 1}
|
||||||
|
stepCount={ongoingFlow.stages.length}
|
||||||
|
onCancel={onCancel}
|
||||||
|
>
|
||||||
|
{stageToComplete.type === AuthType.Password && (
|
||||||
|
<PasswordStage
|
||||||
|
userId={userId}
|
||||||
|
stageData={stageToComplete}
|
||||||
|
onCancel={onCancel}
|
||||||
|
submitAuthDict={action}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</UIAFlowOverlay>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActionUIAFlowsLoaderProps = {
|
||||||
|
authData: IAuthData;
|
||||||
|
unsupported: () => ReactNode;
|
||||||
|
children: (ongoingFlow: UIAFlow) => ReactNode;
|
||||||
|
};
|
||||||
|
export function ActionUIAFlowsLoader({
|
||||||
|
authData,
|
||||||
|
unsupported,
|
||||||
|
children,
|
||||||
|
}: ActionUIAFlowsLoaderProps) {
|
||||||
|
const supportedFlows = useSupportedUIAFlows(authData.flows ?? [], SUPPORTED_IN_APP_UIA_STAGES);
|
||||||
|
const ongoingFlow = supportedFlows.length > 0 ? supportedFlows[0] : undefined;
|
||||||
|
|
||||||
|
if (!ongoingFlow) return unsupported();
|
||||||
|
|
||||||
|
return children(ongoingFlow);
|
||||||
|
}
|
|
@ -13,7 +13,6 @@ import {
|
||||||
IconButton,
|
IconButton,
|
||||||
} from 'folds';
|
} from 'folds';
|
||||||
import FocusTrap from 'focus-trap-react';
|
import FocusTrap from 'focus-trap-react';
|
||||||
import { stopPropagation } from '../utils/keyboard';
|
|
||||||
|
|
||||||
export type UIAFlowOverlayProps = {
|
export type UIAFlowOverlayProps = {
|
||||||
currentStep: number;
|
currentStep: number;
|
||||||
|
@ -29,7 +28,7 @@ export function UIAFlowOverlay({
|
||||||
}: UIAFlowOverlayProps) {
|
}: UIAFlowOverlayProps) {
|
||||||
return (
|
return (
|
||||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||||
<FocusTrap focusTrapOptions={{ initialFocus: false, escapeDeactivates: stopPropagation }}>
|
<FocusTrap focusTrapOptions={{ initialFocus: false, escapeDeactivates: false }}>
|
||||||
<Box style={{ height: '100%' }} direction="Column" grow="Yes" gap="400">
|
<Box style={{ height: '100%' }} direction="Column" grow="Yes" gap="400">
|
||||||
<Box grow="Yes" direction="Column" alignItems="Center" justifyContent="Center">
|
<Box grow="Yes" direction="Column" alignItems="Center" justifyContent="Center">
|
||||||
{children}
|
{children}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
export * from './types';
|
export * from './types';
|
||||||
export * from './DummyStage';
|
export * from './DummyStage';
|
||||||
export * from './EmailStage';
|
export * from './EmailStage';
|
||||||
|
export * from './PasswordStage';
|
||||||
export * from './ReCaptchaStage';
|
export * from './ReCaptchaStage';
|
||||||
export * from './RegistrationTokenStage';
|
export * from './RegistrationTokenStage';
|
||||||
export * from './TermsStage';
|
export * from './TermsStage';
|
||||||
|
|
Loading…
Reference in a new issue