Stop using the js-sdk's compare function (#12782)

* Stop using the js-sdk's compare function

The file is supposed to be a js-sdk internal module so we shouldn't
have been using it, and now it uses the native collator, it's completely
trivial. It was also causing Intl.Collator to be accessed at the module
scope which risked it beating the modernizr check.

* add test

* Fix tests

Move the restoreAllMocks to prevent mock leakage and also add
some custom themes to test the ordering of those.

* Move spy to the right place

* Add ANOTHER test

* Add test for integration manager ordering
This commit is contained in:
David Baker 2024-07-17 14:51:42 +01:00 committed by GitHub
parent 3c9bd69d48
commit 39d453a5a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 146 additions and 29 deletions

View file

@ -17,7 +17,6 @@ limitations under the License.
import React from "react";
import { Room, RoomEvent, RoomMember, RoomMemberEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { compare } from "matrix-js-sdk/src/utils";
import * as WhoIsTyping from "../../../WhoIsTyping";
import Timer from "../../../utils/Timer";
@ -208,7 +207,8 @@ export default class WhoIsTypingTile extends React.Component<IProps, IState> {
// sort them so the typing members don't change order when
// moved to delayedStopTypingTimers
usersTyping.sort((a, b) => compare(a.name, b.name));
const collator = new Intl.Collator();
usersTyping.sort((a, b) => collator.compare(a.name, b.name));
const typingString = WhoIsTyping.whoIsTypingString(usersTyping, this.props.whoIsTypingLimit);
if (!typingString) {

View file

@ -18,7 +18,6 @@
import React, { useState, JSX, PropsWithChildren } from "react";
import { Button } from "@vector-im/compound-web";
import { compare } from "matrix-js-sdk/src/utils";
import { useMatrixClientContext } from "../../../contexts/MatrixClientContext";
import PowerSelector from "../elements/PowerSelector";
@ -78,9 +77,11 @@ export function PowerLevelSelector({
currentPowerLevel && currentPowerLevel.value !== userLevels[currentPowerLevel?.userId],
);
const collator = new Intl.Collator();
// We sort the users by power level, then we filter them
const users = Object.keys(userLevels)
.sort((userA, userB) => sortUser(userA, userB, userLevels))
.sort((userA, userB) => sortUser(collator, userA, userB, userLevels))
.filter(filter);
// No user to display, we return the children into fragment to convert it to JSX.Element type
@ -136,7 +137,14 @@ export function PowerLevelSelector({
* @param userB
* @param userLevels
*/
function sortUser(userA: string, userB: string, userLevels: PowerLevelSelectorProps["userLevels"]): number {
function sortUser(
collator: Intl.Collator,
userA: string,
userB: string,
userLevels: PowerLevelSelectorProps["userLevels"],
): number {
const powerLevelDiff = userLevels[userA] - userLevels[userB];
return powerLevelDiff !== 0 ? powerLevelDiff : compare(userA.toLocaleLowerCase(), userB.toLocaleLowerCase());
return powerLevelDiff !== 0
? powerLevelDiff
: collator.compare(userA.toLocaleLowerCase(), userB.toLocaleLowerCase());
}

View file

@ -16,7 +16,6 @@ limitations under the License.
import { logger } from "matrix-js-sdk/src/logger";
import { ClientEvent, IClientWellKnown, MatrixClient } from "matrix-js-sdk/src/matrix";
import { compare } from "matrix-js-sdk/src/utils";
import type { MatrixEvent } from "matrix-js-sdk/src/matrix";
import SdkConfig from "../SdkConfig";
@ -145,6 +144,7 @@ export class IntegrationManagers {
}
public getOrderedManagers(): IntegrationManagerInstance[] {
const collator = new Intl.Collator();
const ordered: IntegrationManagerInstance[] = [];
for (const kind of KIND_PREFERENCE) {
const managers = this.managers.filter((m) => m.kind === kind);
@ -152,7 +152,7 @@ export class IntegrationManagers {
if (kind === Kind.Account) {
// Order by state_keys (IDs)
managers.sort((a, b) => compare(a.id ?? "", b.id ?? ""));
managers.sort((a, b) => collator.compare(a.id ?? "", b.id ?? ""));
}
ordered.push(...managers);

View file

@ -15,7 +15,6 @@ limitations under the License.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import { compare } from "matrix-js-sdk/src/utils";
import { TagID } from "../../models";
import { IAlgorithm } from "./IAlgorithm";
@ -25,8 +24,9 @@ import { IAlgorithm } from "./IAlgorithm";
*/
export class AlphabeticAlgorithm implements IAlgorithm {
public sortRooms(rooms: Room[], tagId: TagID): Room[] {
const collator = new Intl.Collator();
return rooms.sort((a, b) => {
return compare(a.name, b.name);
return collator.compare(a.name, b.name);
});
}
}

View file

@ -16,7 +16,7 @@
import { Room, RoomStateEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { Optional } from "matrix-events-sdk";
import { compare, MapWithDefault, recursiveMapToObject } from "matrix-js-sdk/src/utils";
import { MapWithDefault, recursiveMapToObject } from "matrix-js-sdk/src/utils";
import { IWidget } from "matrix-widget-api";
import SettingsStore from "../../settings/SettingsStore";
@ -200,6 +200,8 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
const runoff = topWidgets.slice(MAX_PINNED);
rightWidgets.push(...runoff);
const collator = new Intl.Collator();
// Order the widgets in the top container, putting autopinned Jitsi widgets first
// unless they have a specific order in mind
topWidgets.sort((a, b) => {
@ -219,7 +221,7 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
if (orderA === orderB) {
// We just need a tiebreak
return compare(a.id, b.id);
return collator.compare(a.id, b.id);
}
return orderA - orderB;

View file

@ -15,7 +15,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { compare } from "matrix-js-sdk/src/utils";
import { logger } from "matrix-js-sdk/src/logger";
import { _t } from "./languageHandler";
@ -113,7 +112,10 @@ export function getOrderedThemes(): ITheme[] {
.map((p) => ({ id: p[0], name: p[1] })) // convert pairs to objects for code readability
.filter((p) => !isHighContrastTheme(p.id));
const builtInThemes = themes.filter((p) => !p.id.startsWith("custom-"));
const customThemes = themes.filter((p) => !builtInThemes.includes(p)).sort((a, b) => compare(a.name, b.name));
const collator = new Intl.Collator();
const customThemes = themes
.filter((p) => !builtInThemes.includes(p))
.sort((a, b) => collator.compare(a.name, b.name));
return [...builtInThemes, ...customThemes];
}