2022-05-04 17:09:48 +01:00
|
|
|
/*
|
|
|
|
Copyright 2022 Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
import { MatrixClient, User } from "matrix-js-sdk";
|
2022-01-05 17:19:03 -08:00
|
|
|
import { useState, useCallback, useEffect } from "react";
|
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
interface ProfileResult {
|
|
|
|
loading: boolean;
|
|
|
|
error: Error;
|
|
|
|
displayName: string;
|
|
|
|
avatarUrl: string;
|
|
|
|
saveProfile: ({
|
|
|
|
displayName,
|
|
|
|
avatar,
|
|
|
|
removeAvatar,
|
|
|
|
}: {
|
|
|
|
displayName: string;
|
|
|
|
avatar: any;
|
|
|
|
removeAvatar: boolean;
|
|
|
|
}) => Promise<void>;
|
|
|
|
success: boolean;
|
|
|
|
}
|
|
|
|
export function useProfile(client: MatrixClient): ProfileResult {
|
2022-01-05 17:19:03 -08:00
|
|
|
const [{ loading, displayName, avatarUrl, error, success }, setState] =
|
|
|
|
useState(() => {
|
|
|
|
const user = client?.getUser(client.getUserId());
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
loading: false,
|
2022-02-23 16:41:12 -08:00
|
|
|
displayName: user?.rawDisplayName,
|
2022-05-18 19:00:59 -04:00
|
|
|
avatarUrl: user?.avatarUrl,
|
2022-01-05 17:19:03 -08:00
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-06 22:33:13 +02:00
|
|
|
const onChangeUser = (_event: any, { displayName, avatarUrl }: any) => {
|
2022-01-05 17:19:03 -08:00
|
|
|
setState({
|
|
|
|
success: false,
|
|
|
|
loading: false,
|
|
|
|
displayName,
|
2022-05-18 19:00:59 -04:00
|
|
|
avatarUrl,
|
2022-01-05 17:19:03 -08:00
|
|
|
error: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
let user: User;
|
2022-01-05 17:19:03 -08:00
|
|
|
|
|
|
|
if (client) {
|
|
|
|
const userId = client.getUserId();
|
|
|
|
user = client.getUser(userId);
|
|
|
|
user.on("User.displayName", onChangeUser);
|
|
|
|
user.on("User.avatarUrl", onChangeUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (user) {
|
|
|
|
user.removeListener("User.displayName", onChangeUser);
|
|
|
|
user.removeListener("User.avatarUrl", onChangeUser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [client]);
|
|
|
|
|
|
|
|
const saveProfile = useCallback(
|
2022-02-18 16:02:27 -08:00
|
|
|
async ({ displayName, avatar, removeAvatar }) => {
|
2022-01-05 17:19:03 -08:00
|
|
|
if (client) {
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
loading: true,
|
|
|
|
error: null,
|
|
|
|
success: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.setDisplayName(displayName);
|
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
let mxcAvatarUrl: string;
|
2022-01-05 17:19:03 -08:00
|
|
|
|
2022-02-18 16:02:27 -08:00
|
|
|
if (removeAvatar) {
|
|
|
|
await client.setAvatarUrl("");
|
|
|
|
} else if (avatar) {
|
2022-01-05 17:19:03 -08:00
|
|
|
mxcAvatarUrl = await client.uploadContent(avatar);
|
|
|
|
await client.setAvatarUrl(mxcAvatarUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
displayName,
|
2022-05-18 19:00:59 -04:00
|
|
|
avatarUrl: removeAvatar ? null : mxcAvatarUrl ?? prev.avatarUrl,
|
2022-01-05 17:19:03 -08:00
|
|
|
loading: false,
|
|
|
|
success: true,
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
loading: false,
|
|
|
|
error,
|
|
|
|
success: false,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error("Client not initialized before calling saveProfile");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[client]
|
|
|
|
);
|
|
|
|
|
|
|
|
return { loading, error, displayName, avatarUrl, saveProfile, success };
|
|
|
|
}
|