-
Notifications
You must be signed in to change notification settings - Fork 122
Console 1382 member pagination and search #7299
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
Draft
jonathanawesome
wants to merge
13
commits into
main
Choose a base branch
from
console-1382-member-pagination-and-search
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5825c22
fix pagination and add filter to Organization.members
jdolle 475e7fe
Merge branch 'main' into member-pagination-backend
jdolle 68af504
Merge remote-tracking branch 'origin/member-pagination-backend' into …
jonathanawesome 546b550
update route
jonathanawesome abbd0e4
remove empty default search param string
jonathanawesome e1a5546
update query
jonathanawesome 64578a0
strip sorting from UI, remove unused refetch prop, init basic input f…
jonathanawesome 8e820f2
init filter ui
jonathanawesome 5a12547
update hook to clear single value param if length === 0
jonathanawesome a2e9037
initial pagination work
jonathanawesome dea8fc1
fix wonky edge case when clicking prev page button
jonathanawesome a2d73a4
update api to allow for substring search
jonathanawesome 69f6939
update page number viz to show search string and remove cursor-pointe…
jonathanawesome 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
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
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
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
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
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { useCallback, useMemo, useState } from 'react'; | ||
| import { MoreHorizontalIcon, MoveDownIcon, MoveUpIcon } from 'lucide-react'; | ||
| import React, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
| import debounce from 'lodash.debounce'; | ||
| import { ChevronLeftIcon, ChevronRightIcon, MoreHorizontalIcon } from 'lucide-react'; | ||
| import type { IconType } from 'react-icons'; | ||
| import { FaGithub, FaGoogle, FaOpenid, FaUserLock } from 'react-icons/fa'; | ||
| import { useMutation } from 'urql'; | ||
|
|
@@ -20,13 +21,15 @@ import { | |
| DropdownMenuItem, | ||
| DropdownMenuTrigger, | ||
| } from '@/components/ui/dropdown-menu'; | ||
| import { Input } from '@/components/ui/input'; | ||
| import { Link } from '@/components/ui/link'; | ||
| import { SubPageLayout, SubPageLayoutHeader } from '@/components/ui/page-content-layout'; | ||
| import * as Sheet from '@/components/ui/sheet'; | ||
| import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; | ||
| import { useToast } from '@/components/ui/use-toast'; | ||
| import { FragmentType, graphql, useFragment } from '@/gql'; | ||
| import * as GraphQLSchema from '@/gql/graphql'; | ||
| import { useSearchParamsFilter } from '@/lib/hooks/use-search-params-filters'; | ||
| import { MemberInvitationButton } from './invitations'; | ||
| import { MemberRolePicker } from './member-role-picker'; | ||
|
|
||
|
|
@@ -91,10 +94,9 @@ const OrganizationMemberRow_MemberFragment = graphql(` | |
| } | ||
| `); | ||
|
|
||
| function OrganizationMemberRow(props: { | ||
| const OrganizationMemberRow = React.memo(function OrganizationMemberRow(props: { | ||
| organization: FragmentType<typeof OrganizationMembers_OrganizationFragment>; | ||
| member: FragmentType<typeof OrganizationMemberRow_MemberFragment>; | ||
| refetchMembers(): void; | ||
| }) { | ||
| const organization = useFragment(OrganizationMembers_OrganizationFragment, props.organization); | ||
| const member = useFragment(OrganizationMemberRow_MemberFragment, props.member); | ||
|
|
@@ -212,7 +214,7 @@ function OrganizationMemberRow(props: { | |
| </tr> | ||
| </> | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| const MemberRole_OrganizationFragment = graphql(` | ||
| fragment MemberRole_OrganizationFragment on Organization { | ||
|
|
@@ -286,8 +288,9 @@ const OrganizationMembers_OrganizationFragment = graphql(` | |
| owner { | ||
| id | ||
| } | ||
| members { | ||
| members(first: $first, after: $after, filters: { searchTerm: $searchTerm }) { | ||
| edges { | ||
| cursor | ||
| node { | ||
| id | ||
| user { | ||
|
|
@@ -300,6 +303,12 @@ const OrganizationMembers_OrganizationFragment = graphql(` | |
| ...OrganizationMemberRow_MemberFragment | ||
| } | ||
| } | ||
| pageInfo { | ||
| hasNextPage | ||
| hasPreviousPage | ||
| startCursor | ||
| endCursor | ||
| } | ||
| } | ||
| viewerCanManageInvitations | ||
| ...MemberInvitationForm_OrganizationFragment | ||
|
|
@@ -310,111 +319,112 @@ const OrganizationMembers_OrganizationFragment = graphql(` | |
| export function OrganizationMembers(props: { | ||
| organization: FragmentType<typeof OrganizationMembers_OrganizationFragment>; | ||
| refetchMembers(): void; | ||
| currentPage: number; | ||
| onNextPage(): void; | ||
| onPreviousPage(): void; | ||
| }) { | ||
| const organization = useFragment(OrganizationMembers_OrganizationFragment, props.organization); | ||
| const members = organization.members?.edges?.map(edge => edge.node); | ||
| const [orderDirection, setOrderDirection] = useState<'asc' | 'desc' | null>(null); | ||
| const [sortByKey, setSortByKey] = useState<'name' | 'role'>('name'); | ||
|
|
||
| const sortedMembers = useMemo(() => { | ||
| if (!members) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!orderDirection) { | ||
| return members ?? []; | ||
| } | ||
|
|
||
| const sorted = [...members].sort((a, b) => { | ||
| if (sortByKey === 'name') { | ||
| return a.user.displayName.localeCompare(b.user.displayName); | ||
| } | ||
| const pageInfo = organization.members?.pageInfo; | ||
|
|
||
| if (sortByKey === 'role') { | ||
| return (a.role?.name ?? 'Select role').localeCompare(b.role?.name ?? 'Select role') ?? 0; | ||
| } | ||
|
|
||
| return 0; | ||
| }); | ||
| const [search, setSearch] = useSearchParamsFilter<string>('search', ''); | ||
|
|
||
| return orderDirection === 'asc' ? sorted : sorted.reverse(); | ||
| }, [members, orderDirection, sortByKey]); | ||
| // Debounced search to prevent excessive queries | ||
| const debouncedSetSearch = useMemo( | ||
| () => debounce((value: string) => setSearch(value), 300), | ||
| [setSearch], | ||
| ); | ||
|
|
||
| const updateSorting = useCallback( | ||
| (newSortBy: 'name' | 'role') => { | ||
| if (newSortBy === sortByKey) { | ||
| setOrderDirection( | ||
| orderDirection === 'asc' ? 'desc' : orderDirection === 'desc' ? null : 'asc', | ||
| ); | ||
| } else { | ||
| setSortByKey(newSortBy); | ||
| setOrderDirection('asc'); | ||
| } | ||
| const handleSearchChange = useCallback( | ||
| (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| debouncedSetSearch(e.target.value); | ||
| }, | ||
| [sortByKey, orderDirection], | ||
| [debouncedSetSearch], | ||
| ); | ||
|
|
||
| // Cleanup debounced function on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| debouncedSetSearch.cancel(); | ||
| }; | ||
| }, [debouncedSetSearch]); | ||
|
|
||
| return ( | ||
| <SubPageLayout> | ||
| <SubPageLayoutHeader | ||
| subPageTitle="List of organization members" | ||
| description="Manage the members of your organization and their permissions." | ||
| > | ||
| {organization.viewerCanManageInvitations && ( | ||
| <MemberInvitationButton | ||
| refetchInvitations={props.refetchMembers} | ||
| organization={organization} | ||
| <div className="flex flex-row gap-4"> | ||
| <Input | ||
| className="w-[220px] grow cursor-text" | ||
| placeholder="Search by username or email" | ||
| onChange={handleSearchChange} | ||
| defaultValue={search} | ||
| /> | ||
| )} | ||
| {organization.viewerCanManageInvitations && ( | ||
| <MemberInvitationButton | ||
| refetchInvitations={props.refetchMembers} | ||
| organization={organization} | ||
| /> | ||
| )} | ||
| </div> | ||
| </SubPageLayoutHeader> | ||
| <table className="w-full table-auto divide-y-[1px] divide-gray-500/20"> | ||
| <thead> | ||
| <tr> | ||
| <th | ||
| colSpan={2} | ||
| className="relative cursor-pointer select-none py-3 text-left text-sm font-semibold" | ||
| onClick={() => updateSorting('name')} | ||
| > | ||
| <th colSpan={2} className="relative select-none py-3 text-left text-sm font-semibold"> | ||
| Member | ||
| <span className="inline-block"> | ||
| {sortByKey === 'name' ? ( | ||
| orderDirection === 'asc' ? ( | ||
| <MoveUpIcon className="relative top-[3px] size-4" /> | ||
| ) : orderDirection === 'desc' ? ( | ||
| <MoveDownIcon className="relative top-[3px] size-4" /> | ||
| ) : null | ||
| ) : null} | ||
| </span> | ||
| </th> | ||
| <th | ||
| className="relative w-[300px] cursor-pointer select-none py-3 text-center align-middle text-sm font-semibold" | ||
| onClick={() => updateSorting('role')} | ||
| > | ||
| <th className="relative w-[300px] select-none py-3 text-center align-middle text-sm font-semibold"> | ||
| Assigned Role | ||
| <span className="inline-block"> | ||
| {sortByKey === 'role' ? ( | ||
| orderDirection === 'asc' ? ( | ||
| <MoveUpIcon className="relative top-[3px] size-4" /> | ||
| ) : orderDirection === 'desc' ? ( | ||
| <MoveDownIcon className="relative top-[3px] size-4" /> | ||
| ) : null | ||
| ) : null} | ||
| </span> | ||
| </th> | ||
| <th className="w-12 py-3 text-right text-sm font-semibold" /> | ||
| </tr> | ||
| </thead> | ||
| <tbody className="divide-y-[1px] divide-gray-500/20"> | ||
| {sortedMembers.map(node => ( | ||
| <OrganizationMemberRow | ||
| key={node.id} | ||
| refetchMembers={props.refetchMembers} | ||
| organization={props.organization} | ||
| member={node} | ||
| /> | ||
| {members.map(node => ( | ||
| <OrganizationMemberRow key={node.id} organization={props.organization} member={node} /> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| {/* Pagination Controls */} | ||
| <div className="mt-4 flex items-center justify-between"> | ||
| <div className="text-sm text-gray-500"> | ||
| Page {props.currentPage + 1} | ||
| {search && ` - search results for "${search}"`} | ||
| </div> | ||
| <div className="flex items-center gap-2"> | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={() => { | ||
| props.onPreviousPage(); | ||
| setTimeout(() => { | ||
| window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
|
Collaborator
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. should this scroll to the top of the list instead of all the way back to 0? |
||
| }, 0); | ||
| }} | ||
| disabled={props.currentPage === 0} | ||
| > | ||
| <ChevronLeftIcon className="mr-1 size-4" /> | ||
| Previous | ||
| </Button> | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={() => { | ||
| props.onNextPage(); | ||
| setTimeout(() => { | ||
| window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
| }, 0); | ||
| }} | ||
| disabled={!pageInfo?.hasNextPage} | ||
| > | ||
| Next | ||
| <ChevronRightIcon className="ml-1 size-4" /> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </SubPageLayout> | ||
| ); | ||
| } | ||
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
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.
We have a debounce helper you can use:
import { useDebouncedCallback } from 'use-debounce';