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

Commit 017f489

Browse files
committed
nits fixes
1 parent 01f4bb8 commit 017f489

File tree

7 files changed

+190
-1
lines changed

7 files changed

+190
-1
lines changed

res/css/views/messages/_MessageActionBar.pcss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
--MessageActionBar-item-hover-background: $panel-actions;
2222
--MessageActionBar-item-hover-borderRadius: 6px;
2323
--MessageActionBar-item-hover-zIndex: 1;
24+
--MessageActionBar-star-button-color: #ffa534;
2425

2526
position: absolute;
2627
visibility: hidden;
@@ -114,6 +115,14 @@ limitations under the License.
114115
}
115116
}
116117

118+
&.mx_MessageActionBar_favouriteButton::after {
119+
mask-image: url('$(res)/img/element-icons/room/message-bar/star.svg');
120+
}
121+
122+
&.mx_MessageActionBar_favouriteButton_fillstar::after {
123+
background-color: var(--MessageActionBar-star-button-color);
124+
}
125+
117126
&.mx_MessageActionBar_editButton::after {
118127
mask-image: url('$(res)/img/element-icons/room/message-bar/edit.svg');
119128
}
Lines changed: 1 addition & 0 deletions
Loading

src/components/views/messages/MessageActionBar.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { ALTERNATE_KEY_NAME } from "../../../accessibility/KeyboardShortcuts";
4848
import { UserTab } from '../dialogs/UserTab';
4949
import { Action } from '../../../dispatcher/actions';
5050
import SdkConfig from "../../../SdkConfig";
51+
import useFavouriteMessages from '../../../hooks/useFavouriteMessages';
5152

