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

@ -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());
}