Files
element-call/src/room/useRoomName.ts
T

33 lines
945 B
TypeScript
Raw Normal View History

2023-07-06 11:10:43 +02:00
/*
Copyright 2025 New Vector Ltd.
2023-07-06 11:10:43 +02:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
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]),
);
2023-07-06 11:10:43 +02:00
}