Skip to content

Commit

Permalink
review: Quick refactor, better handling of action on button click
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Jan 29, 2025
1 parent 6b76fbc commit d1509b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
36 changes: 20 additions & 16 deletions src/components/viewmodels/rooms/UserIdentityWarningViewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { logger } from "matrix-js-sdk/src/logger";

import { useMatrixClientContext } from "../../../contexts/MatrixClientContext.tsx";
import { useTypedEventEmitter } from "../../../hooks/useEventEmitter.ts";
import { ButtonEvent } from "../../views/elements/AccessibleButton.tsx";

export type ViolationType = "PinViolation" | "VerificationViolation";

Expand All @@ -34,9 +33,16 @@ export type ViolationPrompt = {
*/
export interface UserIdentityWarningState {
currentPrompt?: ViolationPrompt;
onButtonClick: (ev: ButtonEvent) => void;
dispatchAction: (action: UserIdentityWarningViewModelAction) => void;
}

/**
* List of actions that can be dispatched to the UserIdentityWarningViewModel.
*/
export type UserIdentityWarningViewModelAction =
| { type: "PinUserIdentity"; userId: string }
| { type: "WithdrawVerification"; userId: string };

/**
* Maps a list of room members to a list of violations.
* Checks for all members in the room to see if they have any violations.
Expand Down Expand Up @@ -161,28 +167,26 @@ export function useUserIdentityWarningViewModel(room: Room, key: string): UserId
});
}, [loadViolations]);

let onButtonClick: (ev: ButtonEvent) => void = () => {};
if (currentPrompt) {
onButtonClick = (ev: ButtonEvent): void => {
// XXX do we want some posthog tracking?
const dispatchAction = useCallback(
(action: UserIdentityWarningViewModelAction): void => {
if (!crypto) {
return;
}
ev.preventDefault();
if (currentPrompt.type === "VerificationViolation") {
crypto.withdrawVerificationRequirement(currentPrompt.member.userId).catch((e) => {
logger.error("Error withdrawing verification requirement:", e);
});
} else if (currentPrompt.type === "PinViolation") {
crypto.pinCurrentUserIdentity(currentPrompt.member.userId).catch((e) => {
if (action.type === "PinUserIdentity") {
crypto.pinCurrentUserIdentity(action.userId).catch((e) => {
logger.error("Error pinning user identity:", e);
});
} else if (action.type === "WithdrawVerification") {
crypto.withdrawVerificationRequirement(action.userId).catch((e) => {
logger.error("Error withdrawing verification requirement:", e);
});
}
};
}
},
[crypto],
);

return {
currentPrompt,
onButtonClick,
dispatchAction,
};
}
10 changes: 9 additions & 1 deletion src/components/views/rooms/UserIdentityWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ interface UserIdentityWarningProps {
* button to acknowledge the change.
*/
export const UserIdentityWarning: React.FC<UserIdentityWarningProps> = ({ room }) => {
const { currentPrompt, onButtonClick } = useUserIdentityWarningViewModel(room, room.roomId);
const { currentPrompt, dispatchAction } = useUserIdentityWarningViewModel(room, room.roomId);

if (!currentPrompt) return null;

const [title, action] = getTitleAndAction(currentPrompt);

const onButtonClick = (ev: ButtonEvent): void => {
ev.preventDefault();
if (currentPrompt.type === "VerificationViolation") {
dispatchAction({ type: "WithdrawVerification", userId: currentPrompt.member.userId });
} else {
dispatchAction({ type: "PinUserIdentity", userId: currentPrompt.member.userId });
}
};
return warningBanner(
currentPrompt.type === "VerificationViolation",
memberAvatar(currentPrompt.member),
Expand Down

0 comments on commit d1509b9

Please sign in to comment.