-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uikit): added mini profile card
- Loading branch information
Showing
17 changed files
with
248 additions
and
54 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
11 changes: 9 additions & 2 deletions
11
packages/uikit-react-native/src/components/MessageRenderer/MessageIncomingAvatar.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
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
File renamed without changes.
File renamed without changes.
122 changes: 122 additions & 0 deletions
122
packages/uikit-react-native/src/contexts/ProfileCardCtx.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,122 @@ | ||
import React, { useContext, useState } from 'react'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
import { Modal, OutlinedButton, ProfileCard, createStyleSheet } from '@sendbird/uikit-react-native-foundation'; | ||
import type { | ||
SendbirdGroupChannel, | ||
SendbirdGroupChannelCreateParams, | ||
SendbirdMember, | ||
SendbirdUser, | ||
} from '@sendbird/uikit-utils'; | ||
import { Logger, PASS, useIIFE } from '@sendbird/uikit-utils'; | ||
|
||
import { LocalizationContext } from '../contexts/LocalizationCtx'; | ||
import { SendbirdChatContext } from '../contexts/SendbirdChatCtx'; | ||
|
||
type OnCreateChannel = (channel: SendbirdGroupChannel) => void; | ||
type OnBeforeCreateChannel = ( | ||
channelParams: SendbirdGroupChannelCreateParams, | ||
users: SendbirdUser[] | SendbirdMember[], | ||
) => SendbirdGroupChannelCreateParams | Promise<SendbirdGroupChannelCreateParams>; | ||
|
||
export type ProfileCardContextType = { | ||
show(user: SendbirdUser | SendbirdMember): void; | ||
hide(): void; | ||
}; | ||
|
||
type Props = React.PropsWithChildren<{ | ||
onCreateChannel?: OnCreateChannel; | ||
onBeforeCreateChannel?: OnBeforeCreateChannel; | ||
}>; | ||
|
||
export const ProfileCardContext = React.createContext<ProfileCardContextType | null>(null); | ||
export const ProfileCardProvider = ({ children, onCreateChannel, onBeforeCreateChannel = PASS }: Props) => { | ||
const chatContext = useContext(SendbirdChatContext); | ||
const localizationContext = useContext(LocalizationContext); | ||
const { bottom, left, right } = useSafeAreaInsets(); | ||
|
||
const [user, setUser] = useState<SendbirdUser | SendbirdMember>(); | ||
const [visible, setVisible] = useState(false); | ||
|
||
const show: ProfileCardContextType['show'] = (user) => { | ||
setUser(user); | ||
setVisible(true); | ||
}; | ||
|
||
const hide: ProfileCardContextType['hide'] = () => { | ||
setVisible(false); | ||
}; | ||
|
||
if (!chatContext) throw new Error('SendbirdChatContext is not provided'); | ||
if (!localizationContext) throw new Error('LocalizationContext is not provided'); | ||
|
||
const isMe = chatContext.currentUser && user?.userId === chatContext.currentUser.userId; | ||
const messageToUser = async () => { | ||
if (user) { | ||
const params: SendbirdGroupChannelCreateParams = { | ||
invitedUserIds: [user.userId], | ||
name: '', | ||
coverUrl: '', | ||
isDistinct: false, | ||
}; | ||
|
||
if (chatContext.currentUser) params.operatorUserIds = [chatContext.currentUser.userId]; | ||
const processedParams = await onBeforeCreateChannel(params, [user]); | ||
|
||
hide(); | ||
const channel = await chatContext.sdk.groupChannel.createChannel(processedParams); | ||
|
||
if (onCreateChannel) { | ||
onCreateChannel(channel); | ||
} else { | ||
Logger.warn( | ||
'Please set `onCreateChannel` before message to user from profile card, see `profileCard` prop in the `SendbirdUIKitContainer` props', | ||
); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<ProfileCardContext.Provider value={{ show, hide }}> | ||
{children} | ||
<Modal | ||
type={'slide'} | ||
onClose={hide} | ||
onDismiss={() => setUser(undefined)} | ||
visible={visible && Boolean(user)} | ||
backgroundStyle={styles.modal} | ||
> | ||
{user && ( | ||
<ProfileCard | ||
containerStyle={[ | ||
styles.profileCardContainer, | ||
{ paddingLeft: left, paddingRight: right, paddingBottom: bottom }, | ||
]} | ||
uri={user.profileUrl} | ||
username={user.nickname || localizationContext.STRINGS.LABELS.USER_NO_NAME} | ||
bodyLabel={localizationContext.STRINGS.PROFILE_CARD.BODY_LABEL} | ||
body={localizationContext.STRINGS.PROFILE_CARD.BODY(user)} | ||
button={useIIFE(() => { | ||
if (isMe) return undefined; | ||
return ( | ||
<OutlinedButton onPress={messageToUser}> | ||
{localizationContext.STRINGS.PROFILE_CARD.BUTTON_MESSAGE} | ||
</OutlinedButton> | ||
); | ||
})} | ||
/> | ||
)} | ||
</Modal> | ||
</ProfileCardContext.Provider> | ||
); | ||
}; | ||
|
||
const styles = createStyleSheet({ | ||
modal: { | ||
justifyContent: 'flex-end', | ||
}, | ||
profileCardContainer: { | ||
borderTopLeftRadius: 8, | ||
borderTopRightRadius: 8, | ||
}, | ||
}); |
File renamed without changes.
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.