-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use queryReactions to fetch and show reactions in OverlayReactions. #2532
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
440b409
fix: reaction list reactions sorting order based on created_at
khushal87 7414c3f
fix: show reactions using reactions_group
khushal87 3e8eb05
fix: tests
khushal87 1547041
fix: useProcessReactions hook
khushal87 e014bb2
fix: useProcessReactions hook and Channel message spread
khushal87 92f68e8
feat: use queryReactions API to query and show reactions in OverlayRe…
khushal87 772e2fd
feat: use queryReactions API to query and show reactions in OverlayRe…
khushal87 a25d90e
fix: improve useFetchReactions hook
khushal87 1fd162e
docs: old docs lint fix
khushal87 63b4fa4
chore: resolve conflicts from develop
khushal87 5b6f44c
fix: remove repeated code
khushal87 6a885b2
fix: remove repeated code
khushal87 7e55edd
fix: useFetchReactions hook
khushal87 550c3f7
fix: reactions query limit
khushal87 244f1d3
fix: use usememo in the reactions array
khushal87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ import React from 'react'; | |
import { StyleSheet, Text, useWindowDimensions, View, ViewStyle } from 'react-native'; | ||
import { FlatList } from 'react-native-gesture-handler'; | ||
import Animated, { interpolate, useAnimatedStyle, useSharedValue } from 'react-native-reanimated'; | ||
import Svg, { Circle } from 'react-native-svg'; | ||
|
||
import { useFetchReactions } from './hooks/useFetchReactions'; | ||
|
||
import { OverlayReactionsItem } from './OverlayReactionsItem'; | ||
|
||
import type { Alignment } from '../../contexts/messageContext/MessageContext'; | ||
import type { MessageOverlayContextValue } from '../../contexts/messageOverlayContext/MessageOverlayContext'; | ||
|
@@ -12,7 +15,6 @@ import { | |
LoveReaction, | ||
ThumbsDownReaction, | ||
ThumbsUpReaction, | ||
Unknown, | ||
WutReaction, | ||
} from '../../icons'; | ||
|
||
|
@@ -23,21 +25,6 @@ const styles = StyleSheet.create({ | |
avatarContainer: { | ||
padding: 8, | ||
}, | ||
avatarInnerContainer: { | ||
alignSelf: 'center', | ||
}, | ||
avatarName: { | ||
flex: 1, | ||
fontSize: 12, | ||
fontWeight: '700', | ||
paddingTop: 6, | ||
textAlign: 'center', | ||
}, | ||
avatarNameContainer: { | ||
alignItems: 'center', | ||
flexDirection: 'row', | ||
flexGrow: 1, | ||
}, | ||
container: { | ||
alignItems: 'center', | ||
borderRadius: 16, | ||
|
@@ -52,18 +39,6 @@ const styles = StyleSheet.create({ | |
alignItems: 'center', | ||
paddingBottom: 12, | ||
}, | ||
reactionBubble: { | ||
alignItems: 'center', | ||
borderRadius: 24, | ||
justifyContent: 'center', | ||
position: 'absolute', | ||
}, | ||
reactionBubbleBackground: { | ||
borderRadius: 24, | ||
height: 24, | ||
position: 'absolute', | ||
width: 24, | ||
}, | ||
title: { | ||
fontSize: 16, | ||
fontWeight: '700', | ||
|
@@ -109,58 +84,55 @@ export type Reaction = { | |
export type OverlayReactionsProps< | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, | ||
> = Pick<MessageOverlayContextValue<StreamChatGenerics>, 'OverlayReactionsAvatar'> & { | ||
reactions: Reaction[]; | ||
showScreen: Animated.SharedValue<number>; | ||
title: string; | ||
alignment?: Alignment; | ||
messageId?: string; | ||
reactions?: Reaction[]; | ||
supportedReactions?: ReactionData[]; | ||
}; | ||
|
||
type ReactionIconProps = Pick<Reaction, 'type'> & { | ||
pathFill: string; | ||
size: number; | ||
supportedReactions: ReactionData[]; | ||
}; | ||
|
||
const ReactionIcon = ({ pathFill, size, supportedReactions, type }: ReactionIconProps) => { | ||
const Icon = supportedReactions.find((reaction) => reaction.type === type)?.Icon || Unknown; | ||
return <Icon height={size} pathFill={pathFill} width={size} />; | ||
}; | ||
|
||
/** | ||
* OverlayReactions - A high level component which implements all the logic required for message overlay reactions | ||
*/ | ||
export const OverlayReactions = (props: OverlayReactionsProps) => { | ||
const [itemHeight, setItemHeight] = React.useState(0); | ||
const { | ||
alignment: overlayAlignment, | ||
messageId, | ||
OverlayReactionsAvatar, | ||
reactions, | ||
reactions: propReactions, | ||
showScreen, | ||
supportedReactions = reactionData, | ||
title, | ||
} = props; | ||
const layoutHeight = useSharedValue(0); | ||
const layoutWidth = useSharedValue(0); | ||
|
||
const [itemHeight, setItemHeight] = React.useState(0); | ||
const { | ||
loading, | ||
loadNextPage, | ||
reactions: fetchedReactions, | ||
} = useFetchReactions({ | ||
messageId, | ||
sort: { created_at: -1 }, | ||
}); | ||
|
||
const reactions = | ||
propReactions || | ||
(fetchedReactions.map((reaction) => ({ | ||
alignment: 'left', | ||
id: reaction.user?.id, | ||
image: reaction.user?.image, | ||
name: reaction.user?.name, | ||
type: reaction.type, | ||
})) as Reaction[]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should useMemo this I think.. so that we dont map unnecessarily if the fetched reactions are the same |
||
|
||
const { | ||
theme: { | ||
colors: { accent_blue, black, grey_gainsboro, white }, | ||
colors: { black, white }, | ||
overlay: { | ||
padding: overlayPadding, | ||
reactions: { | ||
avatarContainer, | ||
avatarName, | ||
avatarSize, | ||
container, | ||
flatListContainer, | ||
radius, | ||
reactionBubble, | ||
reactionBubbleBackground, | ||
reactionBubbleBorderRadius, | ||
title: titleStyle, | ||
}, | ||
reactions: { avatarContainer, avatarSize, container, flatListContainer, title: titleStyle }, | ||
}, | ||
}, | ||
} = useTheme(); | ||
|
@@ -185,100 +157,13 @@ export const OverlayReactions = (props: OverlayReactionsProps) => { | |
(avatarSize + (Number(avatarContainer.padding || 0) || styles.avatarContainer.padding) * 2), | ||
); | ||
|
||
const renderItem = ({ item }: { item: Reaction }) => { | ||
const { alignment = 'left', name, type } = item; | ||
const x = avatarSize / 2 - (avatarSize / (radius * 4)) * (alignment === 'left' ? 1 : -1); | ||
const y = avatarSize - radius; | ||
|
||
const left = | ||
alignment === 'left' | ||
? x - | ||
(Number(reactionBubbleBackground.width || 0) || styles.reactionBubbleBackground.width) + | ||
radius | ||
: x - radius; | ||
const top = | ||
y - | ||
radius - | ||
(Number(reactionBubbleBackground.height || 0) || styles.reactionBubbleBackground.height); | ||
|
||
return ( | ||
<View style={[styles.avatarContainer, avatarContainer]}> | ||
<View style={styles.avatarInnerContainer}> | ||
<OverlayReactionsAvatar reaction={item} size={avatarSize} /> | ||
<View style={[StyleSheet.absoluteFill]}> | ||
<Svg> | ||
<Circle | ||
cx={x - (radius * 2 - radius / 4) * (alignment === 'left' ? 1 : -1)} | ||
cy={y - radius * 2 - radius / 4} | ||
fill={alignment === 'left' ? grey_gainsboro : white} | ||
r={radius * 2} | ||
stroke={alignment === 'left' ? white : grey_gainsboro} | ||
strokeWidth={radius / 2} | ||
/> | ||
<Circle | ||
cx={x} | ||
cy={y} | ||
fill={alignment === 'left' ? grey_gainsboro : white} | ||
r={radius} | ||
stroke={alignment === 'left' ? white : grey_gainsboro} | ||
strokeWidth={radius / 2} | ||
/> | ||
</Svg> | ||
<View | ||
style={[ | ||
styles.reactionBubbleBackground, | ||
{ | ||
backgroundColor: alignment === 'left' ? grey_gainsboro : white, | ||
borderColor: alignment === 'left' ? white : grey_gainsboro, | ||
borderWidth: radius / 2, | ||
left, | ||
top, | ||
}, | ||
reactionBubbleBackground, | ||
]} | ||
/> | ||
<View style={[StyleSheet.absoluteFill]}> | ||
<Svg> | ||
<Circle | ||
cx={x - (radius * 2 - radius / 4) * (alignment === 'left' ? 1 : -1)} | ||
cy={y - radius * 2 - radius / 4} | ||
fill={alignment === 'left' ? grey_gainsboro : white} | ||
r={radius * 2 - radius / 2} | ||
/> | ||
</Svg> | ||
</View> | ||
<View | ||
style={[ | ||
styles.reactionBubble, | ||
{ | ||
backgroundColor: alignment === 'left' ? grey_gainsboro : white, | ||
height: | ||
(reactionBubbleBorderRadius || styles.reactionBubble.borderRadius) - radius / 2, | ||
left, | ||
top, | ||
width: | ||
(reactionBubbleBorderRadius || styles.reactionBubble.borderRadius) - radius / 2, | ||
}, | ||
reactionBubble, | ||
]} | ||
> | ||
<ReactionIcon | ||
pathFill={accent_blue} | ||
size={(reactionBubbleBorderRadius || styles.reactionBubble.borderRadius) / 2} | ||
supportedReactions={supportedReactions} | ||
type={type} | ||
/> | ||
</View> | ||
</View> | ||
</View> | ||
<View style={styles.avatarNameContainer}> | ||
<Text numberOfLines={2} style={[styles.avatarName, { color: black }, avatarName]}> | ||
{name} | ||
</Text> | ||
</View> | ||
</View> | ||
); | ||
}; | ||
const renderItem = ({ item }: { item: Reaction }) => ( | ||
<OverlayReactionsItem | ||
OverlayReactionsAvatar={OverlayReactionsAvatar} | ||
reaction={item} | ||
supportedReactions={supportedReactions} | ||
/> | ||
); | ||
|
||
const showScreenStyle = useAnimatedStyle<ViewStyle>( | ||
() => ({ | ||
|
@@ -316,33 +201,38 @@ export const OverlayReactions = (props: OverlayReactionsProps) => { | |
]} | ||
> | ||
<Text style={[styles.title, { color: black }, titleStyle]}>{title}</Text> | ||
<FlatList | ||
contentContainerStyle={styles.flatListContentContainer} | ||
data={filteredReactions} | ||
key={numColumns} | ||
keyExtractor={({ name }, index) => `${name}_${index}`} | ||
numColumns={numColumns} | ||
renderItem={renderItem} | ||
scrollEnabled={filteredReactions.length / numColumns > 1} | ||
style={[ | ||
styles.flatListContainer, | ||
flatListContainer, | ||
{ | ||
// we show the item height plus a little extra to tease for scrolling if there are more than one row | ||
maxHeight: | ||
itemHeight + (filteredReactions.length / numColumns > 1 ? itemHeight / 4 : 8), | ||
}, | ||
]} | ||
/> | ||
{!loading && ( | ||
<FlatList | ||
contentContainerStyle={styles.flatListContentContainer} | ||
data={filteredReactions} | ||
key={numColumns} | ||
keyExtractor={({ id, name }, index) => `${name}${id}_${index}`} | ||
numColumns={numColumns} | ||
onEndReached={loadNextPage} | ||
renderItem={renderItem} | ||
scrollEnabled={filteredReactions.length / numColumns > 1} | ||
style={[ | ||
styles.flatListContainer, | ||
flatListContainer, | ||
{ | ||
// we show the item height plus a little extra to tease for scrolling if there are more than one row | ||
maxHeight: | ||
itemHeight + (filteredReactions.length / numColumns > 1 ? itemHeight / 4 : 8), | ||
}, | ||
]} | ||
/> | ||
)} | ||
{/* The below view is unseen by the user, we use it to compute the height that the item must be */} | ||
<View | ||
onLayout={({ nativeEvent: { layout } }) => { | ||
setItemHeight(layout.height); | ||
}} | ||
style={[styles.unseenItemContainer, styles.flatListContentContainer]} | ||
> | ||
{renderItem({ item: filteredReactions[0] })} | ||
</View> | ||
{!loading && ( | ||
<View | ||
onLayout={({ nativeEvent: { layout } }) => { | ||
setItemHeight(layout.height); | ||
}} | ||
style={[styles.unseenItemContainer, styles.flatListContentContainer]} | ||
> | ||
{renderItem({ item: filteredReactions[0] })} | ||
</View> | ||
)} | ||
</Animated.View> | ||
</> | ||
); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move this to a const outside, so that inside the hook useMEmo stringify does not trigger again and again..
its a simple optimisation