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

Commit ac61c8e

Browse files
committed
Adhere to updated sort order for space children
1 parent e9f59ed commit ac61c8e

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/components/structures/SpaceRoomDirectory.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {mediaFromMxc} from "../../customisations/Media";
3939
import InfoTooltip from "../views/elements/InfoTooltip";
4040
import TextWithTooltip from "../views/elements/TextWithTooltip";
4141
import {useStateToggle} from "../../hooks/useStateToggle";
42+
import {getOrder} from "../../stores/SpaceStore";
4243

4344
interface IHierarchyProps {
4445
space: Room;
@@ -254,7 +255,11 @@ export const HierarchyLevel = ({
254255
const space = cli.getRoom(spaceId);
255256
const hasPermissions = space?.currentState.maySendStateEvent(EventType.SpaceChild, cli.getUserId());
256257

257-
const sortedChildren = sortBy([...(relations.get(spaceId)?.values() || [])], ev => ev.content.order || null);
258+
const children = Array.from(relations.get(spaceId)?.values() || []);
259+
const sortedChildren = sortBy(children, ev => {
260+
// XXX: Space Summary API doesn't give the child origin_server_ts but once it does we should use it for sorting
261+
return getOrder(ev.content.order, null, ev.state_key);
262+
});
258263
const [subspaces, childRooms] = sortedChildren.reduce((result, ev: ISpaceSummaryEvent) => {
259264
const roomId = ev.state_key;
260265
if (!rooms.has(roomId)) return result;

src/stores/SpaceStore.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import {sortBy, throttle} from "lodash";
17+
import {ListIteratee, Many, sortBy, throttle} from "lodash";
1818
import {EventType, RoomType} from "matrix-js-sdk/src/@types/event";
1919
import {Room} from "matrix-js-sdk/src/models/room";
2020
import {MatrixEvent} from "matrix-js-sdk/src/models/event";
@@ -61,15 +61,18 @@ const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => { // [spaces,
6161
}, [[], []]);
6262
};
6363

64-
const getOrder = (ev: MatrixEvent): string | null => {
65-
const content = ev.getContent();
66-
if (typeof content.order === "string" && Array.from(content.order).every((c: string) => {
64+
// For sorting space children using a validated `order`, `m.room.create`'s `origin_server_ts`, `room_id`
65+
export const getOrder = (order: string, creationTs: number, roomId: string): Array<Many<ListIteratee<any>>> => {
66+
let validatedOrder: string = null;
67+
68+
if (typeof order === "string" && Array.from(order).every((c: string) => {
6769
const charCode = c.charCodeAt(0);
6870
return charCode >= 0x20 && charCode <= 0x7F;
6971
})) {
70-
return content.order;
72+
validatedOrder = order;
7173
}
72-
return null;
74+
75+
return [validatedOrder, creationTs, roomId];
7376
}
7477

7578
const getRoomFn: FetchRoomFn = (room: Room) => {
@@ -193,7 +196,12 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
193196
private getChildren(spaceId: string): Room[] {
194197
const room = this.matrixClient?.getRoom(spaceId);
195198
const childEvents = room?.currentState.getStateEvents(EventType.SpaceChild).filter(ev => ev.getContent()?.via);
196-
return sortBy(childEvents, getOrder)
199+
return sortBy(childEvents, ev => {
200+
const roomId = ev.getStateKey();
201+
const childRoom = this.matrixClient?.getRoom(roomId);
202+
const createTs = childRoom?.currentState.getStateEvents(EventType.RoomCreate, "")?.getTs();
203+
return getOrder(ev.getContent().order, createTs, roomId);
204+
})
197205
.map(ev => this.matrixClient.getRoom(ev.getStateKey()))
198206
.filter(room => room?.getMyMembership() === "join" || room?.getMyMembership() === "invite") || [];
199207
}

0 commit comments

Comments
 (0)