5253
interface IOptionsButtonProps {
5354
mxEvent: MatrixEvent;
@@ -237,6 +238,26 @@ const ReplyInThreadButton = ({ mxEvent }: IReplyInThreadButton) => {
237238
</RovingAccessibleTooltipButton>;
238239
};
239240

241+
interface IFavouriteButtonProp {
242+
mxEvent: MatrixEvent;
243+
}
244+
245+
const FavouriteButton = ({ mxEvent }: IFavouriteButtonProp) => {
246+
const { isFavourite, toggleFavourite } = useFavouriteMessages();
247+
248+
const eventId = mxEvent.getId();
249+
const classes = classNames("mx_MessageActionBar_maskButton mx_MessageActionBar_favouriteButton", {
250+
'mx_MessageActionBar_favouriteButton_fillstar': isFavourite(eventId),
251+
});
252+
253+
return <RovingAccessibleTooltipButton
254+
className={classes}
255+
title={_t("Favourite")}
256+
onClick={() => toggleFavourite(eventId)}
257+
data-testid={eventId}
258+
/>;
259+
};
260+
240261
interface IMessageActionBarProps {
241262
mxEvent: MatrixEvent;
242263
reactions?: Relations;
@@ -421,6 +442,7 @@ export default class MessageActionBar extends React.PureComponent<IMessageAction
421442
// Like the resend button, the react and reply buttons need to appear before the edit.
422443
// The only catch is we do the reply button first so that we can make sure the react
423444
// button is the very first button without having to do length checks for `splice()`.
445+
424446
if (this.context.canSendMessages) {
425447
if (this.showReplyInThreadAction) {
426448
toolbarOpts.splice(0, 0, threadTooltipButton);
@@ -442,6 +464,11 @@ export default class MessageActionBar extends React.PureComponent<IMessageAction
442464
key="react"
443465
/>);
444466
}
467+
if (SettingsStore.getValue("feature_favourite_messages")) {
468+
toolbarOpts.splice(-1, 0, (
469+
<FavouriteButton key="favourite" mxEvent={this.props.mxEvent} />
470+
));
471+
}
445472

446473
// XXX: Assuming that the underlying tile will be a media event if it is eligible media.
447474
if (MediaEventHelper.isEligible(this.props.mxEvent)) {

src/hooks/useFavouriteMessages.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/*
3+
Copyright 2022 The Matrix.org Foundation C.I.C.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import { useState } from "react";
19+
20+
const favouriteMessageIds = JSON.parse(
21+
localStorage?.getItem("io_element_favouriteMessages")?? "[]") as string[];
22+
23+
export default function useFavouriteMessages() {
24+
const [, setX] = useState<string[]>();
25+
26+
//checks if an id already exist
27+
const isFavourite = (eventId: string): boolean => favouriteMessageIds.includes(eventId);
28+
29+
const toggleFavourite = (eventId: string) => {
30+
isFavourite(eventId) ? favouriteMessageIds.splice(favouriteMessageIds.indexOf(eventId), 1)
31+
: favouriteMessageIds.push(eventId);
32+
33+
//update the local storage
34+
localStorage.setItem('io_element_favouriteMessages', JSON.stringify(favouriteMessageIds));
35+
36+
// This forces a re-render to account for changes in appearance in real-time when the favourite button is toggled
37+
setX([]);
38+
};
39+
40+
return { isFavourite, toggleFavourite };
41+
}

src/i18n/strings/en_EN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@
902902
"Right-click message context menu": "Right-click message context menu",
903903
"Location sharing - pin drop": "Location sharing - pin drop",
904904
"Live Location Sharing (temporary implementation: locations persist in room history)": "Live Location Sharing (temporary implementation: locations persist in room history)",
905+
"Favourite Messages (under active development)": "Favourite Messages (under active development)",
905906
"Font size": "Font size",
906907
"Use custom size": "Use custom size",
907908
"Enable Emoji suggestions while typing": "Enable Emoji suggestions while typing",
@@ -2115,6 +2116,7 @@
21152116
"Can't create a thread from an event with an existing relation": "Can't create a thread from an event with an existing relation",
21162117
"Beta feature": "Beta feature",
21172118
"Beta feature. Click to learn more.": "Beta feature. Click to learn more.",
2119+
"Favourite": "Favourite",
21182120
"Edit": "Edit",
21192121
"Reply": "Reply",
21202122
"Collapse quotes": "Collapse quotes",
@@ -2940,7 +2942,6 @@
29402942
"Copy link": "Copy link",
29412943
"Forget": "Forget",
29422944
"Favourited": "Favourited",
2943-
"Favourite": "Favourite",
29442945
"Mentions only": "Mentions only",
29452946
"Copy room link": "Copy room link",
29462947
"Low Priority": "Low Priority",

src/settings/Settings.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ export const SETTINGS: {[setting: string]: ISetting} = {
429429
),
430430
default: false,
431431
},
432+
"feature_favourite_messages": {
433+
isFeature: true,
434+
labsGroup: LabGroup.Messaging,
435+
supportedLevels: LEVELS_FEATURE,
436+
displayName: _td("Favourite Messages (under active development)"),
437+
default: false,
438+
},
432439
"baseFontSize": {
433440
displayName: _td("Font size"),
434441
supportedLevels: LEVELS_ACCOUNT_SETTINGS,

test/components/views/messages/MessageActionBar-test.tsx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('<MessageActionBar />', () => {
5959
msgtype: MsgType.Text,
6060
body: 'Hello',
6161
},
62+
event_id: "$alices_message",
6263
});
6364

6465
const bobsMessageEvent = new MatrixEvent({
@@ -69,6 +70,7 @@ describe('<MessageActionBar />', () => {
6970
msgtype: MsgType.Text,
7071
body: 'I am bob',
7172
},
73+
event_id: "$bobs_message",
7274
});
7375

7476
const redactedEvent = new MatrixEvent({
@@ -82,6 +84,25 @@ describe('<MessageActionBar />', () => {
8284
...mockClientMethodsEvents(),
8385
getRoom: jest.fn(),
8486
});
87+
88+
const localStorageMock = (() => {
89+
let store = {};
90+
return {
91+
getItem: jest.fn().mockImplementation(key => store[key] ?? null),
92+
setItem: jest.fn().mockImplementation((key, value) => {
93+
store[key] = value;
94+
}),
95+
clear: jest.fn().mockImplementation(() => {
96+
store = {};
97+
}),
98+
removeItem: jest.fn().mockImplementation((key) => delete store[key]),
99+
};
100+
})();
101+
Object.defineProperty(window, 'localStorage', {
102+
value: localStorageMock,
103+
writable: true,
104+
});
105+
85106
const room = new Room(roomId, client, userId);
86107
jest.spyOn(room, 'getPendingEvents').mockReturnValue([]);
87108

@@ -464,4 +485,86 @@ describe('<MessageActionBar />', () => {
464485
});
465486
});
466487
});
488+
489+
describe('favourite button', () => {
490+
//for multiple event usecase
491+
const favButton = (evt: MatrixEvent) => {
492+
return getComponent({ mxEvent: evt }).getByTestId(evt.getId());
493+
};
494+
495+
describe('when favourite_messages feature is enabled', () => {
496+
beforeEach(() => {
497+
jest.spyOn(SettingsStore, 'getValue')
498+
.mockImplementation(setting => setting === 'feature_favourite_messages');
499+
localStorageMock.clear();
500+
});
501+
502+
it('renders favourite button on own actionable event', () => {
503+
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
504+
expect(queryByLabelText('Favourite')).toBeTruthy();
505+
});
506+
507+
it('renders favourite button on other actionable events', () => {
508+
const { queryByLabelText } = getComponent({ mxEvent: bobsMessageEvent });
509+
expect(queryByLabelText('Favourite')).toBeTruthy();
510+
});
511+
512+
it('does not render Favourite button on non-actionable event', () => {
513+
//redacted event is not actionable
514+
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent });
515+
expect(queryByLabelText('Favourite')).toBeFalsy();
516+
});
517+
518+
it('remembers favourited state of multiple events, and handles the localStorage of the events accordingly',
519+
() => {
520+
const alicesAction = favButton(alicesMessageEvent);
521+
const bobsAction = favButton(bobsMessageEvent);
522+
523+
//default state before being clicked
524+
expect(alicesAction.classList).not.toContain('mx_MessageActionBar_favouriteButton_fillstar');
525+
expect(bobsAction.classList).not.toContain('mx_MessageActionBar_favouriteButton_fillstar');
526+
expect(localStorageMock.getItem('io_element_favouriteMessages')).toBeNull();
527+
528+
//if only alice's event is fired
529+
act(() => {
530+
fireEvent.click(alicesAction);
531+
});
532+
533+
expect(alicesAction.classList).toContain('mx_MessageActionBar_favouriteButton_fillstar');
534+
expect(bobsAction.classList).not.toContain('mx_MessageActionBar_favouriteButton_fillstar');
535+
expect(localStorageMock.setItem)
536+
.toHaveBeenCalledWith('io_element_favouriteMessages', '["$alices_message"]');
537+
538+
//when bob's event is fired,both should be styled and stored in localStorage
539+
act(() => {
540+
fireEvent.click(bobsAction);
541+
});
542+
543+
expect(alicesAction.classList).toContain('mx_MessageActionBar_favouriteButton_fillstar');
544+
expect(bobsAction.classList).toContain('mx_MessageActionBar_favouriteButton_fillstar');
545+
expect(localStorageMock.setItem)
546+
.toHaveBeenCalledWith('io_element_favouriteMessages', '["$alices_message","$bobs_message"]');
547+
548+
//finally, at this point the localStorage should contain the two eventids
549+
expect(localStorageMock.getItem('io_element_favouriteMessages'))
550+
.toEqual('["$alices_message","$bobs_message"]');
551+
552+
//if decided to unfavourite bob's event by clicking again
553+
act(() => {
554+
fireEvent.click(bobsAction);
555+
});
556+
expect(bobsAction.classList).not.toContain('mx_MessageActionBar_favouriteButton_fillstar');
557+
expect(alicesAction.classList).toContain('mx_MessageActionBar_favouriteButton_fillstar');
558+
expect(localStorageMock.getItem('io_element_favouriteMessages')).toEqual('["$alices_message"]');
559+
});
560+
});
561+
562+
describe('when favourite_messages feature is disabled', () => {
563+
it('does not render', () => {
564+
jest.spyOn(SettingsStore, 'getValue').mockReturnValue(false);
565+
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
566+
expect(queryByLabelText('Favourite')).toBeFalsy();
567+
});
568+
});
569+
});
467570
});

0 commit comments

Comments
 (0)