forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
241 additions
and
213 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
6 changes: 6 additions & 0 deletions
6
apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarSuggestion.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type UserAvatarSuggestion = { | ||
blob: string; | ||
contentType: string; | ||
service: string; | ||
url: string; | ||
}; |
48 changes: 18 additions & 30 deletions
48
apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarSuggestions.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 |
---|---|---|
@@ -1,43 +1,31 @@ | ||
import type { AvatarObject } from '@rocket.chat/core-typings'; | ||
import { Box, Button, Margins, Avatar } from '@rocket.chat/fuselage'; | ||
import { Button, Avatar } from '@rocket.chat/fuselage'; | ||
import React, { useCallback } from 'react'; | ||
|
||
import { useAvatarSuggestions } from '../../../hooks/useAvatarSuggestions'; | ||
import type { UserAvatarSuggestion } from './UserAvatarSuggestion'; | ||
import { useUserAvatarSuggestions } from './useUserAvatarSuggestions'; | ||
|
||
type UserAvatarSuggestionsProps = { | ||
setAvatarObj: (obj: AvatarObject) => void; | ||
setNewAvatarSource: (source: string) => void; | ||
disabled?: boolean; | ||
onSelectOne?: (suggestion: UserAvatarSuggestion) => void; | ||
}; | ||
|
||
const UserAvatarSuggestions = ({ setAvatarObj, setNewAvatarSource, disabled }: UserAvatarSuggestionsProps) => { | ||
const handleClick = useCallback( | ||
(suggestion) => () => { | ||
setAvatarObj(suggestion); | ||
setNewAvatarSource(suggestion.blob); | ||
}, | ||
[setAvatarObj, setNewAvatarSource], | ||
); | ||
function UserAvatarSuggestions({ disabled, onSelectOne }: UserAvatarSuggestionsProps) { | ||
const { data: suggestions = [] } = useUserAvatarSuggestions(); | ||
|
||
const { data } = useAvatarSuggestions(); | ||
const suggestions = Object.values(data?.suggestions || {}); | ||
const handleClick = useCallback((suggestion: UserAvatarSuggestion) => () => onSelectOne?.(suggestion), [onSelectOne]); | ||
|
||
return ( | ||
<Margins inline='x4'> | ||
{suggestions && | ||
suggestions.length > 0 && | ||
suggestions.map( | ||
(suggestion) => | ||
suggestion.blob && ( | ||
<Button key={suggestion.service} disabled={disabled} square onClick={handleClick(suggestion)}> | ||
<Box mie={4}> | ||
<Avatar title={suggestion.service} url={suggestion.blob as unknown as string} /> | ||
</Box> | ||
</Button> | ||
), | ||
)} | ||
</Margins> | ||
<> | ||
{suggestions.map( | ||
(suggestion) => | ||
suggestion.blob && ( | ||
<Button key={suggestion.service} square disabled={disabled} mi={4} onClick={handleClick(suggestion)}> | ||
<Avatar title={suggestion.service} url={suggestion.blob} /> | ||
</Button> | ||
), | ||
)} | ||
</> | ||
); | ||
}; | ||
} | ||
|
||
export default UserAvatarSuggestions; |
16 changes: 16 additions & 0 deletions
16
apps/meteor/client/components/avatar/UserAvatarEditor/readFileAsDataURL.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const readFileAsDataURL = (file: File) => | ||
new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onloadend = (event) => { | ||
const result = event.target?.result; | ||
if (typeof result === 'string') { | ||
resolve(result); | ||
return; | ||
} | ||
reject(new Error('Failed to read file')); | ||
}; | ||
reader.onerror = (event) => { | ||
reject(new Error(`Failed to read file: ${event}`)); | ||
}; | ||
reader.readAsDataURL(file); | ||
}); |
12 changes: 12 additions & 0 deletions
12
apps/meteor/client/components/avatar/UserAvatarEditor/useUserAvatarSuggestions.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useEndpoint } from '@rocket.chat/ui-contexts'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
export const useUserAvatarSuggestions = () => { | ||
const getAvatarSuggestions = useEndpoint('GET', '/v1/users.getAvatarSuggestion'); | ||
|
||
return useQuery({ | ||
queryKey: ['account', 'profile', 'avatar-suggestions'], | ||
queryFn: async () => getAvatarSuggestions(), | ||
select: (data) => Object.values(data.suggestions), | ||
}); | ||
}; |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ComponentProps } from 'react'; | ||
import React, { Suspense, lazy } from 'react'; | ||
import { useSyncExternalStore } from 'use-sync-external-store/shim'; | ||
|
||
const UserCard = lazy(() => import('./UserCard')); | ||
|
||
type UserCardHolderProps = { | ||
getProps: () => ComponentProps<typeof UserCard>; | ||
subscribeToProps: (callback: () => void) => () => void; | ||
}; | ||
|
||
function UserCardHolder({ getProps, subscribeToProps }: UserCardHolderProps) { | ||
const props = useSyncExternalStore(subscribeToProps, getProps); | ||
|
||
return ( | ||
<Suspense fallback={null}> | ||
<UserCard {...props} /> | ||
</Suspense> | ||
); | ||
} | ||
|
||
export default UserCardHolder; |
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.