-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Refactor EventTile using the MVVM pattern - #7b #33668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rbondesson
wants to merge
11
commits into
element-hq:develop
Choose a base branch
from
ZacksBot:refactor/event-tile-replay
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c2d2429
Normalize Leaf Adapter Pattern
rbondesson e64c1fb
Extract simple EventTile adapters
rbondesson 8a82fc7
Move simple EventTile adapter VM ownership to EventTileViewModel
rbondesson 028b5e3
Release listener-owning EventTile child VMs on adapter unmount
rbondesson 9a65611
Extract stateful EventTile adapters
rbondesson e39667b
Move action bar VM ownership to EventTileViewModel
rbondesson 5657b5a
Move reactions row VM ownership into EventTileViewModel
rbondesson 37ee807
Extract EventTile receipt rendering
rbondesson 3ce4fdb
Extract EventTile sender identity rendering
rbondesson c339586
Merge branch 'element-hq:develop' into refactor/event-tile-replay
rbondesson 5800cf2
Merge branch 'develop' into refactor/event-tile-replay
rbondesson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
163 changes: 163 additions & 0 deletions
163
apps/web/src/components/views/rooms/EventTile/ActionBarAdapter.tsx
This file contains hidden or 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,163 @@ | ||
| /* | ||
| Copyright 2026 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import React, { useCallback, useContext, useEffect, useState, type JSX } from "react"; | ||
| import { type MatrixEvent, type Relations } from "matrix-js-sdk/src/matrix"; | ||
| import { ActionBarView } from "@element-hq/web-shared-components"; | ||
|
|
||
| import type ReplyChain from "../../elements/ReplyChain"; | ||
| import ReactionPicker from "../../emojipicker/ReactionPicker"; | ||
| import MessageContextMenu from "../../context_menus/MessageContextMenu"; | ||
| import ContextMenu, { aboveLeftOf } from "../../../structures/ContextMenu"; | ||
| import RoomContext from "../../../../contexts/RoomContext"; | ||
| import { CardContext } from "../../right_panel/context"; | ||
| import { type RoomPermalinkCreator } from "../../../../utils/permalinks/Permalinks"; | ||
| import { type EventTileViewModel } from "../../../../viewmodels/room/timeline/event-tile/EventTileViewModel"; | ||
| import { type GetRelationsForEvent } from "../../../../viewmodels/room/timeline/event-tile/reactions/EventTileReactionState"; | ||
|
|
||
| interface ActionBarEventTileOps { | ||
| isWidgetHidden(): boolean; | ||
| unhideWidget(): void; | ||
| } | ||
|
|
||
| interface ActionBarEventTile { | ||
| getEventTileOps?(): ActionBarEventTileOps; | ||
| } | ||
|
|
||
| interface ActionBarAdapterProps { | ||
| eventTileViewModel: EventTileViewModel; | ||
| mxEvent: MatrixEvent; | ||
| reactions?: Relations | null; | ||
| permalinkCreator?: RoomPermalinkCreator; | ||
| getTile: () => ActionBarEventTile | null; | ||
| getReplyChain: () => ReplyChain | null; | ||
| onFocusChange?: (focused: boolean) => void; | ||
| isQuoteExpanded?: boolean; | ||
| toggleThreadExpanded: () => void; | ||
| getRelationsForEvent?: GetRelationsForEvent; | ||
| } | ||
|
|
||
| export function ActionBarAdapter({ | ||
| eventTileViewModel, | ||
| mxEvent, | ||
| reactions, | ||
| permalinkCreator, | ||
| getTile, | ||
| getReplyChain, | ||
| onFocusChange, | ||
| isQuoteExpanded, | ||
| toggleThreadExpanded, | ||
| getRelationsForEvent, | ||
| }: Readonly<ActionBarAdapterProps>): JSX.Element { | ||
| const roomContext = useContext(RoomContext); | ||
| const { isCard } = useContext(CardContext); | ||
| const [optionsMenuAnchorRect, setOptionsMenuAnchorRect] = useState<DOMRect | null>(null); | ||
| const [reactionsMenuAnchorRect, setReactionsMenuAnchorRect] = useState<DOMRect | null>(null); | ||
| const isSearch = Boolean(roomContext.search); | ||
| const handleOptionsClick = useCallback((anchor: HTMLElement | null): void => { | ||
| setOptionsMenuAnchorRect(anchor?.getBoundingClientRect() ?? null); | ||
| }, []); | ||
| const handleReactionsClick = useCallback((anchor: HTMLElement | null): void => { | ||
| setReactionsMenuAnchorRect(anchor?.getBoundingClientRect() ?? null); | ||
| }, []); | ||
| const vm = eventTileViewModel.getActionBarViewModel({ | ||
| mxEvent, | ||
| timelineRenderingType: roomContext.timelineRenderingType, | ||
| canSendMessages: roomContext.canSendMessages, | ||
| canReact: roomContext.canReact, | ||
| isSearch, | ||
| isCard, | ||
| isQuoteExpanded, | ||
| onToggleThreadExpanded: toggleThreadExpanded, | ||
| onOptionsClick: handleOptionsClick, | ||
| onReactionsClick: handleReactionsClick, | ||
| getRelationsForEvent, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| // This child VM owns Matrix and settings listeners, so release it when the view using it leaves the tree. | ||
| return () => eventTileViewModel.releaseActionBarViewModel(); | ||
| }, [eventTileViewModel]); | ||
|
|
||
| useEffect(() => { | ||
| vm.setProps({ | ||
| mxEvent, | ||
| timelineRenderingType: roomContext.timelineRenderingType, | ||
| canSendMessages: roomContext.canSendMessages, | ||
| canReact: roomContext.canReact, | ||
| isSearch, | ||
| isCard, | ||
| isQuoteExpanded, | ||
| getRelationsForEvent, | ||
| onToggleThreadExpanded: toggleThreadExpanded, | ||
| onOptionsClick: handleOptionsClick, | ||
| onReactionsClick: handleReactionsClick, | ||
| }); | ||
| }, [ | ||
| vm, | ||
| mxEvent, | ||
| roomContext.timelineRenderingType, | ||
| roomContext.canSendMessages, | ||
| roomContext.canReact, | ||
| isSearch, | ||
| isCard, | ||
| isQuoteExpanded, | ||
| getRelationsForEvent, | ||
| handleOptionsClick, | ||
| handleReactionsClick, | ||
| toggleThreadExpanded, | ||
| ]); | ||
|
|
||
| useEffect(() => { | ||
| onFocusChange?.(Boolean(optionsMenuAnchorRect || reactionsMenuAnchorRect)); | ||
| }, [onFocusChange, optionsMenuAnchorRect, reactionsMenuAnchorRect]); | ||
|
|
||
| useEffect(() => { | ||
| setOptionsMenuAnchorRect(null); | ||
| setReactionsMenuAnchorRect(null); | ||
| }, [mxEvent]); | ||
|
|
||
| const closeOptionsMenu = useCallback((): void => { | ||
| setOptionsMenuAnchorRect(null); | ||
| }, []); | ||
|
|
||
| const closeReactionsMenu = useCallback((): void => { | ||
| setReactionsMenuAnchorRect(null); | ||
| }, []); | ||
|
|
||
| const tile = getTile(); | ||
| const replyChain = getReplyChain(); | ||
| const eventTileOps = tile?.getEventTileOps ? tile.getEventTileOps() : undefined; | ||
| const collapseReplyChain = replyChain?.canCollapse() ? replyChain.collapse : undefined; | ||
|
|
||
| return ( | ||
| <> | ||
| <ActionBarView vm={vm} className="mx_MessageActionBar" /> | ||
| {optionsMenuAnchorRect ? ( | ||
| <MessageContextMenu | ||
| {...aboveLeftOf(optionsMenuAnchorRect)} | ||
| mxEvent={mxEvent} | ||
| permalinkCreator={permalinkCreator} | ||
| eventTileOps={eventTileOps} | ||
| collapseReplyChain={collapseReplyChain} | ||
| onFinished={closeOptionsMenu} | ||
| getRelationsForEvent={getRelationsForEvent} | ||
| /> | ||
| ) : null} | ||
| {reactionsMenuAnchorRect ? ( | ||
| <ContextMenu | ||
| {...aboveLeftOf(reactionsMenuAnchorRect)} | ||
| onFinished={closeReactionsMenu} | ||
| managed={false} | ||
| focusLock | ||
| > | ||
| <ReactionPicker mxEvent={mxEvent} reactions={reactions} onFinished={closeReactionsMenu} /> | ||
| </ContextMenu> | ||
| ) : null} | ||
| </> | ||
| ); | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
apps/web/src/components/views/rooms/EventTile/E2eMessageSharedIconAdapter.tsx
This file contains hidden or 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,64 @@ | ||
| /* | ||
| Copyright 2026 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import React, { useEffect, type JSX } from "react"; | ||
| import { E2eMessageSharedIconView } from "@element-hq/web-shared-components"; | ||
|
|
||
| import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext"; | ||
| import { type EventTileViewModel } from "../../../../viewmodels/room/timeline/event-tile/EventTileViewModel"; | ||
|
|
||
| interface E2eMessageSharedIconAdapterProps { | ||
| eventTileViewModel: EventTileViewModel; | ||
| /** | ||
| * The ID of the room containing the event whose keys were shared. | ||
| */ | ||
| roomId: string; | ||
| /** | ||
| * The ID of the user who shared the keys. | ||
| */ | ||
| keyForwardingUserId: string; | ||
| } | ||
|
|
||
| export function E2eMessageSharedIconAdapter({ | ||
| eventTileViewModel, | ||
| roomId, | ||
| keyForwardingUserId, | ||
| }: Readonly<E2eMessageSharedIconAdapterProps>): JSX.Element { | ||
| const client = useMatrixClientContext(); | ||
| const vm = eventTileViewModel.getE2eMessageSharedIconViewModel({ | ||
| client, | ||
| roomId, | ||
| keyForwardingUserId, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| // This child VM owns Matrix listeners, so release it when the view using it leaves the tree. | ||
| return () => eventTileViewModel.releaseE2eMessageSharedIconViewModel(); | ||
| }, [eventTileViewModel]); | ||
|
|
||
| useEffect(() => { | ||
| vm.setClient(client); | ||
| }, [client, vm]); | ||
|
|
||
| useEffect(() => { | ||
| vm.setRoomId(roomId); | ||
| }, [roomId, vm]); | ||
|
|
||
| useEffect(() => { | ||
| vm.setKeyForwardingUserId(keyForwardingUserId); | ||
| }, [keyForwardingUserId, vm]); | ||
|
|
||
| return ( | ||
| <E2eMessageSharedIconView | ||
| vm={vm} | ||
| className={ | ||
| // Timeline PCSS uses this app class as a layout hook for positioning and layout variants. | ||
| "mx_EventTile_e2eIcon" | ||
| } | ||
| /> | ||
| ); | ||
| } |
43 changes: 43 additions & 0 deletions
43
apps/web/src/components/views/rooms/EventTile/MessageTimestampAdapter.tsx
This file contains hidden or 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,43 @@ | ||
| /* | ||
| Copyright 2026 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import React, { useEffect, type JSX } from "react"; | ||
| import { MessageTimestampView } from "@element-hq/web-shared-components"; | ||
|
|
||
| import { type EventTileViewModel } from "../../../../viewmodels/room/timeline/event-tile/EventTileViewModel"; | ||
| import { type MessageTimestampViewModelProps } from "../../../../viewmodels/room/timeline/event-tile/timestamp/MessageTimestampViewModel.ts"; | ||
| import { Icon as LateIcon } from "../../../../../res/img/sensor.svg"; | ||
|
|
||
| interface MessageTimestampAdapterProps { | ||
| eventTileViewModel: EventTileViewModel; | ||
| kind: "plain" | "linked"; | ||
| timestampProps: MessageTimestampViewModelProps; | ||
| } | ||
|
|
||
| export function MessageTimestampAdapter({ | ||
| eventTileViewModel, | ||
| kind, | ||
| timestampProps, | ||
| }: Readonly<MessageTimestampAdapterProps>): JSX.Element { | ||
| const vm = | ||
| kind === "linked" | ||
| ? eventTileViewModel.getLinkedMessageTimestampViewModel(timestampProps) | ||
| : eventTileViewModel.getMessageTimestampViewModel(timestampProps); | ||
|
|
||
| useEffect(() => { | ||
| vm.setProps(timestampProps); | ||
| }, [vm, timestampProps]); | ||
|
|
||
| return ( | ||
| <> | ||
| {timestampProps.receivedTs ? ( | ||
| <LateIcon className="mx_MessageTimestamp_lateIcon" width="16" height="16" /> | ||
| ) : undefined} | ||
| <MessageTimestampView vm={vm} className="mx_MessageTimestamp" /> | ||
| </> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this get some doc?