Merge pull request #395 from toger5/ts_profile
typescript `src/profile`
This commit is contained in:
commit
6d8e34762e
2 changed files with 67 additions and 18 deletions
|
@ -15,6 +15,8 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useCallback, useEffect, useState } from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
|
import { MatrixClient } from "matrix-js-sdk";
|
||||||
|
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import { useProfile } from "./useProfile";
|
import { useProfile } from "./useProfile";
|
||||||
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
|
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
|
||||||
|
@ -22,7 +24,12 @@ import { Modal, ModalContent } from "../Modal";
|
||||||
import { AvatarInputField } from "../input/AvatarInputField";
|
import { AvatarInputField } from "../input/AvatarInputField";
|
||||||
import styles from "./ProfileModal.module.css";
|
import styles from "./ProfileModal.module.css";
|
||||||
|
|
||||||
export function ProfileModal({ client, ...rest }) {
|
interface Props {
|
||||||
|
client: MatrixClient;
|
||||||
|
onClose: () => {};
|
||||||
|
[rest: string]: unknown;
|
||||||
|
}
|
||||||
|
export function ProfileModal({ client, ...rest }: Props) {
|
||||||
const { onClose } = rest;
|
const { onClose } = rest;
|
||||||
const {
|
const {
|
||||||
success,
|
success,
|
||||||
|
@ -50,13 +57,20 @@ export function ProfileModal({ client, ...rest }) {
|
||||||
(e) => {
|
(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const data = new FormData(e.target);
|
const data = new FormData(e.target);
|
||||||
const displayName = data.get("displayName");
|
const displayNameDataEntry = data.get("displayName");
|
||||||
const avatar = data.get("avatar");
|
const avatar: File | string = data.get("avatar");
|
||||||
|
|
||||||
|
const avatarSize =
|
||||||
|
typeof avatar == "string" ? avatar.length : avatar.size;
|
||||||
|
const displayName =
|
||||||
|
typeof displayNameDataEntry == "string"
|
||||||
|
? displayNameDataEntry
|
||||||
|
: displayNameDataEntry.name;
|
||||||
|
|
||||||
saveProfile({
|
saveProfile({
|
||||||
displayName,
|
displayName,
|
||||||
avatar: avatar && avatar.size > 0 ? avatar : undefined,
|
avatar: avatar && avatarSize > 0 ? avatar : undefined,
|
||||||
removeAvatar: removeAvatar && (!avatar || avatar.size === 0),
|
removeAvatar: removeAvatar && (!avatar || avatarSize === 0),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[saveProfile, removeAvatar]
|
[saveProfile, removeAvatar]
|
|
@ -14,11 +14,36 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
FileType,
|
||||||
|
MatrixClient,
|
||||||
|
MatrixEvent,
|
||||||
|
User,
|
||||||
|
UserEvent,
|
||||||
|
} from "matrix-js-sdk";
|
||||||
import { useState, useCallback, useEffect } from "react";
|
import { useState, useCallback, useEffect } from "react";
|
||||||
|
|
||||||
export function useProfile(client) {
|
interface ProfileLoadState {
|
||||||
|
success?: boolean;
|
||||||
|
loading?: boolean;
|
||||||
|
displayName: string;
|
||||||
|
avatarUrl: string;
|
||||||
|
error?: Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProfileSaveCallback = ({
|
||||||
|
displayName,
|
||||||
|
avatar,
|
||||||
|
removeAvatar,
|
||||||
|
}: {
|
||||||
|
displayName: string;
|
||||||
|
avatar: FileType;
|
||||||
|
removeAvatar: boolean;
|
||||||
|
}) => Promise<void>;
|
||||||
|
|
||||||
|
export function useProfile(client: MatrixClient) {
|
||||||
const [{ loading, displayName, avatarUrl, error, success }, setState] =
|
const [{ loading, displayName, avatarUrl, error, success }, setState] =
|
||||||
useState(() => {
|
useState<ProfileLoadState>(() => {
|
||||||
const user = client?.getUser(client.getUserId());
|
const user = client?.getUser(client.getUserId());
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -31,7 +56,10 @@ export function useProfile(client) {
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onChangeUser = (_event, { displayName, avatarUrl }) => {
|
const onChangeUser = (
|
||||||
|
_event: MatrixEvent,
|
||||||
|
{ displayName, avatarUrl }: User
|
||||||
|
) => {
|
||||||
setState({
|
setState({
|
||||||
success: false,
|
success: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -41,24 +69,24 @@ export function useProfile(client) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let user;
|
let user: User;
|
||||||
|
|
||||||
if (client) {
|
if (client) {
|
||||||
const userId = client.getUserId();
|
const userId = client.getUserId();
|
||||||
user = client.getUser(userId);
|
user = client.getUser(userId);
|
||||||
user.on("User.displayName", onChangeUser);
|
user.on(UserEvent.DisplayName, onChangeUser);
|
||||||
user.on("User.avatarUrl", onChangeUser);
|
user.on(UserEvent.AvatarUrl, onChangeUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (user) {
|
if (user) {
|
||||||
user.removeListener("User.displayName", onChangeUser);
|
user.removeListener(UserEvent.DisplayName, onChangeUser);
|
||||||
user.removeListener("User.avatarUrl", onChangeUser);
|
user.removeListener(UserEvent.AvatarUrl, onChangeUser);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [client]);
|
}, [client]);
|
||||||
|
|
||||||
const saveProfile = useCallback(
|
const saveProfile = useCallback<ProfileSaveCallback>(
|
||||||
async ({ displayName, avatar, removeAvatar }) => {
|
async ({ displayName, avatar, removeAvatar }) => {
|
||||||
if (client) {
|
if (client) {
|
||||||
setState((prev) => ({
|
setState((prev) => ({
|
||||||
|
@ -71,7 +99,7 @@ export function useProfile(client) {
|
||||||
try {
|
try {
|
||||||
await client.setDisplayName(displayName);
|
await client.setDisplayName(displayName);
|
||||||
|
|
||||||
let mxcAvatarUrl;
|
let mxcAvatarUrl: string;
|
||||||
|
|
||||||
if (removeAvatar) {
|
if (removeAvatar) {
|
||||||
await client.setAvatarUrl("");
|
await client.setAvatarUrl("");
|
||||||
|
@ -87,11 +115,11 @@ export function useProfile(client) {
|
||||||
loading: false,
|
loading: false,
|
||||||
success: true,
|
success: true,
|
||||||
}));
|
}));
|
||||||
} catch (error) {
|
} catch (error: unknown) {
|
||||||
setState((prev) => ({
|
setState((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
loading: false,
|
loading: false,
|
||||||
error,
|
error: error instanceof Error ? error : Error(error as string),
|
||||||
success: false,
|
success: false,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
@ -102,5 +130,12 @@ export function useProfile(client) {
|
||||||
[client]
|
[client]
|
||||||
);
|
);
|
||||||
|
|
||||||
return { loading, error, displayName, avatarUrl, saveProfile, success };
|
return {
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
displayName,
|
||||||
|
avatarUrl,
|
||||||
|
saveProfile,
|
||||||
|
success,
|
||||||
|
};
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue