Fix missing metaspace notification badges (#11269)

* Fix missing metaspace notification badges

* Simplify conditional types
This commit is contained in:
Michael Telatynski 2023-07-14 15:40:59 +01:00 committed by GitHub
parent eced103458
commit cdffd1ca1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 6 deletions

View file

@ -65,7 +65,7 @@ interface IButtonProps extends Omit<ComponentProps<typeof AccessibleTooltipButto
export const SpaceButton: React.FC<IButtonProps> = ({ export const SpaceButton: React.FC<IButtonProps> = ({
space, space,
spaceKey, spaceKey: _spaceKey,
className, className,
selected, selected,
label, label,
@ -82,6 +82,8 @@ export const SpaceButton: React.FC<IButtonProps> = ({
const [onFocus, isActive] = useRovingTabIndex(handle); const [onFocus, isActive] = useRovingTabIndex(handle);
const tabIndex = isActive ? 0 : -1; const tabIndex = isActive ? 0 : -1;
const spaceKey = _spaceKey ?? space?.roomId;
let avatar = ( let avatar = (
<div className="mx_SpaceButton_avatarPlaceholder"> <div className="mx_SpaceButton_avatarPlaceholder">
<div className="mx_SpaceButton_icon" /> <div className="mx_SpaceButton_icon" />
@ -92,16 +94,16 @@ export const SpaceButton: React.FC<IButtonProps> = ({
} }
let notifBadge; let notifBadge;
if (space && notificationState) { if (spaceKey && notificationState) {
let ariaLabel = _t("Jump to first unread room."); let ariaLabel = _t("Jump to first unread room.");
if (space.getMyMembership() === "invite") { if (space?.getMyMembership() === "invite") {
ariaLabel = _t("Jump to first invite."); ariaLabel = _t("Jump to first invite.");
} }
const jumpToNotification = (ev: MouseEvent): void => { const jumpToNotification = (ev: MouseEvent): void => {
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
SpaceStore.instance.setActiveRoomInSpace(spaceKey ?? space.roomId); SpaceStore.instance.setActiveRoomInSpace(spaceKey);
}; };
notifBadge = ( notifBadge = (
@ -132,7 +134,9 @@ export const SpaceButton: React.FC<IButtonProps> = ({
const viewSpaceHome = (): void => const viewSpaceHome = (): void =>
// space is set here because of the assignment condition of onClick // space is set here because of the assignment condition of onClick
defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space!.roomId }); defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space!.roomId });
const activateSpace = (): void => SpaceStore.instance.setActiveSpace(spaceKey ?? space?.roomId ?? ""); const activateSpace = (): void => {
if (spaceKey) SpaceStore.instance.setActiveSpace(spaceKey);
};
const onClick = props.onClick ?? (selected && space ? viewSpaceHome : activateSpace); const onClick = props.onClick ?? (selected && space ? viewSpaceHome : activateSpace);
return ( return (

View file

@ -17,7 +17,7 @@ limitations under the License.
import React from "react"; import React from "react";
import { fireEvent, getByTestId, render } from "@testing-library/react"; import { fireEvent, getByTestId, render } from "@testing-library/react";
import { stubClient, mkRoom } from "../../../test-utils"; import { mkRoom, stubClient } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import DMRoomMap from "../../../../src/utils/DMRoomMap"; import DMRoomMap from "../../../../src/utils/DMRoomMap";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
@ -25,6 +25,8 @@ import { Action } from "../../../../src/dispatcher/actions";
import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel"; import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel";
import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces"; import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces";
import SpaceStore from "../../../../src/stores/spaces/SpaceStore"; import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
import { StaticNotificationState } from "../../../../src/stores/notifications/StaticNotificationState";
import { NotificationColor } from "../../../../src/stores/notifications/NotificationColor";
jest.mock("../../../../src/stores/spaces/SpaceStore", () => { jest.mock("../../../../src/stores/spaces/SpaceStore", () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
@ -99,5 +101,22 @@ describe("SpaceButton", () => {
// Re-activating the metaspace is a no-op // Re-activating the metaspace is a no-op
expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People); expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People);
}); });
it("should render notificationState if one is provided", () => {
const notificationState = new StaticNotificationState(null, 8, NotificationColor.Grey);
const { container, asFragment } = render(
<SpaceButton
spaceKey={MetaSpace.People}
selected={true}
label="People"
data-testid="create-space-button"
notificationState={notificationState}
/>,
);
expect(container.querySelector(".mx_NotificationBadge_count")).toHaveTextContent("8");
expect(asFragment()).toMatchSnapshot();
});
}); });
}); });

View file

@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`SpaceButton metaspace should render notificationState if one is provided 1`] = `
<DocumentFragment>
<div
aria-label="People"
class="mx_AccessibleButton mx_SpaceButton mx_SpaceButton_active"
data-testid="create-space-button"
role="button"
tabindex="-1"
>
<div
class="mx_SpaceButton_selectionWrapper"
>
<div
class="mx_SpaceButton_avatarWrapper"
>
<div
class="mx_SpaceButton_avatarPlaceholder"
>
<div
class="mx_SpaceButton_icon"
/>
</div>
<div
class="mx_SpacePanel_badgeContainer"
>
<div
class="mx_AccessibleButton mx_NotificationBadge mx_NotificationBadge_visible mx_NotificationBadge_2char"
role="button"
tabindex="-1"
>
<span
class="mx_NotificationBadge_count"
>
8
</span>
</div>
</div>
</div>
<span
class="mx_SpaceButton_name"
>
People
</span>
</div>
</div>
</DocumentFragment>
`;