Extract voice broadcast header (#9393)

This commit is contained in:
Michael Weimann 2022-10-12 16:00:37 +02:00 committed by GitHub
parent 19bc3f1d9a
commit 533eda2273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 318 additions and 79 deletions

View file

@ -364,4 +364,5 @@
@import "./views/voip/_PiPContainer.pcss";
@import "./views/voip/_VideoFeed.pcss";
@import "./voice-broadcast/atoms/_LiveBadge.pcss";
@import "./voice-broadcast/atoms/_VoiceBroadcastHeader.pcss";
@import "./voice-broadcast/molecules/_VoiceBroadcastRecordingBody.pcss";

View file

@ -32,3 +32,7 @@ limitations under the License.
.mx_Icon_live-badge {
background-color: #fff;
}
.mx_Icon_compound-secondary-content {
background-color: $secondary-content;
}

View file

@ -0,0 +1,44 @@
/*
Copyright 2022 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.
*/
.mx_VoiceBroadcastHeader {
align-items: flex-start;
display: flex;
gap: $spacing-8;
line-height: 20px;
margin-bottom: $spacing-8;
width: 266px;
}
.mx_VoiceBroadcastHeader_sender {
font-size: $font-12px;
font-weight: $font-semi-bold;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.mx_VoiceBroadcastHeader_room {
font-size: $font-12px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.mx_VoiceBroadcastHeader_line {
align-items: center;
color: $secondary-content;
font-size: $font-12px;
display: flex;
gap: $spacing-4;
}

View file

@ -29,6 +29,7 @@ const iconTypeMap = new Map([
export enum IconColour {
Accent = "accent",
LiveBadge = "live-badge",
CompoundSecondaryContent = "compound-secondary-content",
}
export enum IconSize {

View file

@ -637,6 +637,7 @@
"See <b>%(msgtype)s</b> messages posted to this room": "See <b>%(msgtype)s</b> messages posted to this room",
"See <b>%(msgtype)s</b> messages posted to your active room": "See <b>%(msgtype)s</b> messages posted to your active room",
"Live": "Live",
"Voice broadcast": "Voice broadcast",
"Cannot reach homeserver": "Cannot reach homeserver",
"Ensure you have a stable internet connection, or get in touch with the server admin": "Ensure you have a stable internet connection, or get in touch with the server admin",
"Your %(brand)s is misconfigured": "Your %(brand)s is misconfigured",
@ -1850,7 +1851,6 @@
"Emoji": "Emoji",
"Hide stickers": "Hide stickers",
"Sticker": "Sticker",
"Voice broadcast": "Voice broadcast",
"Voice Message": "Voice Message",
"You do not have permission to start polls in this room.": "You do not have permission to start polls in this room.",
"Poll": "Poll",

View file

@ -46,13 +46,10 @@ export const VoiceBroadcastBody: React.FC<IBodyProps> = ({ mxEvent }) => {
recording.stop();
};
const senderId = mxEvent.getSender();
const sender = mxEvent.sender;
return <VoiceBroadcastRecordingBody
onClick={stopVoiceBroadcast}
live={recordingState === VoiceBroadcastInfoState.Started}
member={sender}
userId={senderId}
title={`${sender?.name ?? senderId}${room.name}`}
sender={mxEvent.sender}
roomName={room.name}
/>;
};

View file

@ -0,0 +1,55 @@
/*
Copyright 2022 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 from "react";
import { RoomMember } from "matrix-js-sdk/src/matrix";
import MemberAvatar from "../../../components/views/avatars/MemberAvatar";
import { LiveBadge } from "../..";
import { Icon, IconColour, IconType } from "../../../components/atoms/Icon";
import { _t } from "../../../languageHandler";
interface VoiceBroadcastHeaderProps {
live: boolean;
sender: RoomMember;
roomName: string;
showBroadcast?: boolean;
}
export const VoiceBroadcastHeader: React.FC<VoiceBroadcastHeaderProps> = ({
live,
sender,
roomName,
showBroadcast = false,
}) => {
const broadcast = showBroadcast
? <div className="mx_VoiceBroadcastHeader_line">
<Icon type={IconType.Live} colour={IconColour.CompoundSecondaryContent} />
{ _t("Voice broadcast") }
</div>
: null;
const liveBadge = live ? <LiveBadge /> : null;
return <div className="mx_VoiceBroadcastHeader">
<MemberAvatar member={sender} fallbackUserId={sender.userId} />
<div className="mx_VoiceBroadcastHeader_content">
<div className="mx_VoiceBroadcastHeader_sender">
{ sender.name }
</div>
<div className="mx_VoiceBroadcastHeader_room">
{ roomName }
</div>
{ broadcast }
</div>
{ liveBadge }
</div>;
};

View file

@ -17,40 +17,31 @@ limitations under the License.
import React, { MouseEventHandler } from "react";
import { RoomMember } from "matrix-js-sdk/src/matrix";
import { LiveBadge } from "../..";
import MemberAvatar from "../../../components/views/avatars/MemberAvatar";
import { VoiceBroadcastHeader } from "../..";
interface VoiceBroadcastRecordingBodyProps {
live: boolean;
member: RoomMember;
onClick: MouseEventHandler<HTMLDivElement>;
title: string;
userId: string;
roomName: string;
sender: RoomMember;
}
export const VoiceBroadcastRecordingBody: React.FC<VoiceBroadcastRecordingBodyProps> = ({
live,
member,
onClick,
title,
userId,
roomName,
sender,
}) => {
const liveBadge = live
? <LiveBadge />
: null;
return (
<div
className="mx_VoiceBroadcastRecordingBody"
onClick={onClick}
>
<MemberAvatar member={member} fallbackUserId={userId} />
<div className="mx_VoiceBroadcastRecordingBody_content">
<div className="mx_VoiceBroadcastRecordingBody_title">
{ title }
</div>
</div>
{ liveBadge }
<VoiceBroadcastHeader
live={live}
sender={sender}
roomName={roomName}
/>
</div>
);
};

View file

@ -24,6 +24,7 @@ import { RelationType } from "matrix-js-sdk/src/matrix";
export * from "./audio/VoiceBroadcastRecorder";
export * from "./components/VoiceBroadcastBody";
export * from "./components/atoms/LiveBadge";
export * from "./components/atoms/VoiceBroadcastHeader";
export * from "./components/molecules/VoiceBroadcastRecordingBody";
export * from "./models/VoiceBroadcastRecording";
export * from "./stores/VoiceBroadcastRecordingsStore";

View file

@ -72,9 +72,8 @@ describe("VoiceBroadcastBody", () => {
{
onClick: expect.any(Function),
live: true,
member: infoEvent.sender,
userId: client.getUserId(),
title: "@userId:matrix.org • test room",
sender: infoEvent.sender,
roomName: room.name,
},
{},
);
@ -89,9 +88,8 @@ describe("VoiceBroadcastBody", () => {
{
onClick: expect.any(Function),
live: false,
member: infoEvent.sender,
userId: client.getUserId(),
title: "@userId:matrix.org • test room",
sender: infoEvent.sender,
roomName: room.name,
},
{},
);
@ -105,17 +103,17 @@ describe("VoiceBroadcastBody", () => {
mocked(VoiceBroadcastRecordingBody).mockImplementation(
({
live,
member: _member,
sender,
onClick,
title,
userId: _userId,
roomName,
}) => {
return (
<div
data-testid={recordingTestid}
onClick={onClick}
>
<div>{ title }</div>
<div>{ sender.name }</div>
<div>{ roomName }</div>
<div>{ live && "Live" }</div>
</div>
);

View file

@ -0,0 +1,60 @@
/*
Copyright 2022 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 from "react";
import { Container } from "react-dom";
import { RoomMember } from "matrix-js-sdk/src/matrix";
import { render, RenderResult } from "@testing-library/react";
import { VoiceBroadcastHeader } from "../../../../src/voice-broadcast";
describe("VoiceBroadcastHeader", () => {
const userId = "@user:example.com";
const roomId = "!room:example.com";
const roomName = "test room";
const sender = new RoomMember(roomId, userId);
let container: Container;
const renderHeader = (live: boolean, showBroadcast: boolean = undefined): RenderResult => {
return render(<VoiceBroadcastHeader
live={live}
sender={sender}
roomName={roomName}
showBroadcast={showBroadcast}
/>);
};
beforeAll(() => {
sender.name = "test user";
});
describe("when rendering a live broadcast header with broadcast info", () => {
beforeEach(() => {
container = renderHeader(true, true).container;
});
it("should render the header with a live badge", () => {
expect(container).toMatchSnapshot();
});
});
describe("when rendering a non-live broadcast header", () => {
beforeEach(() => {
container = renderHeader(false).container;
});
it("should render the header without a live badge", () => {
expect(container).toMatchSnapshot();
});
});
});

View file

@ -0,0 +1,109 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`VoiceBroadcastHeader when rendering a live broadcast header with broadcast info should render the header with a live badge 1`] = `
<div>
<div
class="mx_VoiceBroadcastHeader"
>
<span
class="mx_BaseAvatar"
role="presentation"
>
<span
aria-hidden="true"
class="mx_BaseAvatar_initial"
style="font-size: 26px; width: 40px; line-height: 40px;"
>
T
</span>
<img
alt=""
aria-hidden="true"
class="mx_BaseAvatar_image"
src="data:image/png;base64,00"
style="width: 40px; height: 40px;"
title="@user:example.com"
/>
</span>
<div
class="mx_VoiceBroadcastHeader_content"
>
<div
class="mx_VoiceBroadcastHeader_sender"
>
test user
</div>
<div
class="mx_VoiceBroadcastHeader_room"
>
test room
</div>
<div
class="mx_VoiceBroadcastHeader_line"
>
<i
aria-hidden="true"
class="mx_Icon mx_Icon_16 mx_Icon_compound-secondary-content"
role="presentation"
style="mask-image: url(\\"image-file-stub\\");"
/>
Voice broadcast
</div>
</div>
<div
class="mx_LiveBadge"
>
<i
aria-hidden="true"
class="mx_Icon mx_Icon_16 mx_Icon_live-badge"
role="presentation"
style="mask-image: url(\\"image-file-stub\\");"
/>
Live
</div>
</div>
</div>
`;
exports[`VoiceBroadcastHeader when rendering a non-live broadcast header should render the header without a live badge 1`] = `
<div>
<div
class="mx_VoiceBroadcastHeader"
>
<span
class="mx_BaseAvatar"
role="presentation"
>
<span
aria-hidden="true"
class="mx_BaseAvatar_initial"
style="font-size: 26px; width: 40px; line-height: 40px;"
>
T
</span>
<img
alt=""
aria-hidden="true"
class="mx_BaseAvatar_image"
src="data:image/png;base64,00"
style="width: 40px; height: 40px;"
title="@user:example.com"
/>
</span>
<div
class="mx_VoiceBroadcastHeader_content"
>
<div
class="mx_VoiceBroadcastHeader_sender"
>
test user
</div>
<div
class="mx_VoiceBroadcastHeader_room"
>
test room
</div>
</div>
</div>
</div>
`;

View file

@ -18,23 +18,27 @@ import React, { MouseEventHandler } from "react";
import { render, RenderResult } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { RoomMember } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { VoiceBroadcastRecordingBody } from "../../../../src/voice-broadcast";
import MemberAvatar from "../../../../src/components/views/avatars/MemberAvatar";
import { VoiceBroadcastHeader, VoiceBroadcastRecordingBody } from "../../../../src/voice-broadcast";
jest.mock("../../../../src/components/views/avatars/MemberAvatar", () => jest.fn());
jest.mock("../../../../src/voice-broadcast/components/atoms/VoiceBroadcastHeader", () => ({
VoiceBroadcastHeader: ({ live, sender, roomName }: React.ComponentProps<typeof VoiceBroadcastHeader>) => {
return <div data-testid="voice-broadcast-header">
live: { live },
sender: { sender.userId },
room name: { roomName }
</div>;
},
}));
describe("VoiceBroadcastRecordingBody", () => {
const title = "Test Title";
const testRoomName = "test room name";
const userId = "@user:example.com";
const roomMember = new RoomMember("!room:example.com", userId);
let onClick: MouseEventHandler<HTMLDivElement>;
beforeEach(() => {
onClick = jest.fn();
// @ts-ignore
mocked(MemberAvatar).mockReturnValue(<div data-testid="member-avatar" />);
});
describe("when rendered", () => {
@ -44,10 +48,9 @@ describe("VoiceBroadcastRecordingBody", () => {
renderResult = render(
<VoiceBroadcastRecordingBody
onClick={onClick}
title={title}
userId={userId}
roomName={testRoomName}
live={true}
member={roomMember}
sender={roomMember}
/>,
);
});
@ -56,19 +59,9 @@ describe("VoiceBroadcastRecordingBody", () => {
expect(renderResult.container).toMatchSnapshot();
});
it("should pass the props to MemberAvatar", () => {
expect(mocked(MemberAvatar)).toHaveBeenCalledWith(
{
member: roomMember,
fallbackUserId: userId,
},
{},
);
});
describe("and clicked", () => {
beforeEach(async () => {
await userEvent.click(renderResult.getByText(title));
await userEvent.click(renderResult.getByTestId("voice-broadcast-header"));
});
it("should call the onClick prop", () => {
@ -84,10 +77,9 @@ describe("VoiceBroadcastRecordingBody", () => {
renderResult = render(
<VoiceBroadcastRecordingBody
onClick={onClick}
title={title}
userId={userId}
roomName={testRoomName}
live={false}
member={roomMember}
sender={roomMember}
/>,
);
});

View file

@ -6,27 +6,13 @@ exports[`VoiceBroadcastRecordingBody when rendered should render the expected HT
class="mx_VoiceBroadcastRecordingBody"
>
<div
data-testid="member-avatar"
/>
<div
class="mx_VoiceBroadcastRecordingBody_content"
data-testid="voice-broadcast-header"
>
<div
class="mx_VoiceBroadcastRecordingBody_title"
>
Test Title
</div>
</div>
<div
class="mx_LiveBadge"
>
<i
aria-hidden="true"
class="mx_Icon mx_Icon_16 mx_Icon_live-badge"
role="presentation"
style="mask-image: url(\\"image-file-stub\\");"
/>
Live
live:
, sender:
@user:example.com
, room name:
test room name
</div>
</div>
</div>