<commit_before>import React from 'react';
import { c } from 'ttag';
import { authMember, privatizeMember, removeMember, updateRole } from 'proton-shared/lib/api/members';
import { revokeSessions } from 'proton-shared/lib/api/memberSessions';
import { APPS, isSSOMode, isStandaloneMode, MEMBER_PRIVATE, MEMBER_ROLE } from 'proton-shared/lib/constants';
import memberLogin from 'proton-shared/lib/authentication/memberLogin';
import { Address, Member, Organization, User as tsUser } from 'proton-shared/lib/interfaces';
import { noop } from 'proton-shared/lib/helpers/function';
import isTruthy from 'proton-shared/lib/helpers/isTruthy';
import { revoke } from 'proton-shared/lib/api/auth';
import {
    maybeResumeSessionByUser,
    persistSessionWithPassword,
} from 'proton-shared/lib/authentication/persistedSessionHelper';
import { getAppHref } from 'proton-shared/lib/apps/helper';
import { withUIDHeaders } from 'proton-shared/lib/fetch/headers';
import { getUser } from 'proton-shared/lib/api/user';
import { MemberAuthResponse } from 'proton-shared/lib/authentication/interface';
import { LoginTypes } from 'proton-shared/lib/authentication/LoginInterface';

import { DropdownActions } from '../../components';
import {
    useApi,
    useAuthentication,
    useEventManager,
    useLoading,
    useLoginType,
    useModals,
    useNotifications,
} from '../../hooks';

import EditMemberModal from './EditMemberModal';
import AuthModal from '../password/AuthModal';
import DeleteMemberModal from './DeleteMemberModal';

interface Props {
    member: Member;
    addresses: Address[];
    organization: Organization;
}

