Skip to content
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

Refactor loadMessage function in room resource. #2047

Merged
merged 17 commits into from
Jan 21, 2025

Conversation

FadhlanR
Copy link
Contributor

@FadhlanR FadhlanR commented Jan 15, 2025

Problem
I initially investigated the scrolling position issue in the AI panel, where it would jump back to the top whenever new messages were added or updated. The root cause was that we were reinstantiating the messages in the room resource every time a new event occurred. This caused Ember to rerender all the messages in the room, preventing the browser from maintaining the scrolling position.

Solution
In this PR, I refactored the loadMessage function to avoid reinstantiating messages when new events occur. Instead, the event now updates only the necessary parts of the message.

Bugs that are fixed with this PR:

  • Scrolling jumping issue (CS-7745)
  • Message not streaming in (CS-7840)
  • Command result not always rendering (CS-7841)

Copy link

github-actions bot commented Jan 15, 2025

Host Test Results

    1 files  ±0      1 suites  ±0   21m 32s ⏱️ +23s
733 tests +2  731 ✔️ +2  2 💤 ±0  0 ±0 
738 runs  +2  736 ✔️ +2  2 💤 ±0  0 ±0 

Results for commit 967a7b0. ± Comparison against base commit cc09130.

♻️ This comment has been updated with latest results.

@FadhlanR FadhlanR changed the title Fix jumping scrolling issue in AI panel Refactor loadMessage function in room resource. Jan 16, 2025
@FadhlanR FadhlanR marked this pull request as ready for review January 16, 2025 14:56
@@ -1313,7 +1313,7 @@ export default class MatrixService extends Service {
? JSON.parse(event.content.data)
: event.content.data
) as CommandResultWithOutputContent['data'];
this.ensureCardFragmentsLoaded(data.cardEventId, roomData);
await this.ensureCardFragmentsLoaded(data.cardEventId, roomData);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes there is an error says "no cardFragments found". And I noticed that we forgot to add await to this line. It caused card message event are able to be processed without card fragment event.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, I wish there were a way to lint against this but I gather it’s not that simple

@@ -1313,7 +1313,7 @@ export default class MatrixService extends Service {
? JSON.parse(event.content.data)
: event.content.data
) as CommandResultWithOutputContent['data'];
this.ensureCardFragmentsLoaded(data.cardEventId, roomData);
await this.ensureCardFragmentsLoaded(data.cardEventId, roomData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, I wish there were a way to lint against this but I gather it’s not that simple

}

export function formattedMessageForCommand(formattedBody: string) {
return `<p data-test-command-message class="command-message">${formattedBody}</p>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just copied from a different position but does anyone know if we have XSS protection or the like for this? Or is it a safe assumption that malicious messages of this event type can’t be produced?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added this 43de05a.

Copy link
Contributor

@lukemelia lukemelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good approach overall. I don't like that the logic of creating messages (in MessageBuilder) and the logic of updating messages (in RoomResource) is separated. It feels like it should be the responsibility of the same class.

@@ -81,7 +81,7 @@ export default class Room extends Component<Signature> {
@registerConversationScroller={{this.registerConversationScroller}}
@setScrollPosition={{this.setScrollPosition}}
>
{{#each this.messages as |message i|}}
{{#each this.messages key='eventId' as |message i|}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@FadhlanR FadhlanR merged commit 2287691 into main Jan 21, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants