* Don't reference the notification levels by colour We're about to change what colours they are so either we'd have to rename a bunch of constants. We may as well make things not reference what colour anything is in the actual UI. Hopefully these constants are clear enough. * Rename NotificationColor -> NotificationLevel * Red -> Highlight * Grey -> Notification * Bold -> Activity * Anywhere else that calls it 'color' -> 'level' Also fixes some weird mixes of US & UK English. It turns out this is referenced in... quite a lot of places, so this is quite a large PR. It can't really be much smaller, sorry. * One test rename & some hiding due to ts-ignore * More hiding behind ts-ignore * Damn you, @ts-ignore... * Fix test CSS values * Missed some colour -> level Co-authored-by: Florian Duros <florianduros@element.io> * Change other instances of variables renamed in suggestion * Update new test for renames --------- Co-authored-by: Florian Duros <florianduros@element.io>
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
/*
|
|
Copyright 2020 - 2022 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 { Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { NotificationLevel } from "./NotificationLevel";
|
|
import { arrayDiff } from "../../utils/arrays";
|
|
import { RoomNotificationState } from "./RoomNotificationState";
|
|
import { NotificationState, NotificationStateEvents } from "./NotificationState";
|
|
|
|
export type FetchRoomFn = (room: Room) => RoomNotificationState;
|
|
|
|
export class ListNotificationState extends NotificationState {
|
|
private rooms: Room[] = [];
|
|
private states: { [roomId: string]: RoomNotificationState } = {};
|
|
|
|
public constructor(
|
|
private byTileCount = false,
|
|
private getRoomFn: FetchRoomFn,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
public get symbol(): string | null {
|
|
return this._level === NotificationLevel.Unsent ? "!" : null;
|
|
}
|
|
|
|
public setRooms(rooms: Room[]): void {
|
|
// If we're only concerned about the tile count, don't bother setting up listeners.
|
|
if (this.byTileCount) {
|
|
this.rooms = rooms;
|
|
this.calculateTotalState();
|
|
return;
|
|
}
|
|
|
|
const oldRooms = this.rooms;
|
|
const diff = arrayDiff(oldRooms, rooms);
|
|
this.rooms = [...rooms];
|
|
for (const oldRoom of diff.removed) {
|
|
const state = this.states[oldRoom.roomId];
|
|
if (!state) continue; // We likely just didn't have a badge (race condition)
|
|
delete this.states[oldRoom.roomId];
|
|
state.off(NotificationStateEvents.Update, this.onRoomNotificationStateUpdate);
|
|
}
|
|
for (const newRoom of diff.added) {
|
|
const state = this.getRoomFn(newRoom);
|
|
state.on(NotificationStateEvents.Update, this.onRoomNotificationStateUpdate);
|
|
this.states[newRoom.roomId] = state;
|
|
}
|
|
|
|
this.calculateTotalState();
|
|
}
|
|
|
|
public getForRoom(room: Room): RoomNotificationState {
|
|
const state = this.states[room.roomId];
|
|
if (!state) throw new Error("Unknown room for notification state");
|
|
return state;
|
|
}
|
|
|
|
public destroy(): void {
|
|
super.destroy();
|
|
for (const state of Object.values(this.states)) {
|
|
state.off(NotificationStateEvents.Update, this.onRoomNotificationStateUpdate);
|
|
}
|
|
this.states = {};
|
|
}
|
|
|
|
private onRoomNotificationStateUpdate = (): void => {
|
|
this.calculateTotalState();
|
|
};
|
|
|
|
private calculateTotalState(): void {
|
|
const snapshot = this.snapshot();
|
|
|
|
if (this.byTileCount) {
|
|
this._level = NotificationLevel.Highlight;
|
|
this._count = this.rooms.length;
|
|
} else {
|
|
this._count = 0;
|
|
this._level = NotificationLevel.None;
|
|
for (const state of Object.values(this.states)) {
|
|
this._count += state.count;
|
|
this._level = Math.max(this.level, state.level);
|
|
}
|
|
}
|
|
|
|
// finally, publish an update if needed
|
|
this.emitIfUpdated(snapshot);
|
|
}
|
|
}
|