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

Create useRoomName hook #11346

Merged
merged 10 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions res/css/views/rooms/_RoomHeader.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ limitations under the License.
--RoomHeader-indicator-pulseColor: $alert;
}

.mx_LegacyRoomHeader {
.mx_RoomHeader {
flex: 0 0 50px;
border-bottom: 1px solid $primary-hairline-color;
background-color: $background;
}

.mx_LegacyRoomHeader_wrapper {
.mx_RoomHeader_wrapper {
height: 44px;
display: flex;
align-items: center;
Expand All @@ -36,7 +36,7 @@ limitations under the License.
border-bottom: 1px solid $separator;
}

.mx_LegacyRoomHeader_name {
.mx_RoomHeader_name {
flex: 0 1 auto;
overflow: hidden;
color: $primary-content;
Expand Down
2 changes: 1 addition & 1 deletion src/components/structures/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
)}
<ErrorBoundary>
{SettingsStore.getValue("feature_new_room_decoration_ui") ? (
<RoomHeader room={this.state.room} />
<RoomHeader room={this.state.room} oobData={this.props.oobData} />
) : (
<LegacyRoomHeader
room={this.state.room}
Expand Down
5 changes: 4 additions & 1 deletion src/components/views/elements/RoomName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
import { useTypedEventEmitter } from "../../../hooks/useEventEmitter";

interface IProps {
room: Room;
room?: Room;
children?(name: string): JSX.Element;
}

/**
* @deprecated use `useRoomName.ts` instead
*/
const RoomName = ({ room, children }: IProps): JSX.Element => {
const [name, setName] = useState(room?.name);
useTypedEventEmitter(room, RoomEvent.Name, () => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/views/rooms/LegacyRoomHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ interface IState {
rightPanelOpen: boolean;
}

/**
* @deprecated use `src/components/views/rooms/RoomHeader.tsx` instead
*/
export default class RoomHeader extends React.Component<IProps, IState> {
public static defaultProps: Partial<IProps> = {
inRoom: false,
Expand Down
32 changes: 7 additions & 25 deletions src/components/views/rooms/RoomHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,18 @@ import React from "react";

import type { Room } from "matrix-js-sdk/src/models/room";
import { _t } from "../../../languageHandler";
import RoomName from "../elements/RoomName";
import { IOOBData } from "../../../stores/ThreepidInviteStore";
import useRoomName from "../../../hooks/useRoomName";

export default function RoomHeader({ room, oobData }: { room?: Room; oobData?: IOOBData }): JSX.Element {
let oobName = _t("Join Room");
if (oobData && oobData.name) {
oobName = oobData.name;
}
const roomName = useRoomName(room, oobData);

return (
<header className="mx_LegacyRoomHeader light-panel">
<div className="mx_LegacyRoomHeader_wrapper">
{room && (
<RoomName room={room}>
{(name) => {
const roomName = name || oobName;
return (
<div
className="mx_LegacyRoomHeader_name"
dir="auto"
title={roomName}
role="heading"
aria-level={1}
>
{roomName}
</div>
);
}}
</RoomName>
)}
<header className="mx_RoomHeader light-panel">
<div className="mx_RoomHeader_wrapper">
<div className="mx_RoomHeader_name" dir="auto" title={roomName} role="heading" aria-level={1}>
{roomName}
</div>
</div>
</header>
);
Expand Down
51 changes: 51 additions & 0 deletions src/hooks/useRoomName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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 { Room, RoomEvent } from "matrix-js-sdk";
import { IOOBData } from "../stores/ThreepidInviteStore";
import { useCallback, useEffect, useState } from "react";
import { useTypedEventEmitter } from "./useEventEmitter";
import { _t } from "../languageHandler";

/**
* Determines the room name from a combination of the room model and potential
* out-of-band information
* @param room - The room model
* @param oobData - out-of-band information about the room
* @returns {string} the room name
*/
export default function useRoomName(room?: Room, oobData?: IOOBData): string {
germain-gg marked this conversation as resolved.
Show resolved Hide resolved
let oobName = _t("Join Room");
if (oobData && oobData.name) {
oobName = oobData.name;
}

const [name, setName] = useState<string>(room?.name || oobName || "");

const setRoomName = useCallback(() => {
germain-gg marked this conversation as resolved.
Show resolved Hide resolved
setName(room?.name || oobName || "");
}, [room?.name, oobData]);

useTypedEventEmitter(room, RoomEvent.Name, () => {
setRoomName();
});

useEffect(() => {
setRoomName();
}, [room?.name, oobData]);

return name;
}
12 changes: 12 additions & 0 deletions test/components/views/rooms/RoomHeader-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ describe("Roomeader", () => {
const { container } = render(<RoomHeader room={room} />);
expect(container).toHaveTextContent(ROOM_ID);
});

it("display the out-of-band room name", () => {
const OOB_NAME = "My private room";
const { container } = render(
<RoomHeader
oobData={{
name: OOB_NAME,
}}
/>,
);
expect(container).toHaveTextContent(OOB_NAME);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
exports[`Roomeader renders with no props 1`] = `
<DocumentFragment>
<header
class="mx_LegacyRoomHeader light-panel"
class="mx_RoomHeader light-panel"
>
<div
class="mx_LegacyRoomHeader_wrapper"
/>
class="mx_RoomHeader_wrapper"
>
<div
aria-level="1"
class="mx_RoomHeader_name"
dir="auto"
role="heading"
title="Join Room"
>
Join Room
</div>
</div>
</header>
</DocumentFragment>
`;