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

Commit ca8b1b0

Browse files
authored
Implement VoiceBroadcastBody update (#9439)
* Implement VoiceBroadcastBody updat * Add doc in VoiceBroadcastBody-test
1 parent 372720e commit ca8b1b0

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/voice-broadcast/components/VoiceBroadcastBody.tsx

+26-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React from "react";
18-
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
17+
import React, { useEffect, useState } from "react";
18+
import { MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix";
1919

2020
import {
2121
VoiceBroadcastRecordingBody,
@@ -28,17 +28,34 @@ import {
2828
} from "..";
2929
import { IBodyProps } from "../../components/views/messages/IBodyProps";
3030
import { MatrixClientPeg } from "../../MatrixClientPeg";
31-
import { getReferenceRelationsForEvent } from "../../events";
31+
import { RelationsHelper, RelationsHelperEvent } from "../../events/RelationsHelper";
3232

3333
export const VoiceBroadcastBody: React.FC<IBodyProps> = ({ mxEvent }) => {
3434
const client = MatrixClientPeg.get();
35-
const relations = getReferenceRelationsForEvent(mxEvent, VoiceBroadcastInfoEventType, client);
36-
const relatedEvents = relations?.getRelations();
37-
const state = !relatedEvents?.find((event: MatrixEvent) => {
38-
return event.getContent()?.state === VoiceBroadcastInfoState.Stopped;
39-
}) ? VoiceBroadcastInfoState.Started : VoiceBroadcastInfoState.Stopped;
35+
const [infoState, setInfoState] = useState(mxEvent.getContent()?.state || VoiceBroadcastInfoState.Stopped);
4036

41-
if (shouldDisplayAsVoiceBroadcastRecordingTile(state, client, mxEvent)) {
37+
useEffect(() => {
38+
const onInfoEvent = (event: MatrixEvent) => {
39+
if (event.getContent()?.state === VoiceBroadcastInfoState.Stopped) {
40+
// only a stopped event can change the tile state
41+
setInfoState(VoiceBroadcastInfoState.Stopped);
42+
}
43+
};
44+
45+
const relationsHelper = new RelationsHelper(
46+
mxEvent,
47+
RelationType.Reference,
48+
VoiceBroadcastInfoEventType,
49+
client,
50+
);
51+
relationsHelper.on(RelationsHelperEvent.Add, onInfoEvent);
52+
53+
return () => {
54+
relationsHelper.destroy();
55+
};
56+
});
57+
58+
if (shouldDisplayAsVoiceBroadcastRecordingTile(infoState, client, mxEvent)) {
4259
const recording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(mxEvent, client);
4360
return <VoiceBroadcastRecordingBody
4461
recording={recording}

test/voice-broadcast/components/VoiceBroadcastBody-test.tsx

+23-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import React from "react";
18-
import { render, screen } from "@testing-library/react";
18+
import { act, render, screen } from "@testing-library/react";
1919
import { mocked } from "jest-mock";
2020
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
2121

@@ -26,12 +26,12 @@ import {
2626
VoiceBroadcastRecordingBody,
2727
VoiceBroadcastRecordingsStore,
2828
VoiceBroadcastRecording,
29-
shouldDisplayAsVoiceBroadcastRecordingTile,
3029
VoiceBroadcastPlaybackBody,
3130
VoiceBroadcastPlayback,
3231
VoiceBroadcastPlaybacksStore,
3332
} from "../../../src/voice-broadcast";
3433
import { mkEvent, stubClient } from "../../test-utils";
34+
import { RelationsHelper } from "../../../src/events/RelationsHelper";
3535

3636
jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastRecordingBody", () => ({
3737
VoiceBroadcastRecordingBody: jest.fn(),
@@ -41,9 +41,7 @@ jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastPlayb
4141
VoiceBroadcastPlaybackBody: jest.fn(),
4242
}));
4343

44-
jest.mock("../../../src/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile", () => ({
45-
shouldDisplayAsVoiceBroadcastRecordingTile: jest.fn(),
46-
}));
44+
jest.mock("../../../src/events/RelationsHelper");
4745

4846
describe("VoiceBroadcastBody", () => {
4947
const roomId = "!room:example.com";
@@ -111,22 +109,38 @@ describe("VoiceBroadcastBody", () => {
111109

112110
describe("when displaying a voice broadcast recording", () => {
113111
beforeEach(() => {
114-
mocked(shouldDisplayAsVoiceBroadcastRecordingTile).mockReturnValue(true);
112+
renderVoiceBroadcast();
115113
});
116114

117115
it("should render a voice broadcast recording body", () => {
118-
renderVoiceBroadcast();
119116
screen.getByTestId("voice-broadcast-recording-body");
120117
});
118+
119+
describe("and the recordings ends", () => {
120+
beforeEach(() => {
121+
const stoppedEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped);
122+
// get the RelationsHelper instanced used in VoiceBroadcastBody
123+
const relationsHelper = mocked(RelationsHelper).mock.instances[5];
124+
act(() => {
125+
// invoke the callback of the VoiceBroadcastBody hook to simulate an ended broadcast
126+
// @ts-ignore
127+
mocked(relationsHelper.on).mock.calls[0][1](stoppedEvent);
128+
});
129+
});
130+
131+
it("should render a voice broadcast playback body", () => {
132+
screen.getByTestId("voice-broadcast-playback-body");
133+
});
134+
});
121135
});
122136

123137
describe("when displaying a voice broadcast playback", () => {
124138
beforeEach(() => {
125-
mocked(shouldDisplayAsVoiceBroadcastRecordingTile).mockReturnValue(false);
139+
mocked(client).getUserId.mockReturnValue("@other:example.com");
140+
renderVoiceBroadcast();
126141
});
127142

128143
it("should render a voice broadcast playback body", () => {
129-
renderVoiceBroadcast();
130144
screen.getByTestId("voice-broadcast-playback-body");
131145
});
132146
});

0 commit comments

Comments
 (0)