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
16 changes: 10 additions & 6 deletions src/hooks/useUserStatusMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { RoomMember } from "matrix-js-sdk";
import { MatrixClient, RoomMember } from "matrix-js-sdk";
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
import { User } from "matrix-js-sdk/src/models/user";
import { useContext } from "react";

import { GroupMember } from "../components/views/right_panel/UserInfo";
import { MatrixClientPeg } from "../MatrixClientPeg";
import MatrixClientContext from "../contexts/MatrixClientContext";
import { useEventEmitterState } 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);
const getStatusMessage = (user: StatusMessageUser): string => getUser(user).unstable_statusMessage;
const getUser = (cli: MatrixClient, user: StatusMessageUser): User => cli.getUser(user?.userId);
const getStatusMessage = (cli: MatrixClient, user: StatusMessageUser): string => {
return getUser(cli, user).unstable_statusMessage;
};

// Hook to simplify handling Matrix User status
export const useUserStatusMessage = (user?: StatusMessageUser): string => {
return useEventEmitterState(getUser(user), "User.unstable_statusMessage", () => {
return getStatusMessage(user);
const cli = useContext(MatrixClientContext);
return useEventEmitterState(getUser(cli, user), "User.unstable_statusMessage", () => {
return getStatusMessage(cli, user);
});
};