From 361fb2ebba3fe5326cc44cd8e2fcd65a5e001b49 Mon Sep 17 00:00:00 2001 From: Badi Ifaoui Date: Wed, 29 Nov 2023 14:34:28 +0100 Subject: [PATCH] feat: overwrite room name component --- components.json | 1 + src/components/views/elements/RoomName.tsx | 44 ++++++++++++++++++++++ src/hooks/useTokenGatedRoom.tsx | 4 +- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/components/views/elements/RoomName.tsx diff --git a/components.json b/components.json index 6b25c16a66..218b7d3af0 100644 --- a/components.json +++ b/components.json @@ -4,5 +4,6 @@ "src/components/views/auth/AuthPage.tsx": "src/components/views/auth/VectorAuthPage.tsx", "src/components/views/rooms/Autocomplete.tsx": "src/components/views/rooms/Autocomplete.tsx", "src/components/views/rooms/RoomTile.tsx": "src/components/views/rooms/RoomTile.tsx", + "src/components/views/elements/RoomName.tsx": "src/components/views/elements/RoomName.tsx", "src/editor/commands.tsx": "src/editor/commands.tsx" } diff --git a/src/components/views/elements/RoomName.tsx b/src/components/views/elements/RoomName.tsx new file mode 100644 index 0000000000..5d67b244e4 --- /dev/null +++ b/src/components/views/elements/RoomName.tsx @@ -0,0 +1,44 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React, { useEffect, useState } from "react"; +import { Room, RoomEvent } from "matrix-js-sdk/src/matrix"; +import { useTypedEventEmitter } from "matrix-react-sdk/src/hooks/useEventEmitter"; + +import { getRoomName } from "../../../hooks/useTokengatedRoom"; + +interface IProps { + room?: Room; + children?(name: string): JSX.Element; +} + +/** + * @deprecated use `useRoomName.ts` instead + */ +const RoomName = ({ room, children }: IProps): JSX.Element => { + const [name, setName] = useState(getRoomName(room)); + useTypedEventEmitter(room, RoomEvent.Name, () => { + setName(getRoomName(room)); + }); + useEffect(() => { + setName(getRoomName(room)); + }, [room]); + + if (children) return children(name ?? ""); + return <>{name || ""}; +}; + +export default RoomName; diff --git a/src/hooks/useTokenGatedRoom.tsx b/src/hooks/useTokenGatedRoom.tsx index 452ff48370..60b7c4427b 100644 --- a/src/hooks/useTokenGatedRoom.tsx +++ b/src/hooks/useTokenGatedRoom.tsx @@ -1,7 +1,7 @@ import { Room } from "matrix-js-sdk/src/matrix"; import { useMemo } from "react"; -export function getRoomName(room: Room): string { +export function getRoomName(room?: Room): string { return (room?.name || "") .replace(":", ":\u200b") // add a zero-width space to allow linewrapping after the colon (matrix defaults) .replace("[TG]", ""); @@ -11,7 +11,7 @@ export function isTokenGatedRoom(room: Room): boolean { return room?.name?.includes("[TG]"); } -export function useTokenGatedRoom(room: Room): any { +export function useTokenGatedRoom(room: Room): { roomName: string; isVerifiedRoom: boolean } { const roomName = useMemo(() => { return getRoomName(room); }, [room]);