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

Populate list of suggestions matching @mentions #6728

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Dec 17, 2024

Depends on #6727

Make the list of suggestions displayed when typing @mentions include matching users from those who already annotated the document and belong to the active group.

The list is sorted by username and capped at 10 items.

at-mentions-suggestions-2025-01-16_14.29.37.mp4

Implementation decisions

The component needs to dynamically build a list of suggestions from the list of user that already annotated.

That list of users can be built from the list of annotations in the sidebar store. Since we had to fetch the annotations from the store somewhere, I decided to implement the list of users as a selector for the annotations store module.

It doesn't need to be accessed in many places, so it could have been a helper function on its own, but since the annotations had to be fetched from the store, it was convenient to do it this way.

Out of scope

There are many accessibility implications regarding the suggestions popover. This PR includes a couple of those, but I will dedicate a follow-up piece of work to address that properly and do a bit of extra investigation on how others handle it.

On top of that, I've found a small issue on the Popover component when it drops-up. If the contents change while it's open, causing it to get smaller, the position is not recalculated, so it ends up looking disconnected from its anchor element (the textarea in this case). This should be addressed by hypothesis/frontend-shared#1855

Test steps

  1. Go to http://localhost:5000/admin/features and enable the at_mentions feature flag.
  2. Annotate with a few different users.
  3. Reload the page.
  4. Then while creating/editing an annotation, you should see the suggestions popover display users matching the mention.
  5. When you select one via arrow key + Enter or click, it should be applied in the right position.

Todo

  • Arrow key navigation while keeping textarea focused
  • Select on Enter press while keeping textarea focused
  • Select on click, then focus textarea
  • Style items
  • Add tests
  • Apply the mention appropriately, taking into consideration there could be other mentions already.
  • Preserve caret position when applying a mention

@acelaya acelaya force-pushed the at-mentions-suggestions branch 3 times, most recently from 38f04d8 to 7f8e3e9 Compare December 18, 2024 10:02
@acelaya acelaya force-pushed the at-mentions-popover branch 2 times, most recently from 62c5232 to 412f14c Compare December 18, 2024 10:26
@acelaya acelaya force-pushed the at-mentions-suggestions branch 2 times, most recently from 450cb7f to c5c81e1 Compare December 18, 2024 11:27
@acelaya acelaya force-pushed the at-mentions-popover branch 4 times, most recently from 07dc484 to 07ebc15 Compare December 19, 2024 08:44
Base automatically changed from at-mentions-popover to main December 19, 2024 08:46
@acelaya acelaya force-pushed the at-mentions-suggestions branch 7 times, most recently from 22ee031 to 3397468 Compare December 20, 2024 13:18
@acelaya acelaya force-pushed the at-mentions-suggestions branch 4 times, most recently from ec9ac59 to be21b54 Compare January 10, 2025 14:09
@acelaya acelaya force-pushed the at-mentions-suggestions branch 6 times, most recently from d82702a to 547255d Compare January 13, 2025 08:58
@acelaya acelaya force-pushed the at-mentions-suggestions branch 8 times, most recently from 79f6dbf to f05746e Compare January 14, 2025 14:43
@acelaya acelaya force-pushed the at-mentions-suggestions branch from f05746e to 1c40e7d Compare January 15, 2025 11:42
@acelaya acelaya force-pushed the at-mentions-suggestions branch 2 times, most recently from 38866b2 to 0756c83 Compare January 16, 2025 13:26
@acelaya acelaya marked this pull request as ready for review January 16, 2025 13:31
@acelaya acelaya requested a review from robertknight January 16, 2025 13:31
@acelaya acelaya force-pushed the at-mentions-suggestions branch 3 times, most recently from 5454fb3 to 516682a Compare January 16, 2025 14:04
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

The general structure of this looks good. The main feedback I have is that I would try to do some follow-up refactoring where parts of this functionality such as the mentions popup component and text manipulation are extracted out of the MarkdownEditor. This will make them easier to test on their own.

Maybe down the line it will also be useful to be able to reuse mentions functionality in some other non-markdown input (eg. imagine entering usernames to invite members to a group and getting suggestions), but I don't think you need to make it that general at this stage.

src/sidebar/components/MarkdownEditor.tsx Outdated Show resolved Hide resolved
src/sidebar/components/MarkdownEditor.tsx Outdated Show resolved Hide resolved
src/sidebar/util/term-before-position.ts Outdated Show resolved Hide resolved
src/sidebar/util/term-before-position.ts Outdated Show resolved Hide resolved
!activeMention ||
`${u.username} ${u.displayName ?? ''}`
.toLowerCase()
.match(activeMention.toLowerCase()),
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest pulling this out into a function with its own tests in future.

src/sidebar/components/test/MarkdownEditor-test.js Outdated Show resolved Hide resolved
src/sidebar/store/modules/annotations.ts Outdated Show resolved Hide resolved
src/sidebar/store/modules/annotations.ts Outdated Show resolved Hide resolved
src/sidebar/store/modules/annotations.ts Show resolved Hide resolved
src/sidebar/components/Annotation/AnnotationEditor.tsx Outdated Show resolved Hide resolved
@acelaya acelaya force-pushed the at-mentions-suggestions branch 4 times, most recently from 33038e7 to 0bfdf13 Compare January 17, 2025 16:10
@acelaya acelaya requested a review from robertknight January 17, 2025 16:11
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

The main feedback I have is that I think we could either drop or replace the term "suggestions" in some places, depending on whether it is clear that the thing being suggested is a user.

src/sidebar/components/MarkdownEditor.tsx Outdated Show resolved Hide resolved
src/sidebar/components/MarkdownEditor.tsx Outdated Show resolved Hide resolved
src/sidebar/components/MentionSuggestionsPopover.tsx Outdated Show resolved Hide resolved
src/sidebar/components/MentionSuggestionsPopover.tsx Outdated Show resolved Hide resolved
src/sidebar/components/test/MarkdownEditor-test.js Outdated Show resolved Hide resolved
src/sidebar/components/test/MarkdownEditor-test.js Outdated Show resolved Hide resolved
src/sidebar/util/term-before-position.ts Outdated Show resolved Hide resolved
@@ -167,6 +167,9 @@ function AnnotationEditor({

const textStyle = applyTheme(['annotationFontFamily'], settings);

const mentionsEnabled = store.isFeatureEnabled('at_mentions');
const usersWhoAnnotated = store.usersWhoAnnotated();
Copy link
Member

Choose a reason for hiding this comment

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

The list of suggested users also includes the current user. That feels a bit strange. I note that GitHub does include @robertknight in the list of suggestions as I type this comment. It is possible to see yourself in X and Bluesky mentions too. That surprised me.

self at-mention

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't be super hard to filter out "self" later, if we decide that's the desired behavior.

@acelaya acelaya force-pushed the at-mentions-suggestions branch from 0bfdf13 to f45c930 Compare January 20, 2025 12:15
@acelaya acelaya merged commit 0525e4c into main Jan 20, 2025
4 checks passed
@acelaya acelaya deleted the at-mentions-suggestions branch January 20, 2025 12:20
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.

2 participants