|
| 1 | +import './index.scss'; |
| 2 | + |
| 3 | +import React, { |
| 4 | + ReactElement, |
| 5 | + useRef, |
| 6 | + useState, |
| 7 | + useCallback, |
| 8 | +} from 'react'; |
| 9 | +import type SendBird from 'sendbird'; |
| 10 | + |
| 11 | +import ContextMenu, { MenuItems } from '../ContextMenu'; |
| 12 | +import Label, { LabelTypography, LabelColors } from '../Label'; |
| 13 | +import UserProfile from '../UserProfile'; |
| 14 | +import useSendbirdStateContext from '../../hooks/useSendbirdStateContext'; |
| 15 | + |
| 16 | +interface MentionLabelProps { |
| 17 | + mentionTemplate: string; |
| 18 | + mentionedUserId: string; |
| 19 | + isByMe: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +export default function MentionLabel(props: MentionLabelProps): JSX.Element { |
| 23 | + const { |
| 24 | + mentionTemplate, |
| 25 | + mentionedUserId, |
| 26 | + isByMe, |
| 27 | + } = props; |
| 28 | + |
| 29 | + const mentionRef = useRef(); |
| 30 | + |
| 31 | + const sendbirdState = useSendbirdStateContext(); |
| 32 | + const userId = sendbirdState?.config?.userId; |
| 33 | + const sdk = sendbirdState?.stores?.sdkStore?.sdk; |
| 34 | + const amIBeingMentioned = userId === mentionedUserId; |
| 35 | + const [user, setUser] = useState<SendBird.User| null>(); |
| 36 | + const fetchUser = useCallback( |
| 37 | + (toggleDropdown) => { |
| 38 | + if (user) { |
| 39 | + toggleDropdown(); |
| 40 | + } |
| 41 | + const query = sdk.createApplicationUserListQuery(); |
| 42 | + query.userIdsFilter = [mentionedUserId]; |
| 43 | + query.next((members) => { |
| 44 | + if (members?.length > 0) { |
| 45 | + setUser(members[0]); |
| 46 | + } |
| 47 | + toggleDropdown(); |
| 48 | + }) |
| 49 | + }, |
| 50 | + [sdk, mentionedUserId], |
| 51 | + ); |
| 52 | + return ( |
| 53 | + <ContextMenu |
| 54 | + menuTrigger={(toggleDropdown: () => void): ReactElement => ( |
| 55 | + <a |
| 56 | + className={` |
| 57 | + sendbird-word__mention |
| 58 | + ${amIBeingMentioned ? 'sendbird-word__mention--me' : ''} |
| 59 | + `} |
| 60 | + onClick={() => fetchUser(toggleDropdown)} |
| 61 | + ref={mentionRef} |
| 62 | + > |
| 63 | + <Label |
| 64 | + type={LabelTypography.CAPTION_1} |
| 65 | + color={isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1} |
| 66 | + > |
| 67 | + {`${mentionTemplate}${mentionedUserId}`} |
| 68 | + </Label> |
| 69 | + </a> |
| 70 | + )} |
| 71 | + menuItems={(closeDropdown: () => void): ReactElement => ( |
| 72 | + <MenuItems |
| 73 | + /** |
| 74 | + * parentRef: For catching location(x, y) of MenuItems |
| 75 | + * parentContainRef: For toggling more options(menus & reactions) |
| 76 | + */ |
| 77 | + parentRef={mentionRef} |
| 78 | + parentContainRef={mentionRef} |
| 79 | + closeDropdown={closeDropdown} |
| 80 | + style={{ paddingTop: 0, paddingBottom: 0 }} |
| 81 | + > |
| 82 | + <UserProfile disableMessaging user={user} onSuccess={closeDropdown} /> |
| 83 | + </MenuItems> |
| 84 | + )} |
| 85 | + /> |
| 86 | + ) |
| 87 | +} |
0 commit comments