Skip to content

Commit cb429a1

Browse files
committed
fix: long press on poll message issues
1 parent e258bce commit cb429a1

File tree

6 files changed

+78
-26
lines changed

6 files changed

+78
-26
lines changed

package/src/components/Message/Message.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useState } from 'react';
1+
import React, { useCallback, useMemo, useState } from 'react';
22
import { GestureResponderEvent, Keyboard, StyleProp, View, ViewStyle } from 'react-native';
33

44
import type { Attachment, UserResponse } from 'stream-chat';
@@ -54,6 +54,7 @@ import {
5454
MessageType,
5555
} from '../MessageList/hooks/useMessageList';
5656
import type { MessageActionListItemProps } from '../MessageOverlay/MessageActionListItem';
57+
import { Poll as PollComponent } from '../Poll';
5758

5859
export type TouchableEmitter =
5960
| 'fileAttachment'
@@ -562,6 +563,20 @@ const MessageWithContext = <
562563
});
563564

564565
const { userLanguage } = useTranslationContext();
566+
const { Poll, PollButtons, PollHeader } = useChannelContext();
567+
568+
const PollWrapper = useCallback(() => {
569+
const poll = message?.poll_id ? client.polls.fromState(message.poll_id) : undefined;
570+
return message?.poll_id && poll ? (
571+
<PollComponent<StreamChatGenerics>
572+
message={message}
573+
poll={poll}
574+
Poll={Poll}
575+
PollButtons={PollButtons}
576+
PollHeader={PollHeader}
577+
/>
578+
) : null;
579+
}, [PollButtons, PollHeader, Poll, client, message]);
565580

