1 import { type FC, useCallback, useMemo, useRef, useState } from 'react';
3 import { c } from 'ttag';
5 import { Button } from '@proton/atoms';
14 } from '@proton/components';
15 import { Card } from '@proton/pass/components/Layout/Card/Card';
16 import type { Maybe, MaybePromise } from '@proton/pass/types';
18 export type PasswordModalProps = {
26 type: 'new-password' | 'current-password';
29 onSubmit?: (password: string) => MaybePromise<void>;
30 onValidate?: (password: string) => Maybe<string>;
33 export const PasswordModal: FC<PasswordModalProps> = ({
47 const touched = useRef(false);
48 const [password, setPassword] = useState('');
49 const error = useMemo(() => onValidate?.(password), [password, onValidate]);
51 const onValue = useCallback((value: string) => {
52 touched.current = true;
61 onSubmit={() => !error && onSubmit?.(password)}
65 <ModalTwoHeader title={title} closeButtonProps={{ pill: true, disabled: loading }} />
68 <Card className="mb-4 text-sm" type="primary">
78 error={touched.current ? error : undefined}
80 label={label ?? c('Label').t`Password`}
82 placeholder={placeholder ?? c('Placeholder').t`Password`}
87 {warning && <div className="mt-4 mb-4 text-sm color-danger">{warning}</div>}
90 <Button color="danger" shape="outline" onClick={onClose} disabled={loading}>
91 {c('Action').t`Cancel`}
93 <Button type="submit" color="norm" loading={loading} disabled={loading || error !== undefined}>
94 {submitLabel ?? c('Action').t`Authenticate`}