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

Commit

Permalink
Update status message in the member list and user info panel when it …
Browse files Browse the repository at this point in the history
…is changed (#7338)

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
SimonBrandner and t3chguy authored Dec 13, 2021
1 parent a46de3b commit 43499b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
15 changes: 3 additions & 12 deletions src/components/views/right_panel/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import createRoom, { findDMForUser, privateShouldBeEncrypted } from '../../../cr
import DMRoomMap from '../../../utils/DMRoomMap';
import AccessibleButton from '../elements/AccessibleButton';
import SdkConfig from '../../../SdkConfig';
import SettingsStore from "../../../settings/SettingsStore";
import RoomViewStore from "../../../stores/RoomViewStore";
import MultiInviter from "../../../utils/MultiInviter";
import GroupStore from "../../../stores/GroupStore";
Expand Down Expand Up @@ -76,6 +75,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 @@ -1495,13 +1495,14 @@ const BasicUserInfo: React.FC<{
</React.Fragment>;
};

type Member = User | RoomMember | GroupMember;
export type Member = User | RoomMember | GroupMember;

const UserInfoHeader: React.FC<{
member: Member;
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 +1540,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 Down
4 changes: 2 additions & 2 deletions 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);
}
}

Expand All @@ -93,7 +93,7 @@ export default class MemberTile extends React.Component<IProps, IState> {
const { user } = this.props.member;
if (user) {
user.removeListener(
"User._unstable_statusMessage",
"User.unstable_statusMessage",
this.onStatusMessageCommitted,
);
}
Expand Down
38 changes: 38 additions & 0 deletions src/hooks/useUserStatusMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 { MatrixClient } from "matrix-js-sdk/src/client";
import { User } from "matrix-js-sdk/src/models/user";
import { useContext } from "react";

import MatrixClientContext from "../contexts/MatrixClientContext";
import { useEventEmitterState } from "./useEventEmitter";
import { Member } from "../components/views/right_panel/UserInfo";
import { useFeatureEnabled } from "./useSettings";

const getUser = (cli: MatrixClient, user: Member): User => cli.getUser(user?.userId);
const getStatusMessage = (cli: MatrixClient, user: Member): string => {
return getUser(cli, user).unstable_statusMessage;
};

// Hook to simplify handling Matrix User status
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);
});
};

0 comments on commit 43499b9

Please sign in to comment.