2023-07-06 11:10:43 +02:00
|
|
|
/*
|
2025-06-05 07:54:57 -04:00
|
|
|
Copyright 2025 New Vector Ltd.
|
2023-07-06 11:10:43 +02:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-07-06 11:10:43 +02:00
|
|
|
*/
|
|
|
|
|
|
2025-03-13 13:58:43 +01:00
|
|
|
import { type Room, RoomEvent } from "matrix-js-sdk";
|
2026-09-08 13:53:50 +02:00
|
|
|
import { useCallback, useSyncExternalStore } from "react";
|
2023-07-06 11:10:43 +02:00
|
|
|
|
2026-09-08 13:53:50 +02:00
|
|
|
/**
|
|
|
|
|
* The room's name, kept up to date. Null when there is no room yet, for a
|
|
|
|
|
* caller that only sometimes has one.
|
|
|
|
|
*/
|
|
|
|
|
export function useRoomName(room: Room): string;
|
|
|
|
|
export function useRoomName(room: Room | null): string | null;
|
|
|
|
|
export function useRoomName(room: Room | null): string | null {
|
|
|
|
|
const subscribe = useCallback(
|
|
|
|
|
(onChange: () => void) => {
|
|
|
|
|
if (room === null) return (): void => {};
|
|
|
|
|
room.on(RoomEvent.Name, onChange);
|
|
|
|
|
return (): void => {
|
|
|
|
|
room.off(RoomEvent.Name, onChange);
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
[room],
|
|
|
|
|
);
|
|
|
|
|
return useSyncExternalStore(
|
|
|
|
|
subscribe,
|
|
|
|
|
useCallback(() => room?.name ?? null, [room]),
|
2025-06-05 07:54:57 -04:00
|
|
|
);
|
2023-07-06 11:10:43 +02:00
|
|
|
}
|