1 import { type FC } from 'react';
3 import { c } from 'ttag';
5 import { usePassCore } from '@proton/pass/components/Core/PassCoreProvider';
6 import { OTPDonut } from '@proton/pass/components/Otp/OTPDonut';
7 import { OTPValue } from '@proton/pass/components/Otp/OTPValue';
8 import { usePeriodicOtpCode } from '@proton/pass/hooks/usePeriodicOtpCode';
9 import { type OtpRequest } from '@proton/pass/types';
11 import { ValueControl } from './ValueControl';
13 type Props = { label?: string; payload: OtpRequest };
15 /* This component handles the period otp code generation
16 * to avoid cluttering the render cycle of a component in
17 * need of the OTP code generation as it involves alot of
18 * re-rendering. eg: we do not want to re-render `Login.view`
19 * everytime the OTP countdown updates */
20 export const OTPValueControl: FC<Props> = ({ label, payload }) => {
21 const { generateOTP } = usePassCore();
22 const [otp, percent] = usePeriodicOtpCode({ generate: generateOTP, payload });
28 value={otp?.token ?? ''}
29 label={label ?? c('Label').t`2FA token (TOTP)`}
30 actions={<OTPDonut enabled={otp !== null} percent={percent} period={otp?.period} />}
32 <OTPValue code={otp?.token ?? ''} />