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

Update status message in the member list and user info panel when it is changed #7338

Merged
merged 11 commits into from
Dec 13, 2021
14 changes: 3 additions & 11 deletions src/components/views/right_panel/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { bulkSpaceBehaviour } from "../../../utils/space";
import { shouldShowComponent } from "../../../customisations/helpers/UIComponents";
import { UIComponent } from "../../../settings/UIFeature";
import { TimelineRenderingType } from "../../../contexts/RoomContext";
import { useUserStatusMessage } from "../../../hooks/useUserStatusMessage";

export interface IDevice {
deviceId: string;
Expand Down Expand Up @@ -1502,6 +1503,7 @@ const UserInfoHeader: React.FC<{
e2eStatus: E2EStatus;
}> = ({ member, e2eStatus }) => {
const cli = useContext(MatrixClientContext);
const statusMessage = useUserStatusMessage(member);

const onMemberAvatarClick = useCallback(() => {
const avatarUrl = (member as RoomMember).getMxcAvatarUrl
Expand Down Expand Up @@ -1539,20 +1541,10 @@ const UserInfoHeader: React.FC<{
let presenceState;
let presenceLastActiveAgo;
let presenceCurrentlyActive;
let statusMessage;

if (member instanceof RoomMember && member.user) {
presenceState = member.user.presence;
presenceLastActiveAgo = member.user.lastActiveAgo;
presenceCurrentlyActive = member.user.currentlyActive;

if (SettingsStore.getValue("feature_custom_status")) {
if ((member as RoomMember).user) {
statusMessage = member.user.unstable_statusMessage;
} else {
statusMessage = (member as unknown as User).unstable_statusMessage;
}
}
}

const enablePresenceByHsUrl = SdkConfig.get()["enable_presence_by_hs_url"];
Expand All @@ -1573,7 +1565,7 @@ const UserInfoHeader: React.FC<{
}

let statusLabel = null;
if (statusMessage) {
if (SettingsStore.getValue("feature_custom_status") && statusMessage) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the hook should watch SettingsStore.getValue("feature_custom_status") instead, handling even more de-duplication of code. Also means it can not set up event emitter handles when the feature is disabled (if you pass a falsey eventEmitter as the first arg into a useEventEmitter* func it will no-op)

Let me know if that needs a better explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like this be sufficient?

export const useUserStatusMessage = (user?: Member): string => {
    const cli = useContext(MatrixClientContext);
    const enabled = useFeatureEnabled("feature_custom_status");
    return useEventEmitterState(enabled && getUser(cli, user), "User.unstable_statusMessage", () => {
        return getStatusMessage(cli, user);
    });
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, thats what I had in mind, though would be worth a test :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems to work though it doesn't hide the statuses from the right panel when I disable the feature, which I assumed it would... Should it do that?

statusLabel = <span className="mx_UserInfo_statusMessage">{ statusMessage }</span>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/views/rooms/MemberTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class MemberTile extends React.Component<IProps, IState> {
if (SettingsStore.getValue("feature_custom_status")) {
const { user } = this.props.member;
if (user) {
user.on("User._unstable_statusMessage", this.onStatusMessageCommitted);
user.on("User.unstable_statusMessage", this.onStatusMessageCommitted);
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/hooks/useUserStatusMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { RoomMember } from "matrix-js-sdk";
import { User } from "matrix-js-sdk/src/models/user";
import { useCallback, useEffect, useState } from "react";

import { GroupMember } from "../components/views/right_panel/UserInfo";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { useEventEmitter } from "./useEventEmitter";

type StatusMessageUser = User | RoomMember | GroupMember;
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved

const getUser = (user: StatusMessageUser): User => MatrixClientPeg.get().getUser(user?.userId);
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
const getStatusMessage = (user: StatusMessageUser): string => getUser(user).unstable_statusMessage;

// Hook to simplify handling Matrix User status
export const useUserStatusMessage = (user?: StatusMessageUser): string => {
const [value, setValue] = useState<string>(getStatusMessage(user));

const update = useCallback(() => {
if (!user) return;
setValue(getStatusMessage(user));
}, [user]);

useEventEmitter(getUser(user), "User.unstable_statusMessage", update);
useEffect(() => {
update();
return () => {
setValue(undefined);
};
}, [update]);
return value;
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
};