566581
const showMessageOverlay = async (isMessageActionsVisible = true, error = errorOrFailed) => {
567582
await dismissKeyboard();
@@ -613,6 +628,7 @@ const MessageWithContext = <
613628
otherAttachments: attachments.other,
614629
OverlayReactionList,
615630
ownCapabilities,
631+
Poll: PollWrapper,
616632
supportedReactions,
617633
threadList,
618634
userLanguage,

package/src/components/Message/MessageSimple/MessageContent.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
import { MessageTextContainer } from './MessageTextContainer';
1111

12-
import { useChatContext } from '../../../contexts';
12+
import { useChannelContext, useChatContext } from '../../../contexts';
1313
import {
1414
MessageContextValue,
1515
useMessageContext,
@@ -145,6 +145,7 @@ const MessageContentWithContext = <
145145
threadList,
146146
} = props;
147147
const { client } = useChatContext();
148+
const { Poll: PollOverride, PollButtons, PollHeader } = useChannelContext();
148149

149150
const {
150151
theme: {
@@ -383,7 +384,16 @@ const MessageContentWithContext = <
383384
case 'poll': {
384385
const pollId = message.poll_id;
385386
const poll = pollId && client.polls.fromState(pollId);
386-
return pollId && poll ? <Poll key={`poll_${message.poll_id}`} poll={poll} /> : null;
387+
return pollId && poll ? (
388+
<Poll
389+
key={`poll_${message.poll_id}`}
390+
message={message}
391+
poll={poll}
392+
Poll={PollOverride}
393+
PollButtons={PollButtons}
394+
PollHeader={PollHeader}
395+
/>
396+
) : null;
387397
}
388398
case 'text':
389399
default:

package/src/components/MessageOverlay/MessageOverlay.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { MessageActionList as DefaultMessageActionList } from './MessageActionLi
2525
import { OverlayReactionList as OverlayReactionListDefault } from './OverlayReactionList';
2626
import { OverlayReactionsAvatar as OverlayReactionsAvatarDefault } from './OverlayReactionsAvatar';
2727

28+
import { OwnCapabilitiesProvider } from '../../contexts';
2829
import { ChatProvider } from '../../contexts/chatContext/ChatContext';
2930
import { MessageProvider } from '../../contexts/messageContext/MessageContext';
3031
import {
@@ -135,6 +136,7 @@ const MessageOverlayWithContext = <
135136
OverlayReactions = DefaultOverlayReactions,
136137
OverlayReactionsAvatar = OverlayReactionsAvatarDefault,
137138
ownCapabilities,
139+
Poll,
138140
setOverlay,
139141
threadList,
140142
videos,
@@ -441,6 +443,14 @@ const MessageOverlayWithContext = <
441443
/>
442444
)
443445
);
446+
case 'poll': {
447+
const pollId = message.poll_id;
448+
return Poll && pollId && ownCapabilities ? (
449+
<OwnCapabilitiesProvider key={`poll_${pollId}`} value={ownCapabilities}>
450+
<Poll />
451+
</OwnCapabilitiesProvider>
452+
) : null;
453+
}
444454
case 'text':
445455
default:
446456
return otherAttachments?.length && otherAttachments[0].actions ? null : (

package/src/components/Poll/Poll.tsx

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo } from 'react';
22
import { StyleSheet, Text, View } from 'react-native';
33

4-
import { Poll as PollClass, PollOption as PollOptionClass } from 'stream-chat';
4+
import { PollOption as PollOptionClass } from 'stream-chat';
55

66
import {
77
AddCommentButton,
@@ -16,12 +16,19 @@ import {
1616
import { usePollState } from './hooks/usePollState';
1717

1818
import {
19+
ChannelContextValue,
1920
PollContextProvider,
20-
useChannelContext,
21-
useMessageContext,
21+
PollContextValue,
22+
usePollContext,
2223
useTheme,
2324
useTranslationContext,
2425
} from '../../contexts';
26+
import type { DefaultStreamChatGenerics } from '../../types/types';
27+
28+
export type PollProps<
29+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
30+
> = Pick<PollContextValue<StreamChatGenerics>, 'poll' | 'message' | 'PollHeader' | 'PollButtons'> &
31+
Pick<ChannelContextValue<StreamChatGenerics>, 'Poll'>;
2532

2633
export const PollButtons = () => (
2734
<>
@@ -64,7 +71,7 @@ export const PollHeader = () => {
6471
};
6572

6673
const PollWithContext = () => {
67-
const { PollButtons: PollButtonsOverride, PollHeader: PollHeaderOverride } = useChannelContext();
74+
const { PollButtons: PollButtonsOverride, PollHeader: PollHeaderOverride } = usePollContext();
6875
const { options } = usePollState();
6976

7077
const {
@@ -88,21 +95,26 @@ const PollWithContext = () => {
8895
);
8996
};
9097

91-
export const Poll = ({ poll }: { poll: PollClass }) => {
92-
const { Poll: PollOverride } = useChannelContext();
93-
const { message } = useMessageContext();
94-
95-
return (
96-
<PollContextProvider
97-
value={{
98-
message,
99-
poll,
100-
}}
101-
>
102-
{PollOverride ? <PollOverride /> : <PollWithContext />}
103-
</PollContextProvider>
104-
);
105-
};
98+
export const Poll = <
99+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
100+
>({
101+
message,
102+
poll,
103+
Poll: PollOverride,
104+
PollButtons,
105+
PollHeader,
106+
}: PollProps<StreamChatGenerics>) => (
107+
<PollContextProvider
108+
value={{
109+
message,
110+
poll,
111+
PollButtons,
112+
PollHeader,
113+
}}
114+
>
115+
{PollOverride ? <PollOverride /> : <PollWithContext />}
116+
</PollContextProvider>
117+
);
106118

107119
const styles = StyleSheet.create({
108120
container: { padding: 15, width: 270 },

package/src/contexts/messageOverlayContext/MessageOverlayContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { PropsWithChildren, useContext } from 'react';
22

33
import type { ImageProps } from 'react-native';
44

5-
import type { Attachment, TranslationLanguages } from 'stream-chat';
5+
import { Attachment, TranslationLanguages } from 'stream-chat';
66

77
import { useResettableState } from './hooks/useResettableState';
88

@@ -46,6 +46,7 @@ export type MessageOverlayData<
4646
otherAttachments?: Attachment<StreamChatGenerics>[];
4747
OverlayReactionList?: React.ComponentType<OverlayReactionListProps<StreamChatGenerics>>;
4848
ownCapabilities?: OwnCapabilitiesContextValue;
49+
Poll?: React.ComponentType;
4950
supportedReactions?: ReactionData[];
5051
threadList?: boolean;
5152
userLanguage?: TranslationLanguages;

package/src/contexts/pollContext/pollContext.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Poll } from 'stream-chat';
44

55
import { MessageType } from '../../components';
66
import type { DefaultStreamChatGenerics } from '../../types/types';
7+
import { ChannelContextValue } from '../channelContext/ChannelContext';
78
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
89

910
import { isTestEnvironment } from '../utils/isTestEnvironment';
@@ -13,15 +14,17 @@ export type PollContextValue<
1314
> = {
1415
message: MessageType<StreamChatGenerics>;
1516
poll: Poll<StreamChatGenerics>;
16-
};
17+
} & Pick<ChannelContextValue, 'PollButtons' | 'PollHeader'>;
1718

1819
export const PollContext = React.createContext(DEFAULT_BASE_CONTEXT_VALUE as PollContextValue);
1920

20-
export const PollContextProvider = ({
21+
export const PollContextProvider = <
22+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
23+
>({
2124
children,
2225
value,
2326
}: PropsWithChildren<{
24-
value: PollContextValue;
27+
value: PollContextValue<StreamChatGenerics>;
2528
}>) => (
2629
<PollContext.Provider value={value as unknown as PollContextValue}>
2730
{children}

0 commit comments

Comments
 (0)