|
| 1 | +import { useContext, useRef, useState, useMemo } from 'react'; |
| 2 | +import { View, Text } from 'react-native'; |
| 3 | +import { observer } from 'mobx-react-lite'; |
| 4 | +import EmojiSelector from 'react-native-emoji-selector'; |
| 5 | +// import EmojiPicker from 'emoji-picker-react'; |
| 6 | + |
| 7 | +import type BottomSheetCore from '@gorhom/bottom-sheet'; |
| 8 | + |
| 9 | +import type { Message } from 'revolt.js'; |
| 10 | + |
| 11 | +import { client } from '@clerotri/lib/client'; |
| 12 | +import { showToast } from '@clerotri/lib/utils'; |
| 13 | +import { app, setFunction } from '@clerotri/Generic'; |
| 14 | +import { BottomSheet } from '@clerotri/components/common/BottomSheet'; |
| 15 | +import { ThemeContext } from '@clerotri/lib/themes'; |
| 16 | +import { useBackHandler } from '@clerotri/lib/ui'; |
| 17 | + |
| 18 | +export const AddReactionSheet = observer(() => { |
| 19 | + const { currentTheme } = useContext(ThemeContext); |
| 20 | + |
| 21 | + const [message, setMessage] = useState(null as Message | null); |
| 22 | + |
| 23 | + const sheetRef = useRef<BottomSheetCore>(null); |
| 24 | + |
| 25 | + useBackHandler(() => { |
| 26 | + if (message) { |
| 27 | + sheetRef.current?.close(); |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + return false; |
| 32 | + }); |
| 33 | + |
| 34 | + setFunction('openAddReaction', async (m: Message | null) => { |
| 35 | + setMessage(m); |
| 36 | + m ? sheetRef.current?.expand() : sheetRef.current?.close(); |
| 37 | + }); |
| 38 | + |
| 39 | + function selectEmoji(emoji: string) { |
| 40 | + if (!message) return; |
| 41 | + |
| 42 | + const reaction = message.reactions.get(emoji) || []; |
| 43 | + |
| 44 | + message.channel?.havePermission('React') |
| 45 | + ? !Array.from(reaction).includes(client.user?._id!) |
| 46 | + ? message.react(emoji) |
| 47 | + : message.unreact(emoji) |
| 48 | + : showToast('You cannot react to this message.'); |
| 49 | + app.openAddReaction(null); |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + // BottomSheet cannot wrap our children in a scroll view because it will |
| 54 | + // create a nested set of scroll views which causes issues |
| 55 | + <BottomSheet innerScroll={true} sheetRef={sheetRef}> |
| 56 | + <View style={{ paddingHorizontal: 16, flex: 1 }}> |
| 57 | + {!message ? ( |
| 58 | + <></> |
| 59 | + ) : ( |
| 60 | + <EmojiSelector |
| 61 | + onEmojiSelected={selectEmoji} |
| 62 | + columns={8} /> |
| 63 | + )} |
| 64 | + </View> |
| 65 | + </BottomSheet> |
| 66 | + ); |
| 67 | +}); |
0 commit comments