From 6042e015e08c247e90887edefd643366207ebf7a Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 6 Jul 2020 17:49:37 +0100 Subject: [PATCH 1/5] Remove unused dispatches view_indexed_room and view_prev_room Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/structures/LoggedInView.tsx | 14 -------------- src/components/structures/MatrixChat.tsx | 19 ------------------- 2 files changed, 33 deletions(-) diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index 9fbc98dee3..4876d79131 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -409,20 +409,6 @@ class LoggedInView extends React.Component { }; _onKeyDown = (ev) => { - /* - // Remove this for now as ctrl+alt = alt-gr so this breaks keyboards which rely on alt-gr for numbers - // Will need to find a better meta key if anyone actually cares about using this. - if (ev.altKey && ev.ctrlKey && ev.keyCode > 48 && ev.keyCode < 58) { - dis.dispatch({ - action: 'view_indexed_room', - roomIndex: ev.keyCode - 49, - }); - ev.stopPropagation(); - ev.preventDefault(); - return; - } - */ - let handled = false; const ctrlCmdOnly = isOnlyCtrlOrCmdKeyEvent(ev); const hasModifier = ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey; diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 315c648e15..89ee1bc22d 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -596,15 +596,9 @@ export default class MatrixChat extends React.PureComponent { } break; } - case 'view_prev_room': - this.viewNextRoom(-1); - break; case 'view_next_room': this.viewNextRoom(1); break; - case 'view_indexed_room': - this.viewIndexedRoom(payload.roomIndex); - break; case Action.ViewUserSettings: { const tabPayload = payload as OpenToTabPayload; const UserSettingsDialog = sdk.getComponent("dialogs.UserSettingsDialog"); @@ -812,19 +806,6 @@ export default class MatrixChat extends React.PureComponent { }); } - // TODO: Move to RoomViewStore - private viewIndexedRoom(roomIndex: number) { - const allRooms = RoomListSorter.mostRecentActivityFirst( - MatrixClientPeg.get().getRooms(), - ); - if (allRooms[roomIndex]) { - dis.dispatch({ - action: 'view_room', - room_id: allRooms[roomIndex].roomId, - }); - } - } - // switch view to the given room // // @param {Object} roomInfo Object containing data about the room to be joined From 2da1320d99d2e811311b4bd8e4eee617a420128e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 6 Jul 2020 17:57:40 +0100 Subject: [PATCH 2/5] Type view_room_delta as ViewRoomDelta Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/structures/LoggedInView.tsx | 5 +-- src/dispatcher/actions.ts | 5 +++ .../payloads/ViewRoomDeltaPayload.ts | 32 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/dispatcher/payloads/ViewRoomDeltaPayload.ts diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index 4876d79131..a5a1c628a3 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -53,6 +53,7 @@ import { } from "../../toasts/ServerLimitToast"; import { Action } from "../../dispatcher/actions"; import LeftPanel2 from "./LeftPanel2"; +import { ViewRoomDeltaPayload } from "../../dispatcher/payloads/ViewRoomDeltaPayload"; // We need to fetch each pinned message individually (if we don't already have it) // so each pinned message may trigger a request. Limit the number per room for sanity. @@ -460,8 +461,8 @@ class LoggedInView extends React.Component { case Key.ARROW_UP: case Key.ARROW_DOWN: if (ev.altKey && !ev.ctrlKey && !ev.metaKey) { - dis.dispatch({ - action: 'view_room_delta', + dis.dispatch({ + action: Action.ViewRoomDelta, delta: ev.key === Key.ARROW_UP ? -1 : 1, unread: ev.shiftKey, }); diff --git a/src/dispatcher/actions.ts b/src/dispatcher/actions.ts index 379a0a4451..9be674b59e 100644 --- a/src/dispatcher/actions.ts +++ b/src/dispatcher/actions.ts @@ -79,4 +79,9 @@ export enum Action { * Sets a system font. Should be used with UpdateSystemFontPayload */ UpdateSystemFont = "update_system_font", + + /** + * Changes room based on room list order and payload parameters. Should be used with ViewRoomDeltaPayload. + */ + ViewRoomDelta = "view_room_delta", } diff --git a/src/dispatcher/payloads/ViewRoomDeltaPayload.ts b/src/dispatcher/payloads/ViewRoomDeltaPayload.ts new file mode 100644 index 0000000000..de33a88b2e --- /dev/null +++ b/src/dispatcher/payloads/ViewRoomDeltaPayload.ts @@ -0,0 +1,32 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +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 { ActionPayload } from "../payloads"; +import { Action } from "../actions"; + +export interface ViewRoomDeltaPayload extends ActionPayload { + action: Action.ViewRoomDelta; + + /** + * The delta index of the room to view. + */ + delta: number; + + /** + * Optionally, whether or not to filter to unread (Bold/Grey/Red) rooms only. (Default: false) + */ + unread?: boolean; +} From 1849ed90d264e2d84081e53d2d3a295c7aef17fa Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 6 Jul 2020 17:58:29 +0100 Subject: [PATCH 3/5] Implement ViewRoomDelta for the new Room List Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/RoomList2.tsx | 53 ++++++++++++++++++- .../notifications/RoomNotificationState.ts | 2 +- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/RoomList2.tsx b/src/components/views/rooms/RoomList2.tsx index 06708931a2..0eef2143dd 100644 --- a/src/components/views/rooms/RoomList2.tsx +++ b/src/components/views/rooms/RoomList2.tsx @@ -17,13 +17,16 @@ limitations under the License. */ import * as React from "react"; +import { Dispatcher } from "flux"; +import { Room } from "matrix-js-sdk/src/models/room"; + import { _t, _td } from "../../../languageHandler"; import { RovingTabIndexProvider } from "../../../accessibility/RovingTabIndex"; import { ResizeNotifier } from "../../../utils/ResizeNotifier"; -import RoomListStore, { LISTS_UPDATE_EVENT, RoomListStore2 } from "../../../stores/room-list/RoomListStore2"; +import RoomListStore, { LISTS_UPDATE_EVENT } from "../../../stores/room-list/RoomListStore2"; +import RoomViewStore from "../../../stores/RoomViewStore"; import { ITagMap } from "../../../stores/room-list/algorithms/models"; import { DefaultTagID, TagID } from "../../../stores/room-list/models"; -import { Dispatcher } from "flux"; import dis from "../../../dispatcher/dispatcher"; import defaultDispatcher from "../../../dispatcher/dispatcher"; import RoomSublist2 from "./RoomSublist2"; @@ -35,6 +38,9 @@ import GroupAvatar from "../avatars/GroupAvatar"; import TemporaryTile from "./TemporaryTile"; import { StaticNotificationState } from "../../../stores/notifications/StaticNotificationState"; import { NotificationColor } from "../../../stores/notifications/NotificationColor"; +import { TagSpecificNotificationState } from "../../../stores/notifications/TagSpecificNotificationState"; +import { Action } from "../../../dispatcher/actions"; +import { ViewRoomDeltaPayload } from "../../../dispatcher/payloads/ViewRoomDeltaPayload"; // TODO: Remove banner on launch: https://github.com/vector-im/riot-web/issues/14231 // TODO: Rename on launch: https://github.com/vector-im/riot-web/issues/14231 @@ -140,6 +146,7 @@ const TAG_AESTHETICS: { export default class RoomList2 extends React.Component { private searchFilter: NameFilterCondition = new NameFilterCondition(); + private dispatcherRef; constructor(props: IProps) { super(props); @@ -148,6 +155,8 @@ export default class RoomList2 extends React.Component { sublists: {}, layouts: new Map(), }; + + this.dispatcherRef = defaultDispatcher.register(this.onAction); } public componentDidUpdate(prevProps: Readonly): void { @@ -172,8 +181,48 @@ export default class RoomList2 extends React.Component { public componentWillUnmount() { RoomListStore.instance.off(LISTS_UPDATE_EVENT, this.updateLists); + defaultDispatcher.unregister(this.dispatcherRef); } + private onAction = (payload: ActionPayload) => { + if (payload.action === Action.ViewRoomDelta) { + const viewRoomDeltaPayload = payload as ViewRoomDeltaPayload; + const currentRoomId = RoomViewStore.getRoomId(); + const room = this.getRoomDelta(currentRoomId, viewRoomDeltaPayload.delta, viewRoomDeltaPayload.unread); + if (room) { + dis.dispatch({ + action: 'view_room', + room_id: room.roomId, + show_room_tile: true, // to make sure the room gets scrolled into view + }); + } + } + }; + + private getRoomDelta = (roomId: string, delta: number, unread = false) => { + const lists = RoomListStore.instance.orderedLists; + let rooms: Room = []; + TAG_ORDER.forEach(t => { + let listRooms = lists[t]; + + if (unread) { + const notificationStates = rooms.map(r => new TagSpecificNotificationState(r, t)); + // filter to only notification rooms (and our current active room so we can index properly) + listRooms = notificationStates.filter(state => { + return state.room.roomId === roomId || state.color >= NotificationColor.Bold; + }); + notificationStates.forEach(state => state.destroy()); + } + + rooms.push(...listRooms); + }); + + const currentIndex = rooms.findIndex(r => r.roomId === roomId); + // use slice to account for looping around the start + const [room] = rooms.slice((currentIndex + delta) % rooms.length); + return room; + }; + private updateLists = () => { const newLists = RoomListStore.instance.orderedLists; console.log("new lists", newLists); diff --git a/src/stores/notifications/RoomNotificationState.ts b/src/stores/notifications/RoomNotificationState.ts index f9b19fcbcb..51355a2d4d 100644 --- a/src/stores/notifications/RoomNotificationState.ts +++ b/src/stores/notifications/RoomNotificationState.ts @@ -31,7 +31,7 @@ export class RoomNotificationState extends EventEmitter implements IDestroyable, private _count: number; private _color: NotificationColor; - constructor(private room: Room) { + constructor(public readonly room: Room) { super(); this.room.on("Room.receipt", this.handleReadReceipt); this.room.on("Room.timeline", this.handleRoomEventUpdate); From 8acec1f417e632e21b7ddf1fd9bd2ac05b9c5dfb Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 6 Jul 2020 17:58:39 +0100 Subject: [PATCH 4/5] delint Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/structures/LoggedInView.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index a5a1c628a3..6354bb8739 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -19,7 +19,6 @@ limitations under the License. import * as React from 'react'; import * as PropTypes from 'prop-types'; import { MatrixClient } from 'matrix-js-sdk/src/client'; -import { MatrixEvent } from 'matrix-js-sdk/src/models/event'; import { DragDropContext } from 'react-beautiful-dnd'; import {Key, isOnlyCtrlOrCmdKeyEvent, isOnlyCtrlOrCmdIgnoreShiftKeyEvent} from '../../Keyboard'; From 18064c19a382985b8d05b346254f3a1b8570fd83 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 6 Jul 2020 18:13:11 +0100 Subject: [PATCH 5/5] add TODO Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/RoomList2.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/views/rooms/RoomList2.tsx b/src/components/views/rooms/RoomList2.tsx index 0eef2143dd..66c1ea8798 100644 --- a/src/components/views/rooms/RoomList2.tsx +++ b/src/components/views/rooms/RoomList2.tsx @@ -206,6 +206,8 @@ export default class RoomList2 extends React.Component { let listRooms = lists[t]; if (unread) { + // TODO Be smarter and not spin up a bunch of wasted listeners just to kill them 4 lines later + // https://github.com/vector-im/riot-web/issues/14035 const notificationStates = rooms.map(r => new TagSpecificNotificationState(r, t)); // filter to only notification rooms (and our current active room so we can index properly) listRooms = notificationStates.filter(state => {