Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Wait for initial profile load before displaying widget #7556

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/components/views/elements/AppTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { replaceableComponent } from "../../../utils/replaceableComponent";
import CallHandler from '../../../CallHandler';
import { IApp } from "../../../stores/WidgetStore";
import { WidgetLayoutStore, Container } from "../../../stores/widgets/WidgetLayoutStore";
import { OwnProfileStore } from '../../../stores/OwnProfileStore';
import { UPDATE_EVENT } from '../../../stores/AsyncStore';

interface IProps {
app: IApp;
Expand Down Expand Up @@ -87,6 +89,8 @@ interface IState {
// Assume that widget has permission to load if we are the user who
// added it to the room, or if explicitly granted by the user
hasPermissionToLoad: boolean;
// Wait for user profile load to display correct name
isUserProfileReady: boolean;
error: Error;
menuDisplayed: boolean;
widgetPageTitle: string;
Expand Down Expand Up @@ -130,10 +134,24 @@ export default class AppTile extends React.Component<IProps, IState> {
}

this.state = this.getNewState(props);
this.watchUserReady();

this.allowedWidgetsWatchRef = SettingsStore.watchSetting("allowedWidgets", null, this.onAllowedWidgetsChange);
}

private watchUserReady = () => {
if (OwnProfileStore.instance.isProfileInfoFetched) {
return;
}
OwnProfileStore.instance.once(UPDATE_EVENT, this.onUserReady);
};

private onUserReady = (): void => {
if (OwnProfileStore.instance.isProfileInfoFetched) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a given, the event only fires after a setState which always sets the fetchedAt so this if statement will never fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the update event is also emitted on a reset, where state is set to {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That happens during logout/soft-logout when everything is unmounted, but if you're keeping this if statement then going back to the on listener is better otherwise you would handle a reset case wrong (not that the component would remain rendered for it to matter but better be consistent with your approach I guess)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I would hit the reset case in the in-room registration flow i'm working on for a customer ticket but yeah, it looks like the RoomView + AppTile are remounted anyway. Will remove this check and stick with once

this.setState({ isUserProfileReady: true });
}
};

// This is a function to make the impact of calling SettingsStore slightly less
private hasPermissionToLoad = (props: IProps): boolean => {
if (this.usingLocalWidget()) return true;
Expand All @@ -160,6 +178,7 @@ export default class AppTile extends React.Component<IProps, IState> {
// Assume that widget has permission to load if we are the user who
// added it to the room, or if explicitly granted by the user
hasPermissionToLoad: this.hasPermissionToLoad(newProps),
isUserProfileReady: OwnProfileStore.instance.isProfileInfoFetched,
error: null,
menuDisplayed: false,
widgetPageTitle: this.props.widgetPageTitle,
Expand Down Expand Up @@ -220,6 +239,7 @@ export default class AppTile extends React.Component<IProps, IState> {
}

SettingsStore.unwatchSetting(this.allowedWidgetsWatchRef);
OwnProfileStore.instance.removeListener(UPDATE_EVENT, this.onUserReady);
}

private resetWidget(newProps: IProps): void {
Expand Down Expand Up @@ -473,7 +493,7 @@ export default class AppTile extends React.Component<IProps, IState> {
/>
</div>
);
} else if (this.state.initialising) {
} else if (this.state.initialising || !this.state.isUserProfileReady) {
appTileBody = (
<div className={appTileBodyClass + (this.state.loading ? 'mx_AppLoading' : '')} style={appTileBodyStyles}>
{ loadingElement }
Expand Down
12 changes: 11 additions & 1 deletion src/stores/OwnProfileStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { mediaFromMxc } from "../customisations/Media";
interface IState {
displayName?: string;
avatarUrl?: string;
fetchedAt?: number;
}

const KEY_DISPLAY_NAME = "mx_profile_displayname";
Expand Down Expand Up @@ -67,6 +68,10 @@ export class OwnProfileStore extends AsyncStoreWithClient<IState> {
}
}

public get isProfileInfoFetched(): boolean {
return !!this.state.fetchedAt;
}

/**
* Gets the MXC URI of the user's avatar, or null if not present.
*/
Expand Down Expand Up @@ -135,7 +140,12 @@ export class OwnProfileStore extends AsyncStoreWithClient<IState> {
} else {
window.localStorage.removeItem(KEY_AVATAR_URL);
}
await this.updateState({ displayName: profileInfo.displayname, avatarUrl: profileInfo.avatar_url });

await this.updateState({
displayName: profileInfo.displayname,
avatarUrl: profileInfo.avatar_url,
fetchedAt: Date.now(),
});
};

private onStateEvents = throttle(async (ev: MatrixEvent) => {
Expand Down