The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { getTwoFactorAuthenticationErrorToastOptions } from '@/auth/utils/getTwoFactorAuthenticationErrorToastOptions';
import { useApolloAdminClient } from '@/settings/admin-panel/apollo/hooks/useApolloAdminClient';
import { TwoFactorAuthenticationRecoveryCodeConfirmationDialog } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationRecoveryCodeConfirmationDialog';
import { TwoFactorAuthenticationRecoveryCodeDisplay } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationRecoveryCodeDisplay';
import { useGeneratedRecoveryCode } from '@/settings/two-factor-authentication/hooks/useGeneratedRecoveryCode';
import { useDialog } from '@/ui/layout/dialog/hooks/useDialog';
import { useMutation } from '@apollo/client/react';
import { styled } from '@linaria/react';
import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { IconKey } from 'twenty-ui/icon';
import { Button } from 'twenty-ui/primitives/input';
import { GenerateTwoFactorAuthenticationRecoveryCodeAsServerAdminDocument } from '~/generated-admin/graphql';
import { useLingui } from '@lingui/react/macro';
const StyledRecoveryCodeContainer = styled.div`
width: 100%;
`;
type SettingsAdminTwoFactorAuthenticationRecoveryCodeProps = {
userId: string;
workspaceId: string;
memberName: string;
};
export const SettingsAdminTwoFactorAuthenticationRecoveryCode = ({
userId,
workspaceId,
memberName,
}: SettingsAdminTwoFactorAuthenticationRecoveryCodeProps) => {
const { t } = useLingui();
const dialogId = `admin-two-factor-authentication-recovery-code-${userId}-${workspaceId}`;
const apolloAdminClient = useApolloAdminClient();
const { openDialog } = useDialog();
const { enqueueToast } = useToast();
const {
generatedRecoveryCode,
showGeneratedRecoveryCode,
clearGeneratedRecoveryCode,
} = useGeneratedRecoveryCode();
const [generateRecoveryCode] = useMutation(
GenerateTwoFactorAuthenticationRecoveryCodeAsServerAdminDocument,
{ client: apolloAdminClient },
);
const handleGenerate = async (otp: string) => {
try {
const result = await generateRecoveryCode({
variables: {
userId,
workspaceId,
otp: isNonEmptyString(otp) ? otp : undefined,
},
});
const recoveryCode =
result.data?.generateTwoFactorAuthenticationRecoveryCodeAsServerAdmin;
if (isDefined(recoveryCode)) {
showGeneratedRecoveryCode(recoveryCode);
}
} catch (error) {
enqueueToast(
getTwoFactorAuthenticationErrorToastOptions({
error,
fallbackMessage: t`Failed to generate a recovery code.`,
}),
);
}
};
return (
<>
<Button
startIcon={<IconKey />}
variant="outline"
onClick={() => openDialog(dialogId)}
>{t`Generate 2FA recovery code`}</Button>
{isDefined(generatedRecoveryCode) && (
<StyledRecoveryCodeContainer>
<TwoFactorAuthenticationRecoveryCodeDisplay
recoveryCode={generatedRecoveryCode.recoveryCode}
expiresAt={generatedRecoveryCode.expiresAt}
memberName={memberName}
onExpire={clearGeneratedRecoveryCode}
/>
</StyledRecoveryCodeContainer>
)}
<TwoFactorAuthenticationRecoveryCodeConfirmationDialog
dialogId={dialogId}
memberName={memberName}
onConfirm={handleGenerate}
/>
</>
);
};