const MemberActions = ({ member, addresses = [], organization }: Props) => {
    const api = useApi();
    const { call } = useEventManager();
    const authentication = useAuthentication();
    const { createNotification } = useNotifications();
    const [loading, withLoading] = useLoading();
    const { createModal } = useModals();
    const loginType = useLoginType();

    const handleConfirmDelete = async () => {
        if (member.Role === MEMBER_ROLE.ORGANIZATION_OWNER) {
            await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_MEMBER));
        }
        await api(removeMember(member.ID));
        await call();
    };

    const login = async () => {
        const apiConfig = authMember(member.ID);
        const { UID, LocalID } = await new Promise((resolve, reject) => {
            createModal(
                <AuthModal<MemberAuthResponse>
                    onClose={reject}
                    onSuccess={({ result }) => resolve(result)}
                    config={apiConfig}
                />
            );
        });

        if (isSSOMode) {
            const memberApi = <T,>(config: any) => api<T>(withUIDHeaders(UID, config));
            const User = await memberApi<{ User: tsUser }>(getUser()).then(({ User }) => User);

            const done = (localID: number) => {
                const url = getAppHref('/overview', APPS.PROTONACCOUNT, localID);
                if (loginType === LoginTypes.TRANSIENT || loginType === LoginTypes.PERSISTENT_WORKAROUND) {
                    document.location.assign(url);
                } else {
                    window.open(url);
                }
            };

            const validatedSession = await maybeResumeSessionByUser(api, User);
            if (validatedSession) {
                memberApi(revoke()).catch(noop);
                done(validatedSession.LocalID);
                return;
            }

            await persistSessionWithPassword({
                api: memberApi,
                keyPassword: <PASSWORD>(),
                User,
                LocalID,
                UID,
            });
            done(LocalID);
            return;
        }

        if (isStandaloneMode) {
            return;
        }

        // Legacy mode
        const url = `${window.location.origin}/login/sub`;
        await memberLogin({ UID, mailboxPassword: <PASSWORD>(), url } as any);
    };

    const makeAdmin = async () => {
        await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_OWNER));
        await call();
        createNotification({ text: c('Success message').t`Role updated` });
    };

    const revokeAdmin = async () => {
        await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_MEMBER));
        await call();
        createNotification({ text: c('Success message').t`Role updated` });
    };

    const makePrivate = async () => {
        await api(privatizeMember(member.ID));
        await call();
        createNotification({ text: c('Success message').t`Status updated` });
    };

    const revokeMemberSessions = async () => {
        await api(revokeSessions(member.ID));
        await call();
        createNotification({ text: c('Success message').t`Sessions revoked` });
    };

    const canMakeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_MEMBER;
    const canDelete = !member.Self;
    const canEdit = organization.HasKeys;
    const canRevoke = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_OWNER;
    const canRevokeSessions = !member.Self;

    const canLogin =
        !member.Self && member.Private === MEMBER_PRIVATE.READABLE && member.Keys.length && addresses.length;
    const canMakePrivate = member.Private === MEMBER_PRIVATE.READABLE;

    const openEdit = () => {
        createModal(<EditMemberModal member={member} onClose={noop} />);
    };

    const openDelete = async () => {
        await new Promise((resolve, reject) => {
            createModal(<DeleteMemberModal member={member} onConfirm={resolve} onClose={reject} />);
        });
        await withLoading(handleConfirmDelete());
        createNotification({ text: c('Success message').t`User deleted` });
    };

    const list = [
        canEdit && {
            text: c('Member action').t`Edit`,
            onClick: openEdit,
        },
        canDelete &&
            ({
                text: c('Member action').t`Delete`,
                actionType: 'delete',
                onClick: openDelete,
            } as const),
        canMakeAdmin && {
            text: c('Member action').t`Make admin`,
            onClick: () => withLoading(makeAdmin()),
        },
        canRevoke && {
            text: c('Member action').t`Revoke admin`,
            onClick: () => withLoading(revokeAdmin()),
        },
        canLogin && {
            text: c('Member action').t`Sign in`,
            onClick: login,
        },<commit_msg>Fix void promise<commit_after>import React from 'react';
import { c } from 'ttag';
import { authMember, privatizeMember, removeMember, updateRole } from 'proton-shared/lib/api/members';
import { revokeSessions } from 'proton-shared/lib/api/memberSessions';
import { APPS, isSSOMode, isStandaloneMode, MEMBER_PRIVATE, MEMBER_ROLE } from 'proton-shared/lib/constants';
import memberLogin from 'proton-shared/lib/authentication/memberLogin';
import { Address, Member, Organization, User as tsUser } from 'proton-shared/lib/interfaces';
import { noop } from 'proton-shared/lib/helpers/function';
import isTruthy from 'proton-shared/lib/helpers/isTruthy';
import { revoke } from 'proton-shared/lib/api/auth';
import {
    maybeResumeSessionByUser,
    persistSessionWithPassword,
} from 'proton-shared/lib/authentication/persistedSessionHelper';
import { getAppHref } from 'proton-shared/lib/apps/helper';
import { withUIDHeaders } from 'proton-shared/lib/fetch/headers';
import { getUser } from 'proton-shared/lib/api/user';
import { MemberAuthResponse } from 'proton-shared/lib/authentication/interface';
import { LoginTypes } from 'proton-shared/lib/authentication/LoginInterface';

import { DropdownActions } from '../../components';
import {
    useApi,
    useAuthentication,
    useEventManager,
    useLoading,
    useLoginType,
    useModals,
    useNotifications,
} from '../../hooks';

import EditMemberModal from './EditMemberModal';
import AuthModal from '../password/AuthModal';
import DeleteMemberModal from './DeleteMemberModal';

interface Props {
    member: Member;
    addresses: Address[];
    organization: Organization;
}

const MemberActions = ({ member, addresses = [], organization }: Props) => {
    const api = useApi();
    const { call } = useEventManager();
    const authentication = useAuthentication();
    const { createNotification } = useNotifications();
    const [loading, withLoading] = useLoading();
    const { createModal } = useModals();
    const loginType = useLoginType();

    const handleConfirmDelete = async () => {
        if (member.Role === MEMBER_ROLE.ORGANIZATION_OWNER) {
            await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_MEMBER));
        }
        await api(removeMember(member.ID));
        await call();
    };

    const login = async () => {
        const apiConfig = authMember(member.ID);
        const { UID, LocalID } = await new Promise((resolve, reject) => {
            createModal(
                <AuthModal<MemberAuthResponse>
                    onClose={reject}
                    onSuccess={({ result }) => resolve(result)}
                    config={apiConfig}
                />
            );
        });

        if (isSSOMode) {
            const memberApi = <T,>(config: any) => api<T>(withUIDHeaders(UID, config));
            const User = await memberApi<{ User: tsUser }>(getUser()).then(({ User }) => User);

            const done = (localID: number) => {
                const url = getAppHref('/overview', APPS.PROTONACCOUNT, localID);
                if (loginType === LoginTypes.TRANSIENT || loginType === LoginTypes.PERSISTENT_WORKAROUND) {
                    document.location.assign(url);
                } else {
                    window.open(url);
                }
            };

            const validatedSession = await maybeResumeSessionByUser(api, User);
            if (validatedSession) {
                memberApi(revoke()).catch(noop);
                done(validatedSession.LocalID);
                return;
            }

            await persistSessionWithPassword({
                api: memberApi,
                keyPassword: <PASSWORD>(),
                User,
                LocalID,
                UID,
            });
            done(LocalID);
            return;
        }

        if (isStandaloneMode) {
            return;
        }

        // Legacy mode
        const url = `${window.location.origin}/login/sub`;
        await memberLogin({ UID, mailboxPassword: <PASSWORD>(), url } as any);
    };

    const makeAdmin = async () => {
        await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_OWNER));
        await call();
        createNotification({ text: c('Success message').t`Role updated` });
    };

    const revokeAdmin = async () => {
        await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_MEMBER));
        await call();
        createNotification({ text: c('Success message').t`Role updated` });
    };

    const makePrivate = async () => {
        await api(privatizeMember(member.ID));
        await call();
        createNotification({ text: c('Success message').t`Status updated` });
    };

    const revokeMemberSessions = async () => {
        await api(revokeSessions(member.ID));
        await call();
        createNotification({ text: c('Success message').t`Sessions revoked` });
    };

    const canMakeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_MEMBER;
    const canDelete = !member.Self;
    const canEdit = organization.HasKeys;
    const canRevoke = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_OWNER;
    const canRevokeSessions = !member.Self;

    const canLogin =
        !member.Self && member.Private === MEMBER_PRIVATE.READABLE && member.Keys.length && addresses.length;
    const canMakePrivate = member.Private === MEMBER_PRIVATE.READABLE;

    const openEdit = () => {
        createModal(<EditMemberModal member={member} onClose={noop} />);
    };

    const openDelete = async () => {
        await new Promise<void>((resolve, reject) => {
            createModal(<DeleteMemberModal member={member} onConfirm={resolve} onClose={reject} />);
        });
        await withLoading(handleConfirmDelete());
        createNotification({ text: c('Success message').t`User deleted` });
    };

    const list = [
        canEdit && {
            text: c('Member action').t`Edit`,
            onClick: openEdit,
        },
        canDelete &&
            ({
                text: c('Member action').t`Delete`,
                actionType: 'delete',
                onClick: openDelete,
            } as const),
        canMakeAdmin && {
            text: c('Member action').t`Make admin`,
            onClick: () => withLoading(makeAdmin()),
        },
        canRevoke && {
            text: c('Member action').t`Revoke admin`,
            onClick: () => withLoading(revokeAdmin()),
        },
        canLogin && {
            text: c('Member action').t`Sign in`,
            onClick: login,
        },
{'fraction_non_alphanumeric': 0.13027171833141982, 'fraction_numerical': 0.0, 'mean_word_length': 2.432098765432099, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5213929', 'n_tokens_mistral': 3646, 'n_tokens_neox': 3587, 'n_words': 772}