-
Notifications
You must be signed in to change notification settings - Fork 203
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
Add logic to load focused group members #6756
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import shallowEqual from 'shallowequal'; | |
|
||
// @ts-ignore - TS doesn't know about SVG files. | ||
import { default as logo } from '../../images/icons/logo.svg'; | ||
import type { Group } from '../../types/api'; | ||
import type { Group, GroupMember, GroupMembers } from '../../types/api'; | ||
import type { SidebarSettings } from '../../types/config'; | ||
import type { Service } from '../../types/config'; | ||
import { serviceConfig } from '../config/service-config'; | ||
|
@@ -23,6 +23,8 @@ const DEFAULT_ORGANIZATION = { | |
logo: 'data:image/svg+xml;utf8,' + encodeURIComponent(logo), | ||
}; | ||
|
||
export const MEMBERS_PAGE_SIZE = 100; | ||
|
||
/** | ||
* For any group that does not have an associated organization, populate with | ||
* the default Hypothesis organization. | ||
|
@@ -61,6 +63,12 @@ export class GroupsService { | |
private _serviceConfig: Service | null; | ||
private _reloadSetUp: boolean; | ||
|
||
/** | ||
* Tracks the loading of the focused group members. | ||
* If this is null it means we are not loading members yet. | ||
*/ | ||
private _focusedMembersController: AbortController | null = null; | ||
|
||
constructor( | ||
store: SidebarStore, | ||
api: APIService, | ||
|
@@ -450,6 +458,10 @@ export class GroupsService { | |
|
||
const groupHasChanged = prevGroupId !== newGroupId && prevGroupId !== null; | ||
if (groupHasChanged && newGroupId) { | ||
// Abort previous in-flight members loading if the group was different | ||
// from the focused one | ||
this._focusedMembersController?.abort(); | ||
|
||
// Move any top-level new annotations to the newly-focused group. | ||
// Leave replies where they are. | ||
const updatedAnnotations = this._store | ||
|
@@ -478,4 +490,71 @@ export class GroupsService { | |
userid: 'me', | ||
}); | ||
} | ||
|
||
/** | ||
* Fetch members for a group from the API and load them into the store as the | ||
* members of the focused group. | ||
*/ | ||
async loadFocusedGroupMembers(groupId: string): Promise<void> { | ||
// Abort previous loading, if any | ||
this._focusedMembersController?.abort(); | ||
|
||
this._focusedMembersController = new AbortController(); | ||
const { signal } = this._focusedMembersController; | ||
|
||
const members = await this._fetchAllMembers(groupId, signal); | ||
if (!signal?.aborted) { | ||
this._store.loadFocusedGroupMembers(members); | ||
} | ||
acelaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private async _fetchAllMembers( | ||
groupId: string, | ||
signal?: AbortSignal, | ||
): Promise<GroupMember[]> { | ||
// Fetch first page of members, to determine how many more pages there are | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a race condition here that if group members are added while we are paging through, we will end up with duplicates. Unlikely, but possible in principle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. Something similar could happen if a member that's already fetched in a previous page is removed. That could cause a member from a subsequent page to not be fetched since it has moved a position "up" and is now part of a page which is supposedly fetched already. None of these issues are easy to handle with current API contract though, where pagination is not done via cursor, but good to have in mind. |
||
const firstPage = await this._fetchMembers({ groupId, signal }); | ||
const pages = Math.min( | ||
Math.ceil(firstPage.meta.page.total / MEMBERS_PAGE_SIZE), | ||
// Do not try to load more than 10 pages, to avoid long loading times and | ||
// hitting the server too much | ||
10, | ||
); | ||
let members = firstPage.data; | ||
|
||
// Fetch remaining pages. Start at 2 to skip first one which was already | ||
// loaded. | ||
// TODO Consider parallelizing requests | ||
for (let page = 2; page <= pages; page++) { | ||
// Do not attempt any further request once the signal has been aborted | ||
if (signal?.aborted) { | ||
break; | ||
} | ||
|
||
const membersPage = await this._fetchMembers({ groupId, page, signal }); | ||
members = members.concat(membersPage.data); | ||
} | ||
|
||
return members; | ||
} | ||
acelaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private _fetchMembers({ | ||
groupId, | ||
page = 1, | ||
signal, | ||
}: { | ||
groupId: string; | ||
page?: number; | ||
signal?: AbortSignal; | ||
}): Promise<GroupMembers> { | ||
return this._api.group.members.read( | ||
{ | ||
pubid: groupId, | ||
'page[number]': page, | ||
'page[size]': MEMBERS_PAGE_SIZE, | ||
}, | ||
undefined, | ||
signal, | ||
); | ||
} | ||
} |
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.
The focused group ID doesn't need to be an argument to this method as we always want to load members for the group that is currently focused, and the service can get that from the store.
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.
Yeah, I know you suggested that in the previous review, and at some point the method was implemented that way.
However, not passing a group ID here means the side effect in the component does not need to depend on the focused group ID, and therefore, it does not re-run if focused group changes while mounted, and before members have been loaded, the group changes again.
One solution would be to still add the focused group ID as a dependency, but it wouldn't be immediately obvious why.
I think there has to be a way to still make that work. Perhaps ensuring focusedGroupMembers value changes when another group is focused. Right now it could be
null
already, so it wouldn't change and therefore, wouldn't trigger a re-render.Anyway, I'll try to think about that separately, as this PR is big enough already.