-
Notifications
You must be signed in to change notification settings - Fork 252
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): Add list of suggestions in OrganizationSwitcher (#1577)
* feat(clerk-js): Add list of suggestions in OrganizationSwitcher * feat(clerk-js): Create a "small" size button * feat(clerk-js): Allow `automatic_suggestion` to be set for a verified domain * chore(clerk-js): Add changeset * fix(types): PublicOrganizationDataJSON should not extend ClerkResourceJSON
- Loading branch information
1 parent
a412a50
commit 1e117be
Showing
11 changed files
with
205 additions
and
13 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,9 @@ | ||
--- | ||
'@clerk/localizations': patch | ||
'@clerk/clerk-js': patch | ||
'@clerk/shared': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Introduces list of suggestions within <OrganizationSwitcher/> | ||
+ Users can request to join a suggested organization |
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
154 changes: 154 additions & 0 deletions
154
packages/clerk-js/src/ui/components/OrganizationSwitcher/UserSuggestionList.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,154 @@ | ||
import type { OrganizationSuggestionResource } from '@clerk/types'; | ||
|
||
import { useCoreOrganizationList } from '../../contexts'; | ||
import { Box, Button, descriptors, Flex, localizationKeys, Spinner, Text } from '../../customizables'; | ||
import { OrganizationPreview, useCardState, withCardStateProvider } from '../../elements'; | ||
import { useInView } from '../../hooks'; | ||
import { common } from '../../styledSystem'; | ||
import { handleError } from '../../utils'; | ||
|
||
export const UserSuggestionList = () => { | ||
const { userSuggestions } = useCoreOrganizationList({ | ||
userSuggestions: { | ||
infinite: true, | ||
}, | ||
}); | ||
|
||
const { ref } = useInView({ | ||
threshold: 0, | ||
onChange: inView => { | ||
if (inView) { | ||
userSuggestions.fetchNext?.(); | ||
} | ||
}, | ||
}); | ||
|
||
if ((userSuggestions.count ?? 0) === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Flex | ||
direction='col' | ||
elementDescriptor={descriptors.organizationSwitcherPopoverInvitationActions} | ||
> | ||
<Text | ||
variant='smallRegular' | ||
sx={t => ({ | ||
minHeight: 'unset', | ||
height: t.space.$12, | ||
padding: `${t.space.$3} ${t.space.$6}`, | ||
display: 'flex', | ||
alignItems: 'center', | ||
})} | ||
// Handle plurals | ||
localizationKey={localizationKeys( | ||
(userSuggestions.count ?? 0) > 1 | ||
? 'organizationSwitcher.suggestionCountLabel_many' | ||
: 'organizationSwitcher.suggestionCountLabel_single', | ||
{ | ||
count: userSuggestions.count, | ||
}, | ||
)} | ||
/> | ||
<Box | ||
sx={t => ({ | ||
maxHeight: `calc(4 * ${t.sizes.$12})`, | ||
overflowY: 'auto', | ||
...common.unstyledScrollbar(t), | ||
})} | ||
> | ||
{userSuggestions?.data?.map(inv => { | ||
return ( | ||
<SuggestionPreview | ||
key={inv.id} | ||
{...inv} | ||
/> | ||
); | ||
})} | ||
|
||
{(userSuggestions.hasNextPage || userSuggestions.isFetching) && ( | ||
<Box | ||
ref={ref} | ||
sx={t => ({ | ||
width: '100%', | ||
height: t.space.$12, | ||
position: 'relative', | ||
})} | ||
> | ||
<Box | ||
sx={{ | ||
margin: 'auto', | ||
position: 'absolute', | ||
left: '50%', | ||
top: '50%', | ||
transform: 'translateY(-50%) translateX(-50%)', | ||
}} | ||
> | ||
<Spinner | ||
size='md' | ||
colorScheme='primary' | ||
/> | ||
</Box> | ||
</Box> | ||
)} | ||
</Box> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const AcceptRejectSuggestionButtons = (props: OrganizationSuggestionResource) => { | ||
const card = useCardState(); | ||
const { userSuggestions } = useCoreOrganizationList({ | ||
userSuggestions: { | ||
infinite: true, | ||
}, | ||
}); | ||
|
||
const mutateSwrState = () => { | ||
(userSuggestions as any)?.unstable__mutate?.(); | ||
}; | ||
|
||
const handleAccept = () => { | ||
return card | ||
.runAsync(props.accept()) | ||
.then(mutateSwrState) | ||
.catch(err => handleError(err, [], card.setError)); | ||
}; | ||
|
||
return ( | ||
<Button | ||
elementDescriptor={descriptors.organizationSwitcherInvitationAcceptButton} | ||
textVariant='buttonExtraSmallBold' | ||
variant='solid' | ||
size='sm' | ||
isLoading={card.isLoading} | ||
onClick={handleAccept} | ||
localizationKey={localizationKeys('organizationSwitcher.suggestionsAccept')} | ||
/> | ||
); | ||
}; | ||
|
||
const SuggestionPreview = withCardStateProvider((props: OrganizationSuggestionResource) => { | ||
return ( | ||
<Flex | ||
align='center' | ||
gap={2} | ||
sx={t => ({ | ||
minHeight: 'unset', | ||
height: t.space.$12, | ||
justifyContent: 'space-between', | ||
padding: `0 ${t.space.$6}`, | ||
})} | ||
> | ||
<OrganizationPreview | ||
elementId='organizationSwitcher' | ||
avatarSx={t => ({ margin: `0 calc(${t.space.$3}/2)` })} | ||
organization={props.publicOrganizationData} | ||
size='sm' | ||
/> | ||
|
||
<AcceptRejectSuggestionButtons {...props} /> | ||
</Flex> | ||
); | ||
}); |
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