From 1220ad37ce102bff3db97498b8c7327266ba6d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 7 Jul 2021 11:54:53 +0200 Subject: [PATCH 1/8] Remove the usage of symbol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 2 +- src/components/views/directory/NetworkDropdown.tsx | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index 3acd9f1a2e1..fe07920d954 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -61,7 +61,7 @@ interface IState { loading: boolean; protocolsLoading: boolean; error?: string; - instanceId: string | symbol; + instanceId: string; roomServer: string; filterString: string; selectedCommunityId?: string; diff --git a/src/components/views/directory/NetworkDropdown.tsx b/src/components/views/directory/NetworkDropdown.tsx index c57aa7bccc1..155349e39d5 100644 --- a/src/components/views/directory/NetworkDropdown.tsx +++ b/src/components/views/directory/NetworkDropdown.tsx @@ -41,7 +41,8 @@ import QuestionDialog from "../dialogs/QuestionDialog"; import UIStore from "../../../stores/UIStore"; import { compare } from "../../../utils/strings"; -export const ALL_ROOMS = Symbol("ALL_ROOMS"); +// XXX: We would ideally use a symbol here but we can't since we save this value to localStorage +export const ALL_ROOMS = "ALL_ROOMS"; const SETTING_NAME = "room_directory_servers"; @@ -94,8 +95,7 @@ export interface IInstance { fields: object; network_id: string; // XXX: this is undocumented but we rely on it. - // we inject a fake entry with a symbolic instance_id. - instance_id: string | symbol; + instance_id: string; } export interface IProtocol { @@ -112,8 +112,8 @@ export type Protocols = Record; interface IProps { protocols: Protocols; selectedServerName: string; - selectedInstanceId: string | symbol; - onOptionChange(server: string, instanceId?: string | symbol): void; + selectedInstanceId: string; + onOptionChange(server: string, instanceId?: string): void; } // This dropdown sources homeservers from three places: @@ -171,7 +171,7 @@ const NetworkDropdown = ({ onOptionChange, protocols = {}, selectedServerName, s const protocolsList = server === hsName ? Object.values(protocols) : []; if (protocolsList.length > 0) { - // add a fake protocol with the ALL_ROOMS symbol + // add a fake protocol with the ALL_ROOMS protocolsList.push({ instances: [{ fields: [], From b94dc2d0e5b3eea9d62077f99c206b8c42e06707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 7 Jul 2021 12:01:08 +0200 Subject: [PATCH 2/8] Remember the last used server for room directory searches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index fe07920d954..c0fdcebc759 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -48,6 +48,9 @@ import { ActionPayload } from "../../dispatcher/payloads"; const MAX_NAME_LENGTH = 80; const MAX_TOPIC_LENGTH = 800; +const LAST_SERVER_KEY = "mx_last_room_directory_server"; +const LAST_INSTANCE_KEY = "mx_last_room_directory_instance"; + function track(action: string) { Analytics.trackEvent('RoomDirectory', action); } @@ -150,8 +153,8 @@ export default class RoomDirectory extends React.Component { publicRooms: [], loading: true, error: null, - instanceId: undefined, - roomServer: MatrixClientPeg.getHomeserverName(), + instanceId: localStorage.getItem(LAST_INSTANCE_KEY), + roomServer: localStorage.getItem(LAST_SERVER_KEY) || MatrixClientPeg.getHomeserverName(), filterString: this.props.initialText || "", selectedCommunityId, communityName: null, @@ -342,7 +345,7 @@ export default class RoomDirectory extends React.Component { } }; - private onOptionChange = (server: string, instanceId?: string | symbol) => { + private onOptionChange = (server: string, instanceId?: string) => { // clear next batch so we don't try to load more rooms this.nextBatch = null; this.setState({ @@ -360,6 +363,14 @@ export default class RoomDirectory extends React.Component { // find the five gitter ones, at which point we do not want // to render all those rooms when switching back to 'all networks'. // Easiest to just blow away the state & re-fetch. + + // We have to be careful here so that we don't set instanceId = "undefined" + localStorage.setItem(LAST_SERVER_KEY, server); + if (instanceId) { + localStorage.setItem(LAST_INSTANCE_KEY, instanceId); + } else { + localStorage.removeItem(LAST_INSTANCE_KEY); + } }; private onFillRequest = (backwards: boolean) => { From 255ab49ccbbc65f7235460c83c9bebe59669d45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 7 Jul 2021 13:48:48 +0200 Subject: [PATCH 3/8] Handle edge cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 28 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index c0fdcebc759..3892c9da5cc 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -119,7 +119,29 @@ export default class RoomDirectory extends React.Component { } else if (!selectedCommunityId) { MatrixClientPeg.get().getThirdpartyProtocols().then((response) => { this.protocols = response; - this.setState({ protocolsLoading: false }); + const myHomeserver = MatrixClientPeg.getHomeserverName(); + const lsRoomServer = localStorage.getItem(LAST_SERVER_KEY); + const lsInstanceId = localStorage.getItem(LAST_INSTANCE_KEY); + const configSevers = SdkConfig.get().roomDirectory?.servers || []; + const roomServer = configSevers.includes(lsRoomServer) + ? lsRoomServer + : myHomeserver; + const instanceIds = []; + if (roomServer === myHomeserver) { + Object.values(this.protocols).forEach((protocol) => { + protocol.instances.forEach((instance) => instanceIds.push(instance.instance_id)); + }); + } + const instanceId = (instanceIds.includes(lsInstanceId) || lsInstanceId === ALL_ROOMS) + ? lsInstanceId + : null; + + this.setState({ + protocolsLoading: false, + instanceId: instanceId, + roomServer: roomServer, + }); + this.refreshRoomList(); }, (err) => { console.warn(`error loading third party protocols: ${err}`); this.setState({ protocolsLoading: false }); @@ -153,8 +175,8 @@ export default class RoomDirectory extends React.Component { publicRooms: [], loading: true, error: null, - instanceId: localStorage.getItem(LAST_INSTANCE_KEY), - roomServer: localStorage.getItem(LAST_SERVER_KEY) || MatrixClientPeg.getHomeserverName(), + instanceId: null, + roomServer: null, filterString: this.props.initialText || "", selectedCommunityId, communityName: null, From 5b5ef5e04c5f7ccfd77ba4c717c2a5f0a6f560cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 7 Jul 2021 13:59:02 +0200 Subject: [PATCH 4/8] Handle servers from settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index 3892c9da5cc..4a0e7615c4c 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -123,7 +123,8 @@ export default class RoomDirectory extends React.Component { const lsRoomServer = localStorage.getItem(LAST_SERVER_KEY); const lsInstanceId = localStorage.getItem(LAST_INSTANCE_KEY); const configSevers = SdkConfig.get().roomDirectory?.servers || []; - const roomServer = configSevers.includes(lsRoomServer) + const settingsServers = SettingsStore.getValue("room_directory_servers") || []; + const roomServer = [...configSevers, ...settingsServers].includes(lsRoomServer) ? lsRoomServer : myHomeserver; const instanceIds = []; From 88a5969f2d0a790e52ce8be839889b055db4f509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 9 Jul 2021 10:32:43 +0200 Subject: [PATCH 5/8] Remove a word Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/directory/NetworkDropdown.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/directory/NetworkDropdown.tsx b/src/components/views/directory/NetworkDropdown.tsx index 155349e39d5..0492168f360 100644 --- a/src/components/views/directory/NetworkDropdown.tsx +++ b/src/components/views/directory/NetworkDropdown.tsx @@ -171,7 +171,7 @@ const NetworkDropdown = ({ onOptionChange, protocols = {}, selectedServerName, s const protocolsList = server === hsName ? Object.values(protocols) : []; if (protocolsList.length > 0) { - // add a fake protocol with the ALL_ROOMS + // add a fake protocol with ALL_ROOMS protocolsList.push({ instances: [{ fields: [], From 84b00b5c388709aab1f7c0e3b45291fd7e0ec5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 9 Jul 2021 10:39:02 +0200 Subject: [PATCH 6/8] Make the code more readable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 31 +++++++++++---------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index 4a0e7615c4c..4d335bf63c5 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -122,25 +122,26 @@ export default class RoomDirectory extends React.Component { const myHomeserver = MatrixClientPeg.getHomeserverName(); const lsRoomServer = localStorage.getItem(LAST_SERVER_KEY); const lsInstanceId = localStorage.getItem(LAST_INSTANCE_KEY); - const configSevers = SdkConfig.get().roomDirectory?.servers || []; - const settingsServers = SettingsStore.getValue("room_directory_servers") || []; - const roomServer = [...configSevers, ...settingsServers].includes(lsRoomServer) - ? lsRoomServer - : myHomeserver; - const instanceIds = []; - if (roomServer === myHomeserver) { - Object.values(this.protocols).forEach((protocol) => { - protocol.instances.forEach((instance) => instanceIds.push(instance.instance_id)); - }); + const roomServer = ( + SdkConfig.get().roomDirectory?.servers?.includes(lsRoomServer) || + SettingsStore.getValue("room_directory_servers")?.includes(lsRoomServer) + ) ? lsRoomServer : myHomeserver; + + let instanceId: string = null; + if ( + (lsInstanceId === ALL_ROOMS) || + ( + roomServer === myHomeserver && + Object.values(this.protocols).some(p => p.instances.some(i => i.instance_id === lsInstanceId)) + ) + ) { + instanceId = lsInstanceId; } - const instanceId = (instanceIds.includes(lsInstanceId) || lsInstanceId === ALL_ROOMS) - ? lsInstanceId - : null; this.setState({ protocolsLoading: false, - instanceId: instanceId, - roomServer: roomServer, + instanceId, + roomServer, }); this.refreshRoomList(); }, (err) => { From 6dcf860181dc04bb93417a7d3687c505a1d0bedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 9 Jul 2021 10:44:20 +0200 Subject: [PATCH 7/8] Refresh the room list only if validation failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index 4d335bf63c5..b36531236ac 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -138,12 +138,17 @@ export default class RoomDirectory extends React.Component { instanceId = lsInstanceId; } - this.setState({ - protocolsLoading: false, - instanceId, - roomServer, - }); - this.refreshRoomList(); + // Refresh the room list only if validation failed and we had to change these + if (this.state.instanceId !== instanceId || this.state.roomServer !== roomServer) { + this.setState({ + protocolsLoading: false, + instanceId, + roomServer, + }); + this.refreshRoomList(); + return; + } + this.setState({ protocolsLoading: false }); }, (err) => { console.warn(`error loading third party protocols: ${err}`); this.setState({ protocolsLoading: false }); @@ -177,8 +182,8 @@ export default class RoomDirectory extends React.Component { publicRooms: [], loading: true, error: null, - instanceId: null, - roomServer: null, + instanceId: localStorage.getItem(LAST_INSTANCE_KEY), + roomServer: localStorage.getItem(LAST_SERVER_KEY), filterString: this.props.initialText || "", selectedCommunityId, communityName: null, From 046b3f325c79eca455c0780331e8b44a90d3bc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 9 Jul 2021 12:31:44 +0200 Subject: [PATCH 8/8] Iterate PR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/RoomDirectory.tsx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index b36531236ac..bd25a764a01 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -122,19 +122,20 @@ export default class RoomDirectory extends React.Component { const myHomeserver = MatrixClientPeg.getHomeserverName(); const lsRoomServer = localStorage.getItem(LAST_SERVER_KEY); const lsInstanceId = localStorage.getItem(LAST_INSTANCE_KEY); - const roomServer = ( + + let roomServer = myHomeserver; + if ( SdkConfig.get().roomDirectory?.servers?.includes(lsRoomServer) || SettingsStore.getValue("room_directory_servers")?.includes(lsRoomServer) - ) ? lsRoomServer : myHomeserver; + ) { + roomServer = lsRoomServer; + } let instanceId: string = null; - if ( - (lsInstanceId === ALL_ROOMS) || - ( - roomServer === myHomeserver && - Object.values(this.protocols).some(p => p.instances.some(i => i.instance_id === lsInstanceId)) - ) - ) { + if (roomServer === myHomeserver && ( + lsInstanceId === ALL_ROOMS || + Object.values(this.protocols).some(p => p.instances.some(i => i.instance_id === lsInstanceId)) + )) { instanceId = lsInstanceId; }