Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,14 @@ body {
font-size: 16px;
margin: 4px;
}

/* The emoji picker for reacting to a message, positioned next to the button that opened it */
.mx_ReactionPicker {
position: fixed;
z-index: 100;
background: var(--cpd-color-bg-canvas-default);
border: 1px solid var(--cpd-color-border-interactive-secondary);
border-radius: var(--cpd-space-2x);
box-shadow: 0 4px 24px rgb(0 0 0 / 0.15);
overflow: hidden;
}
80 changes: 78 additions & 2 deletions src/Timeline/EventTile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
ActionBarAction,
ActionBarView,
DecryptionFailureBodyView,
DecryptionFailureReason,
type EventSendState,
Expand All @@ -11,7 +13,7 @@ import {
import { Avatar } from "@vector-im/compound-web";
import classNames from "classnames";
import type React from "react";
import type { ReactElement, ReactNode } from "react";
import { type ReactElement, type ReactNode, useState } from "react";
import { UtdCause } from "../generated/matrix_sdk_crypto";
import {
type ClientInterface,
Expand All @@ -30,9 +32,14 @@ import {
} from "../index.web";
import { mxcToUrl } from "../utils/mxcToUrl";
import { TextualBodyKind } from "../utils/sharedEnums";
import { StaticViewModel } from "../utils/StaticViewModel";
import { StaticViewModel, staticViewModel } from "../utils/StaticViewModel";
import { userStatusFromProfile } from "../utils/userStatus";
import { AudioBody, FileBody, ImageBody, VideoBody } from "./body/MediaBodies";
import {
ownReactionKeys,
type ReactionsContext,
ReactionsRow,
} from "./reactions/ReactionsRow";
import { TextualBody } from "./body/TextualBody";

interface EventTileProp {
Expand All @@ -45,6 +52,7 @@ interface EventTileProp {
lastInSection: boolean;
sendState?: EventSendState;
client: ClientInterface;
reactions: ReactionsContext;
}
export function getChangeDescription(
membershipChange: MembershipChange,
Expand Down Expand Up @@ -202,7 +210,12 @@ export const EventTile: React.FC<EventTileProp> = ({
lastInSection,
sendState,
client,
reactions,
}) => {
// The action bar shows while the tile is hovered or has focus
const [isHovered, setHovered] = useState(false);
const [hasFocus, setHasFocus] = useState(false);

const senderProfile: Partial<{
displayName?: string;
displayNameAmbiguous?: boolean;
Expand All @@ -216,9 +229,61 @@ export const EventTile: React.FC<EventTileProp> = ({
let body: string | ReactElement | undefined;
let stateChange: ReactNode[] | ReactNode | undefined = undefined;
let msgLikeKind: MsgLikeKind | undefined;
let footer: ReactNode;
let actionBar: ReactNode;
if (TimelineItemContent.MsgLike.instanceOf(event.content)) {
const message = event.content.inner.content;
msgLikeKind = message.kind;
// Undecryptable and deleted messages can't be reacted to
const canHaveReactions =
!MsgLikeKind.Redacted.instanceOf(message.kind) &&
!MsgLikeKind.UnableToDecrypt.instanceOf(message.kind);
if (canHaveReactions && message.reactions.length > 0) {
footer = (
<ReactionsRow
tileId={id}
itemId={event.eventOrTransactionId}
reactions={message.reactions}
context={reactions}
/>
);
}
// Keep the action bar while its picker is open, so focus has somewhere to go back to
const isPickerOpen = reactions.reactionPickerTileId === id;
if (
canHaveReactions &&
reactions.canReact &&
(isHovered || hasFocus || isPickerOpen)
) {
actionBar = (
<ActionBarView
vm={staticViewModel(
{
actions: [ActionBarAction.React],
isDownloadEncrypted: false,
isDownloadLoading: false,
isPinned: false,
isQuoteExpanded: false,
isThreadReplyAllowed: false,
},
{
onReactionsClick: (anchor: HTMLElement | null) => {
if (!anchor) return;
reactions.toggleReactionPicker({
tileId: id,
itemId: event.eventOrTransactionId,
anchor,
selected: ownReactionKeys(
message.reactions,
reactions.ownUserId,
),
});
},
},
)}
/>
);
}

if (MsgLikeKind.Redacted.instanceOf(message.kind)) {
body = (
Expand Down Expand Up @@ -346,6 +411,15 @@ export const EventTile: React.FC<EventTileProp> = ({

return (
<EventTileView
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
onFocus={() => setHasFocus(true)}
onBlur={(e) => {
// Focus moving between things inside the tile doesn't count
if (!e.currentTarget.contains(e.relatedTarget)) {
setHasFocus(false);
}
}}
root={{
id,
// The timeline already puts each row in its own list item.
Expand Down Expand Up @@ -411,6 +485,8 @@ export const EventTile: React.FC<EventTileProp> = ({
/>
),
body: <div className="mx_Content">{body || "No content"}</div>,
footer,
actionBar,
}}
/>
);
Expand Down
63 changes: 61 additions & 2 deletions src/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ import {
ReadMarker,
type TimelineItem,
TimelineView,
useViewModel,
} from "@element-hq/web-shared-components";
import { InlineSpinner } from "@vector-im/compound-web";
import type React from "react";
import { type ReactNode, useCallback, useEffect, useRef } from "react";
import {
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import type {
ClientInterface,
EventTimelineItem,
} from "../generated/matrix_sdk_ffi";
import { StaticViewModel } from "../utils/StaticViewModel";
import { EventTile } from "./EventTile";
import {
ReactionPicker,
type ReactionPickerTarget,
} from "./reactions/ReactionPicker";
import type { ReactionsContext } from "./reactions/ReactionsRow";
import type { TimelineViewModel } from "./TimelineViewModel";

export interface TimelineProps {
Expand Down Expand Up @@ -47,6 +60,41 @@ export const Timeline: React.FC<TimelineProps> = ({
[],
);

const { ownUserId, canReact, canRedactOwn, memberNames } = useViewModel(vm);

// One picker for the whole timeline, so it isn't lost when its tile scrolls out of view
const [reactionPicker, setReactionPicker] =
useState<ReactionPickerTarget | null>(null);
const toggleReactionPicker = useCallback(
(target: ReactionPickerTarget) =>
setReactionPicker((current) =>
current?.tileId === target.tileId ? null : target,
),
[],
);
const closeReactionPicker = useCallback(() => setReactionPicker(null), []);

const reactions = useMemo(
(): ReactionsContext => ({
ownUserId,
canReact,
canRedactOwn,
memberNames,
toggleReaction: vm.toggleReaction,
toggleReactionPicker,
reactionPickerTileId: reactionPicker?.tileId,
}),
[
vm,
ownUserId,
canReact,
canRedactOwn,
memberNames,
toggleReactionPicker,
reactionPicker?.tileId,
],
);

const renderItem = useCallback(
(item: TimelineItem): ReactNode => {
switch (item.kind) {
Expand Down Expand Up @@ -83,18 +131,29 @@ export const Timeline: React.FC<TimelineProps> = ({
lastInSection={item.lastInSection}
sendState={item.sendState}
client={client}
reactions={reactions}
/>
);
default:
return null;
}
},
[client, dateSeparatorVm],
[client, dateSeparatorVm, reactions],
);

return (
<div className="mx_Timeline">
<TimelineView vm={vm} renderItem={renderItem} />
{reactionPicker && canReact && (
<ReactionPicker
target={reactionPicker}
canRedactOwn={canRedactOwn}
onChoose={(key) =>
vm.toggleReaction(reactionPicker.itemId, key)
}
onFinished={closeReactionPicker}
/>
)}
</div>
);
};
Loading
Loading