Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
use pendingUpdateCount rather, so we don't show multiple spinners
Browse files Browse the repository at this point in the history
  • Loading branch information
bwindels committed Mar 4, 2020
1 parent 29af8ad commit afc7273
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/components/views/right_panel/UserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, {useCallback, useMemo, useState, useEffect, useContext} from 'react';
import React, {useCallback, useMemo, useState, useEffect, useContext, useRef} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {Group, RoomMember, User} from 'matrix-js-sdk';
Expand Down Expand Up @@ -136,25 +136,28 @@ function useIsEncrypted(cli, room) {
return isEncrypted;
}

function useHasCrossSigningKeys(cli, member, canVerify) {
const [waitingForCrossSigningKeys, setWaitingForCrossSigningKeys] = useState(false);
const [hasCrossSigningKeys, setHasCrossSigningKeys] = useState(false);
function useHasCrossSigningKeys(cli, member, canVerify, setUpdating) {
// use a ref to setUpdating because we don't want to rerun
// the useAsyncMemo hook when it changes.
const updatingRef = useRef();
useEffect(() => {
if (canVerify) {
setWaitingForCrossSigningKeys(true);
(async () => {
try {
await cli.downloadKeys([member.userId]);
const xsi = cli.getStoredCrossSigningForUser(member.userId);
const key = xsi && xsi.getId();
setHasCrossSigningKeys(!!key);
} finally {
setWaitingForCrossSigningKeys(false);
}
})();
updatingRef.current = setUpdating;
}, [setUpdating]);

return useAsyncMemo(async () => {
if (!canVerify) {
return false;
}
updatingRef.current(true);
try {
await cli.downloadKeys([member.userId]);
const xsi = cli.getStoredCrossSigningForUser(member.userId);
const key = xsi && xsi.getId();
return !!key;
} finally {
updatingRef.current(false);
}
}, [canVerify, cli, member]);
return {waitingForCrossSigningKeys, hasCrossSigningKeys};
}, [cli, member, canVerify], false);
}

async function verifyDevice(userId, device) {
Expand Down Expand Up @@ -1353,20 +1356,18 @@ const BasicUserInfo = ({room, member, groupId, devices, isRoomEncrypted}) => {
homeserverSupportsCrossSigning &&
isRoomEncrypted && !userVerified && !isMe;

const {hasCrossSigningKeys, waitingForCrossSigningKeys} =
useHasCrossSigningKeys(cli, member, canVerify);
const setUpdating = useCallback((updating) => {
setPendingUpdateCount(pendingUpdateCount + (updating ? 1 : -1));
}, [setPendingUpdateCount, pendingUpdateCount]);
const hasCrossSigningKeys =
useHasCrossSigningKeys(cli, member, canVerify, setUpdating );

if (canVerify) {
if (hasCrossSigningKeys) {
verifyButton = (
<AccessibleButton className="mx_UserInfo_field" onClick={() => verifyUser(member)}>
{_t("Verify")}
</AccessibleButton>
);
} else if (waitingForCrossSigningKeys) {
const Spinner = sdk.getComponent("elements.Spinner");
verifyButton = <Spinner />;
}
if (canVerify && hasCrossSigningKeys) {
verifyButton = (
<AccessibleButton className="mx_UserInfo_field" onClick={() => verifyUser(member)}>
{_t("Verify")}
</AccessibleButton>
);
}

let devicesSection;
Expand Down

0 comments on commit afc7273

Please sign in to comment.