-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Add federated users on channel creation (#25986)
* convert useHasLicense to TS * Make federation setting public * Allow creation of federated channels * Create new autocomplete * Fix state * Fix broken types * Icons * Change Federated hint * fix: fix errors due to the models migration * fix: fix lint Co-authored-by: Marcos Defendi <marcos.defendi@rocket.chat>
- Loading branch information
1 parent
d7d3ca1
commit 2299887
Showing
23 changed files
with
311 additions
and
132 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
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
89 changes: 89 additions & 0 deletions
89
apps/meteor/client/components/UserAutoCompleteMultiple/UserAutoCompleteMultipleFederated.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,89 @@ | ||
import { MultiSelectFiltered, Icon, Box, Chip } from '@rocket.chat/fuselage'; | ||
import type { Options } from '@rocket.chat/fuselage'; | ||
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks'; | ||
import { useEndpoint } from '@rocket.chat/ui-contexts'; | ||
import React, { memo, ReactElement, useState, ComponentProps } from 'react'; | ||
import { useQuery } from 'react-query'; | ||
|
||
import UserAvatar from '../avatar/UserAvatar'; | ||
import renderOptions from './UserAutoCompleteMultipleOptions'; | ||
|
||
type UserAutoCompleteMultipleFederatedProps = { | ||
onChange: (value: Array<string>) => void; | ||
value: Array<string>; | ||
placeholder?: string; | ||
}; | ||
|
||
export type UserAutoCompleteOptionType = { | ||
name: string; | ||
username: string; | ||
_federated?: boolean; | ||
}; | ||
|
||
type UserAutoCompleteOptions = { | ||
[k: string]: UserAutoCompleteOptionType; | ||
}; | ||
|
||
const matrixRegex = new RegExp('(.*:.*)', 'gi'); | ||
|
||
const UserAutoCompleteMultipleFederated = ({ | ||
onChange, | ||
value, | ||
placeholder, | ||
...props | ||
}: UserAutoCompleteMultipleFederatedProps): ReactElement => { | ||
const [filter, setFilter] = useState(''); | ||
const [selectedCache, setSelectedCache] = useState<UserAutoCompleteOptions>({}); | ||
|
||
const debouncedFilter = useDebouncedValue(filter, 1000); | ||
const getUsers = useEndpoint('GET', '/v1/users.autocomplete'); | ||
|
||
const { data } = useQuery(['users.autocomplete', debouncedFilter], async () => { | ||
const users = await getUsers({ selector: JSON.stringify({ term: debouncedFilter }) }); | ||
const options = users.items.map((item): [string, UserAutoCompleteOptionType] => [item.username, item]); | ||
|
||
// Add extra option if filter text matches `username:server` | ||
// Used to add federated users that do not exist yet | ||
if (matrixRegex.test(debouncedFilter)) { | ||
options.unshift([debouncedFilter, { name: debouncedFilter, username: debouncedFilter, _federated: true }]); | ||
} | ||
|
||
return options; | ||
}); | ||
|
||
const options = data || []; | ||
|
||
const onAddSelected: ComponentProps<typeof Options>['onSelect'] = ([value]) => { | ||
const cachedOption = options.find(([curVal]) => curVal === value)?.[1]; | ||
if (!cachedOption) { | ||
throw new Error('UserAutoCompleteMultiple - onAddSelected - failed to cache option'); | ||
} | ||
setSelectedCache({ ...selectedCache, [value]: cachedOption }); | ||
}; | ||
|
||
return ( | ||
<MultiSelectFiltered | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={onChange} | ||
filter={filter} | ||
setFilter={setFilter} | ||
renderSelected={({ value, onMouseDown }: { value: string; onMouseDown: () => void }): ReactElement => { | ||
const currentCachedOption = selectedCache[value]; | ||
|
||
return ( | ||
<Chip key={value} {...props} height='x20' onMouseDown={onMouseDown} mie='x4' mb='x2'> | ||
{currentCachedOption._federated ? <Icon size='x20' name='globe' /> : <UserAvatar size='x20' username={value} />} | ||
<Box is='span' margin='none' mis='x4'> | ||
{currentCachedOption.name || currentCachedOption.username} | ||
</Box> | ||
</Chip> | ||
); | ||
}} | ||
renderOptions={renderOptions(options, onAddSelected)} | ||
options={options.concat(Object.entries(selectedCache)).map(([, item]) => [item.username, item.name || item.username])} | ||
/> | ||
); | ||
}; | ||
|
||
export default memo(UserAutoCompleteMultipleFederated); |
31 changes: 31 additions & 0 deletions
31
apps/meteor/client/components/UserAutoCompleteMultiple/UserAutoCompleteMultipleOption.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,31 @@ | ||
import { IUser } from '@rocket.chat/core-typings'; | ||
import { Option, OptionDescription } from '@rocket.chat/fuselage'; | ||
import React, { ReactElement } from 'react'; | ||
|
||
import UserAvatar from '../avatar/UserAvatar'; | ||
|
||
type UserAutoCompleteMultipleOptionProps = { | ||
label: { | ||
_federated?: boolean; | ||
} & Pick<IUser, 'username' | 'name'>; | ||
}; | ||
|
||
const UserAutoCompleteMultipleOption = ({ label, ...props }: UserAutoCompleteMultipleOptionProps): ReactElement => { | ||
const { name, username, _federated } = label; | ||
|
||
return ( | ||
<Option | ||
{...props} | ||
avatar={_federated ? undefined : <UserAvatar username={username || ''} size='x20' />} | ||
icon={_federated ? 'globe' : undefined} | ||
key={username} | ||
label={ | ||
<> | ||
{name || username} {!_federated && <OptionDescription>({username})</OptionDescription>} | ||
</> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default UserAutoCompleteMultipleOption; |
31 changes: 31 additions & 0 deletions
31
apps/meteor/client/components/UserAutoCompleteMultiple/UserAutoCompleteMultipleOptions.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,31 @@ | ||
import { Options } from '@rocket.chat/fuselage'; | ||
import React, { forwardRef, ComponentProps, RefAttributes, ReactElement, Ref } from 'react'; | ||
|
||
import { UserAutoCompleteOptionType } from './UserAutoCompleteMultipleFederated'; | ||
import UserAutoCompleteMultipleOption from './UserAutoCompleteMultipleOption'; | ||
|
||
type Options = Array<[UserAutoCompleteOptionType['username'], UserAutoCompleteOptionType]>; | ||
|
||
const renderOptions = ( | ||
options: Options, | ||
onSelect: ComponentProps<typeof Options>['onSelect'], | ||
): ((props: ComponentProps<typeof Options> & RefAttributes<HTMLElement>) => ReactElement | null) => | ||
forwardRef(function UserAutoCompleteMultipleOptions( | ||
{ onSelect: _onSelect, ...props }: ComponentProps<typeof Options>, | ||
ref: Ref<HTMLElement>, | ||
): ReactElement { | ||
return ( | ||
<Options | ||
{...props} | ||
options={options} | ||
onSelect={(val): void => { | ||
onSelect(val); | ||
_onSelect(val); | ||
}} | ||
ref={ref} | ||
renderItem={UserAutoCompleteMultipleOption} | ||
/> | ||
); | ||
}); | ||
|
||
export default renderOptions; |
2 changes: 1 addition & 1 deletion
2
...eteor/client/hooks/usePreventProgation.ts → ...eor/client/hooks/usePreventPropagation.ts
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.