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

Remember the last used server for room directory searches #6322

Merged
44 changes: 39 additions & 5 deletions src/components/structures/RoomDirectory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -61,7 +64,7 @@ interface IState {
loading: boolean;
protocolsLoading: boolean;
error?: string;
instanceId: string | symbol;
instanceId: string;
roomServer: string;
filterString: string;
selectedCommunityId?: string;
Expand Down Expand Up @@ -116,7 +119,30 @@ export default class RoomDirectory extends React.Component<IProps, IState> {
} 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 settingsServers = SettingsStore.getValue("room_directory_servers") || [];
const roomServer = [...configSevers, ...settingsServers].includes(lsRoomServer)
? lsRoomServer
: myHomeserver;
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
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;
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved

this.setState({
protocolsLoading: false,
instanceId: instanceId,
roomServer: roomServer,
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
});
this.refreshRoomList();
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
}, (err) => {
console.warn(`error loading third party protocols: ${err}`);
this.setState({ protocolsLoading: false });
Expand Down Expand Up @@ -150,8 +176,8 @@ export default class RoomDirectory extends React.Component<IProps, IState> {
publicRooms: [],
loading: true,
error: null,
instanceId: undefined,
roomServer: MatrixClientPeg.getHomeserverName(),
instanceId: null,
roomServer: null,
filterString: this.props.initialText || "",
selectedCommunityId,
communityName: null,
Expand Down Expand Up @@ -342,7 +368,7 @@ export default class RoomDirectory extends React.Component<IProps, IState> {
}
};

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({
Expand All @@ -360,6 +386,14 @@ export default class RoomDirectory extends React.Component<IProps, IState> {
// 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) => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/views/directory/NetworkDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
export const ALL_ROOMS = "ALL_ROOMS";

const SETTING_NAME = "room_directory_servers";

Expand Down Expand Up @@ -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 {
Expand All @@ -112,8 +112,8 @@ export type Protocols = Record<string, IProtocol>;
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:
Expand Down Expand Up @@ -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
SimonBrandner marked this conversation as resolved.
Show resolved Hide resolved
protocolsList.push({
instances: [{
fields: [],
Expand Down