Files
ThreadNet-Web/src/components/views/settings/ChangeAvatar.tsx
T

206 lines
6.0 KiB
TypeScript
Raw Normal View History

2015-09-21 17:23:51 +01:00
/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
2021-08-14 11:04:36 +02:00
Copyright 2021 New Vector Ltd
2015-09-21 17:23:51 +01:00
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 React from 'react';
2021-08-16 09:19:58 +01:00
import { MatrixEvent, Room } from 'matrix-js-sdk/src';
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
2017-06-08 12:33:29 +01:00
import { _t } from '../../../languageHandler';
2020-10-30 18:18:17 +00:00
import Spinner from '../elements/Spinner';
2021-06-29 13:11:58 +01:00
import { replaceableComponent } from "../../../utils/replaceableComponent";
import { mediaFromMxc } from "../../../customisations/Media";
import RoomAvatar from '../avatars/RoomAvatar';
import BaseAvatar from '../avatars/BaseAvatar';
2021-08-14 11:04:36 +02:00
interface IProps {
initialAvatarUrl?: string;
room?: Room;
// if false, you need to call changeAvatar.onFileSelected yourself.
showUploadSection?: boolean;
width?: number;
height?: number;
className?: string;
}
interface IState {
avatarUrl?: string;
errorText?: string;
phase?: Phases;
}
enum Phases {
Display = "display",
Uploading = "uploading",
Error = "error",
}
2015-09-21 17:23:51 +01:00
@replaceableComponent("views.settings.ChangeAvatar")
2021-08-14 11:04:36 +02:00
export default class ChangeAvatar extends React.Component<IProps, IState> {
public static defaultProps = {
2020-08-29 12:14:16 +01:00
showUploadSection: true,
className: "",
width: 80,
height: 80,
};
2015-12-23 16:52:59 +00:00
2021-08-14 11:04:36 +02:00
private avatarSet = false;
constructor(props: IProps) {
2020-08-29 12:14:16 +01:00
super(props);
this.state = {
2015-09-21 17:23:51 +01:00
avatarUrl: this.props.initialAvatarUrl,
2021-08-14 11:04:36 +02:00
phase: Phases.Display,
2017-01-20 14:22:27 +00:00
};
2020-08-29 12:14:16 +01:00
}
2015-09-21 17:23:51 +01:00
2021-08-14 11:04:36 +02:00
public componentDidMount(): void {
2017-10-13 20:57:15 -06:00
MatrixClientPeg.get().on("RoomState.events", this.onRoomStateEvents);
2020-08-29 12:14:16 +01:00
}
2017-10-13 20:57:15 -06:00
2020-04-01 14:35:39 -06:00
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
2021-08-16 09:16:02 +01:00
// eslint-disable-next-line
public UNSAFE_componentWillReceiveProps(newProps): void {
if (this.avatarSet) {
// don't clobber what the user has just set
return;
}
this.setState({
2017-10-11 17:56:17 +01:00
avatarUrl: newProps.initialAvatarUrl,
});
2020-08-29 12:14:16 +01:00
}
2021-08-14 11:04:36 +02:00
public componentWillUnmount(): void {
2017-10-13 20:57:15 -06:00
if (MatrixClientPeg.get()) {
MatrixClientPeg.get().removeListener("RoomState.events", this.onRoomStateEvents);
}
2020-08-29 12:14:16 +01:00
}
2017-10-13 20:57:15 -06:00
2021-08-14 11:04:36 +02:00
public onRoomStateEvents = (ev: MatrixEvent) => {
2017-10-13 21:13:32 -06:00
if (!this.props.room) {
return;
}
2017-10-13 20:57:15 -06:00
if (ev.getRoomId() !== this.props.room.roomId || ev.getType() !== 'm.room.avatar'
|| ev.getSender() !== MatrixClientPeg.get().getUserId()) {
return;
}
if (!ev.getContent().url) {
this.avatarSet = false;
this.setState({}); // force update
}
2020-08-29 12:14:16 +01:00
};
2017-10-13 20:57:15 -06:00
2021-08-14 11:04:36 +02:00
public setAvatarFromFile(file): Promise<{}> {
2017-10-11 17:56:17 +01:00
let newUrl = null;
2015-09-21 17:23:51 +01:00
this.setState({
2021-08-14 11:04:36 +02:00
phase: Phases.Uploading,
2015-09-21 17:23:51 +01:00
});
2021-08-14 11:04:36 +02:00
const httpPromise = MatrixClientPeg.get().uploadContent(file).then((url) => {
2015-09-21 17:23:51 +01:00
newUrl = url;
2021-08-14 11:04:36 +02:00
if (this.props.room) {
return MatrixClientPeg.get().sendStateEvent(
2021-08-14 11:04:36 +02:00
this.props.room.roomId,
'm.room.avatar',
2021-06-29 13:11:58 +01:00
{ url: url },
2017-10-11 17:56:17 +01:00
'',
);
} else {
return MatrixClientPeg.get().setAvatarUrl(url);
}
2015-12-23 16:52:59 +00:00
});
2021-08-14 11:04:36 +02:00
httpPromise.then(() => {
this.setState({
phase: Phases.Display,
avatarUrl: mediaFromMxc(newUrl).srcHttp,
2015-09-21 17:23:51 +01:00
});
2021-08-14 11:04:36 +02:00
}, () => {
this.setState({
phase: Phases.Error,
2015-09-21 17:23:51 +01:00
});
2021-08-14 11:04:36 +02:00
this.onError();
2015-09-21 17:23:51 +01:00
});
2015-12-23 16:52:59 +00:00
return httpPromise;
2020-08-29 12:14:16 +01:00
}
2021-08-14 11:04:36 +02:00
private onFileSelected = (ev: React.ChangeEvent<HTMLInputElement>) => {
this.avatarSet = true;
2015-12-23 16:52:59 +00:00
return this.setAvatarFromFile(ev.target.files[0]);
2020-08-29 12:14:16 +01:00
};
2021-08-14 11:04:36 +02:00
private onError = (): void => {
this.setState({
2017-10-11 17:56:17 +01:00
errorText: _t("Failed to upload profile picture!"),
});
2020-08-29 12:14:16 +01:00
};
2021-08-14 11:04:36 +02:00
public render(): JSX.Element {
2017-10-11 17:56:17 +01:00
let avatarImg;
// Having just set an avatar we just display that since it will take a little
// time to propagate through to the RoomAvatar.
if (this.props.room && !this.avatarSet) {
2020-10-30 18:18:17 +00:00
avatarImg = <RoomAvatar
2021-07-23 10:23:45 +01:00
room={this.props.room}
width={this.props.width}
height={this.props.height}
resizeMethod='crop'
2020-10-30 18:18:17 +00:00
/>;
} else {
2016-02-15 22:01:22 +02:00
// XXX: FIXME: once we track in the JS what our own displayname is(!) then use it here rather than ?
2021-07-23 10:23:45 +01:00
avatarImg = <BaseAvatar
width={this.props.width}
height={this.props.height}
resizeMethod='crop'
name='?'
idName={MatrixClientPeg.get().getUserIdLocalpart()}
url={this.state.avatarUrl}
/>;
}
2017-10-11 17:56:17 +01:00
let uploadSection;
2015-12-23 16:52:59 +00:00
if (this.props.showUploadSection) {
uploadSection = (
2015-12-24 09:20:16 +00:00
<div className={this.props.className}>
2017-10-11 17:56:17 +01:00
{ _t("Upload new:") }
<input type="file" accept="image/*" onChange={this.onFileSelected} />
{ this.state.errorText }
2017-01-20 14:22:27 +00:00
</div>
2015-12-23 16:52:59 +00:00
);
}
switch (this.state.phase) {
2021-08-14 11:04:36 +02:00
case Phases.Display:
case Phases.Error:
return (
<div>
2015-12-24 09:20:16 +00:00
<div className={this.props.className}>
2017-10-11 17:56:17 +01:00
{ avatarImg }
</div>
2017-10-11 17:56:17 +01:00
{ uploadSection }
</div>
);
2021-08-14 11:04:36 +02:00
case Phases.Uploading:
return (
2020-10-30 18:18:17 +00:00
<Spinner />
);
}
2020-08-29 12:14:16 +01:00
}
}