-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js,types,localizations): Search members on `OrganizationPr…
…ofile` (#4942)
- Loading branch information
1 parent
9dc8a67
commit a26cf0f
Showing
49 changed files
with
243 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
--- | ||
'@clerk/localizations': patch | ||
'@clerk/clerk-js': patch | ||
'@clerk/shared': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Introduced searching for members list on `OrganizationProfile` |
This file contains 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 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 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
105 changes: 105 additions & 0 deletions
105
packages/clerk-js/src/ui/components/OrganizationProfile/MembersSearch.tsx
This file contains 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,105 @@ | ||
import type { useOrganization } from '@clerk/shared/react'; | ||
import type { GetMembersParams } from '@clerk/types'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { descriptors, Flex, Icon, localizationKeys, useLocalizations } from '../../../ui/customizables'; | ||
import { Animated, InputWithIcon } from '../../../ui/elements'; | ||
import { MagnifyingGlass } from '../../../ui/icons'; | ||
import { Spinner } from '../../../ui/primitives'; | ||
import { ACTIVE_MEMBERS_PAGE_SIZE } from './OrganizationMembers'; | ||
|
||
type MembersSearchProps = { | ||
/** | ||
* Controlled query param state by parent component | ||
*/ | ||
query: GetMembersParams['query']; | ||
/** | ||
* Controlled input field value by parent component | ||
*/ | ||
value: string; | ||
/** | ||
* Paginated organization memberships | ||
*/ | ||
memberships: ReturnType<typeof useOrganization>['memberships']; | ||
/** | ||
* Handler for change event on input field | ||
*/ | ||
onSearchChange: (value: string) => void; | ||
/** | ||
* Handler for `query` value changes | ||
*/ | ||
onQueryTrigger: (query: string) => void; | ||
}; | ||
|
||
const membersSearchDebounceMs = 500; | ||
|
||
export const MembersSearch = ({ query, value, memberships, onSearchChange, onQueryTrigger }: MembersSearchProps) => { | ||
const { t } = useLocalizations(); | ||
|
||
const debounceTimer = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const eventValue = event.target.value; | ||
onSearchChange(eventValue); | ||
|
||
const shouldClearQuery = eventValue === ''; | ||
if (shouldClearQuery) { | ||
onQueryTrigger(eventValue); | ||
} | ||
}; | ||
|
||
// Debounce the input value changes until the user stops typing | ||
// to trigger the `query` param setter | ||
function handleKeyUp() { | ||
if (debounceTimer.current) { | ||
clearTimeout(debounceTimer.current); | ||
} | ||
|
||
debounceTimer.current = setTimeout(() => { | ||
onQueryTrigger(value.trim()); | ||
}, membersSearchDebounceMs); | ||
} | ||
|
||
// If search is not performed on a initial page, resets pagination offset | ||
// based on the response count | ||
useEffect(() => { | ||
if (!query || !memberships?.data) { | ||
return; | ||
} | ||
|
||
const hasOnePageLeft = (memberships?.count ?? 0) <= ACTIVE_MEMBERS_PAGE_SIZE; | ||
if (hasOnePageLeft) { | ||
memberships?.fetchPage?.(1); | ||
} | ||
}, [query, memberships]); | ||
|
||
const isFetchingNewData = value && !!memberships?.isLoading && !!memberships.data?.length; | ||
|
||
return ( | ||
<Animated asChild> | ||
<Flex sx={{ width: '100%' }}> | ||
<InputWithIcon | ||
value={value} | ||
type='search' | ||
autoCapitalize='none' | ||
spellCheck={false} | ||
aria-label='Search' | ||
placeholder={t(localizationKeys('organizationProfile.membersPage.action__search'))} | ||
leftIcon={ | ||
isFetchingNewData ? ( | ||
<Spinner size='xs' /> | ||
) : ( | ||
<Icon | ||
icon={MagnifyingGlass} | ||
elementDescriptor={descriptors.organizationProfileMembersSearchInputIcon} | ||
/> | ||
) | ||
} | ||
onKeyUp={handleKeyUp} | ||
onChange={handleChange} | ||
elementDescriptor={descriptors.organizationProfileMembersSearchInput} | ||
/> | ||
</Flex> | ||
</Animated> | ||
); | ||
}; |
This file contains 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 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 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 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 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 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 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 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.