-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing presence indicator to new room header (#12865)
* Add missing presence indicator to new room header DecoratedRoomAvatar doesn't match Figma styles so created a composable avatar wrapper Signed-off-by: Michael Telatynski <[email protected]> * Add oobData to new room header avatar Signed-off-by: Michael Telatynski <[email protected]> * Simplify Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> * Simplify Signed-off-by: Michael Telatynski <[email protected]> * Add tests Signed-off-by: Michael Telatynski <[email protected]> * Improve coverage Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
- Loading branch information
Showing
9 changed files
with
414 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2024 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_WithPresenceIndicator { | ||
position: relative; | ||
contain: content; | ||
line-height: 0; | ||
|
||
.mx_WithPresenceIndicator_icon { | ||
position: absolute; | ||
right: -2px; | ||
bottom: -2px; | ||
} | ||
|
||
.mx_WithPresenceIndicator_icon::before { | ||
content: ""; | ||
width: 100%; | ||
height: 100%; | ||
right: 0; | ||
bottom: 0; | ||
position: absolute; | ||
border: 2px solid var(--cpd-color-bg-canvas-default); | ||
border-radius: 50%; | ||
} | ||
|
||
.mx_WithPresenceIndicator_icon_offline::before { | ||
background-color: $presence-offline; | ||
} | ||
|
||
.mx_WithPresenceIndicator_icon_online::before { | ||
background-color: $accent; | ||
} | ||
|
||
.mx_WithPresenceIndicator_icon_away::before { | ||
background-color: $presence-away; | ||
} | ||
|
||
.mx_WithPresenceIndicator_icon_busy::before { | ||
background-color: $presence-busy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
Copyright 2024 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, { ReactNode, useEffect, useState } from "react"; | ||
import { ClientEvent, Room, RoomMember, RoomStateEvent, UserEvent } from "matrix-js-sdk/src/matrix"; | ||
import { Tooltip } from "@vector-im/compound-web"; | ||
|
||
import { isPresenceEnabled } from "../../../utils/presence"; | ||
import { _t } from "../../../languageHandler"; | ||
import DMRoomMap from "../../../utils/DMRoomMap"; | ||
import { getJoinedNonFunctionalMembers } from "../../../utils/room/getJoinedNonFunctionalMembers"; | ||
import { useEventEmitter } from "../../../hooks/useEventEmitter"; | ||
import { BUSY_PRESENCE_NAME } from "../rooms/PresenceLabel"; | ||
|
||
interface Props { | ||
room: Room; | ||
size: string; // CSS size | ||
tooltipProps?: { | ||
tabIndex?: number; | ||
}; | ||
children: ReactNode; | ||
} | ||
|
||
enum Presence { | ||
// Note: the names here are used in CSS class names | ||
Online = "ONLINE", | ||
Away = "AWAY", | ||
Offline = "OFFLINE", | ||
Busy = "BUSY", | ||
} | ||
|
||
function tooltipText(variant: Presence): string { | ||
switch (variant) { | ||
case Presence.Online: | ||
return _t("presence|online"); | ||
case Presence.Away: | ||
return _t("presence|away"); | ||
case Presence.Offline: | ||
return _t("presence|offline"); | ||
case Presence.Busy: | ||
return _t("presence|busy"); | ||
} | ||
} | ||
|
||
function getDmMember(room: Room): RoomMember | null { | ||
const otherUserId = DMRoomMap.shared().getUserIdForRoomId(room.roomId); | ||
return otherUserId ? room.getMember(otherUserId) : null; | ||
} | ||
|
||
export const useDmMember = (room: Room): RoomMember | null => { | ||
const [dmMember, setDmMember] = useState<RoomMember | null>(getDmMember(room)); | ||
const updateDmMember = (): void => { | ||
setDmMember(getDmMember(room)); | ||
}; | ||
|
||
useEventEmitter(room.currentState, RoomStateEvent.Members, updateDmMember); | ||
useEventEmitter(room.client, ClientEvent.AccountData, updateDmMember); | ||
useEffect(updateDmMember, [room]); | ||
|
||
return dmMember; | ||
}; | ||
|
||
function getPresence(member: RoomMember | null): Presence | null { | ||
if (!member?.user) return null; | ||
|
||
const presence = member.user.presence; | ||
const isOnline = member.user.currentlyActive || presence === "online"; | ||
if (BUSY_PRESENCE_NAME.matches(member.user.presence)) { | ||
return Presence.Busy; | ||
} | ||
if (isOnline) { | ||
return Presence.Online; | ||
} | ||
if (presence === "offline") { | ||
return Presence.Offline; | ||
} | ||
if (presence === "unavailable") { | ||
return Presence.Away; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const usePresence = (room: Room, member: RoomMember | null): Presence | null => { | ||
const [presence, setPresence] = useState<Presence | null>(getPresence(member)); | ||
const updatePresence = (): void => { | ||
setPresence(getPresence(member)); | ||
}; | ||
|
||
useEventEmitter(member?.user, UserEvent.Presence, updatePresence); | ||
useEventEmitter(member?.user, UserEvent.CurrentlyActive, updatePresence); | ||
useEffect(updatePresence, [member]); | ||
|
||
if (getJoinedNonFunctionalMembers(room).length !== 2 || !isPresenceEnabled(room.client)) return null; | ||
return presence; | ||
}; | ||
|
||
const WithPresenceIndicator: React.FC<Props> = ({ room, size, tooltipProps, children }) => { | ||
const dmMember = useDmMember(room); | ||
const presence = usePresence(room, dmMember); | ||
|
||
let icon: JSX.Element | undefined; | ||
if (presence) { | ||
icon = ( | ||
<div | ||
tabIndex={tooltipProps?.tabIndex ?? 0} | ||
className={`mx_WithPresenceIndicator_icon mx_WithPresenceIndicator_icon_${presence.toLowerCase()}`} | ||
style={{ | ||
width: size, | ||
height: size, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
if (!presence) return <>{children}</>; | ||
|
||
return ( | ||
<div className="mx_WithPresenceIndicator"> | ||
{children} | ||
<Tooltip label={tooltipText(presence)} placement="bottom"> | ||
{icon} | ||
</Tooltip> | ||
</div> | ||
); | ||
}; | ||
|
||
export default WithPresenceIndicator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.