typescript src/profile
This commit is contained in:
parent
69cfa1db6d
commit
785eca7289
2 changed files with 34 additions and 9 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,
|
||||||
|
@ -51,12 +58,13 @@ export function ProfileModal({ client, ...rest }) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const data = new FormData(e.target);
|
const data = new FormData(e.target);
|
||||||
const displayName = data.get("displayName");
|
const displayName = data.get("displayName");
|
||||||
const avatar = data.get("avatar");
|
const avatar: File | string = data.get("avatar");
|
||||||
|
const avatarSize =
|
||||||
|
typeof avatar == "string" ? avatar.length : avatar.size;
|
||||||
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,9 +14,26 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { MatrixClient, User } from "matrix-js-sdk";
|
||||||
import { useState, useCallback, useEffect } from "react";
|
import { useState, useCallback, useEffect } from "react";
|
||||||
|
|
||||||
export function useProfile(client) {
|
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 {
|
||||||
const [{ loading, displayName, avatarUrl, error, success }, setState] =
|
const [{ loading, displayName, avatarUrl, error, success }, setState] =
|
||||||
useState(() => {
|
useState(() => {
|
||||||
const user = client?.getUser(client.getUserId());
|
const user = client?.getUser(client.getUserId());
|
||||||
|
@ -31,7 +48,7 @@ export function useProfile(client) {
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onChangeUser = (_event, { displayName, avatarUrl }) => {
|
const onChangeUser = (_event: any, { displayName, avatarUrl }: any) => {
|
||||||
setState({
|
setState({
|
||||||
success: false,
|
success: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -41,7 +58,7 @@ export function useProfile(client) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let user;
|
let user: User;
|
||||||
|
|
||||||
if (client) {
|
if (client) {
|
||||||
const userId = client.getUserId();
|
const userId = client.getUserId();
|
||||||
|
@ -71,7 +88,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("");
|
Loading…
Add table
Reference in a new issue