Skip to content

Commit f8c926f

Browse files
author
HuyDo
committed
fix: add send link to addons
1 parent 4f052f5 commit f8c926f

File tree

10 files changed

+34
-43
lines changed

10 files changed

+34
-43
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ Then using our Addons component in ChatScreen
112112
```javascript
113113
import React from 'react'
114114
import {ChatScreen as BaseChatScreen} from 'rn-firebase-chat'
115-
import {CameraView, useCamera} from 'rn-firebase-chat/src/addons/camera'
115+
import {
116+
CameraView,
117+
useCamera,
118+
CustomBubbleWithLinkPreview,
119+
} from 'rn-firebase-chat/src/addons';
116120

117121
...
118122

@@ -122,6 +126,13 @@ export const ChatScreen: React.FC = () => {
122126
<BaseChatScreen
123127
memberIds={[partnerInfo.id]}
124128
partners={[partnerInfo]}
129+
//Add custom link preview here
130+
customLinkPreview={(bubbleMessage, viewStatus) => (
131+
<View>
132+
<CustomBubbleWithLinkPreview bubbleMessage={bubbleMessage} />
133+
{viewStatus}
134+
</View>
135+
)}
125136
inputToolbarProps={{
126137
hasCamera: true,
127138
hasGallery: true,

src/addons/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './camera';
2+
export * from './linkPreview';
File renamed without changes.

src/chat/components/bubble/CustomBubbleWithLinkPreview.tsx renamed to src/addons/linkPreview/bubble/CustomBubbleWithLinkPreview.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'react-native';
1010
import { Bubble, BubbleProps } from 'react-native-gifted-chat';
1111
import type { MessageProps } from '../../../interfaces';
12-
import { LinkPreview } from '../linkPreview';
12+
import { LinkPreview } from '../../../addons';
1313

1414
export interface ICustomBubbleWithLinkPreviewStyles {
1515
customContainerStyle?: StyleProp<ViewStyle>;
@@ -24,7 +24,6 @@ interface ICustomBubbleWithLinkPreviewProps {
2424
urls: string[],
2525
bubbleMessage: BubbleProps<MessageProps>
2626
) => JSX.Element;
27-
enableLinkPreview: boolean;
2827
}
2928

3029
const urlRegex = /(https?:\/\/[^\s]+)/g;
@@ -40,7 +39,6 @@ export const CustomBubbleWithLinkPreview: React.FC<
4039
bubbleMessage,
4140
customBubbleWithLinkPreviewStyles,
4241
customBubbleWithLinkPreview,
43-
enableLinkPreview,
4442
} = props;
4543
const { currentMessage } = bubbleMessage;
4644
const urls = currentMessage?.text.match(urlRegex);
@@ -92,7 +90,7 @@ export const CustomBubbleWithLinkPreview: React.FC<
9290
{!!currentMessage?.text &&
9391
renderTextWithLinks(currentMessage.text)}
9492
</Text>
95-
{!!firstUrl && enableLinkPreview && (
93+
{!!firstUrl && (
9694
<LinkPreview
9795
containerStyle={[
9896
styles.previewContainer,
@@ -112,7 +110,6 @@ export const CustomBubbleWithLinkPreview: React.FC<
112110
customContainerStyle,
113111
customMessagePreviewStyle,
114112
customPreviewContainerStyle,
115-
enableLinkPreview,
116113
renderTextWithLinks,
117114
]
118115
);

src/addons/linkPreview/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { LinkPreview } from './LinkPreview';
2+
import { CustomBubbleWithLinkPreview } from './bubble/CustomBubbleWithLinkPreview';
3+
4+
export { LinkPreview, CustomBubbleWithLinkPreview };
File renamed without changes.
File renamed without changes.

src/chat/ChatScreen.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import InputToolbar, { IInputToolbar } from './components/InputToolbar';
4444
type ChildrenProps = {
4545
onSend: (messages: MessageProps) => Promise<void>;
4646
};
47-
import { ICustomBubbleWithLinkPreviewStyles } from './components/bubble/CustomBubbleWithLinkPreview';
4847

4948
interface ChatScreenProps extends GiftedChatProps {
5049
style?: StyleProp<ViewStyle>;
@@ -67,12 +66,10 @@ interface ChatScreenProps extends GiftedChatProps {
6766
messageStatusEnable?: boolean;
6867
customMessageStatus?: (hasUnread: boolean) => JSX.Element;
6968
children?: (props: ChildrenProps) => ReactNode | ReactNode;
70-
customLinkPreviewStyles?: ICustomBubbleWithLinkPreviewStyles;
71-
customLinkPreview: (
72-
urls: string[],
73-
bubbleMessage: BubbleProps<MessageProps>
74-
) => JSX.Element;
75-
enableLinkPreview?: boolean;
69+
customLinkPreview?: (
70+
bubbleMessage: BubbleProps<MessageProps>,
71+
viewMessageStatus: ReactNode
72+
) => React.ReactNode;
7673
}
7774

7875
export const ChatScreen: React.FC<ChatScreenProps> = ({
@@ -91,9 +88,7 @@ export const ChatScreen: React.FC<ChatScreenProps> = ({
9188
enableTyping = true,
9289
typingTimeoutSeconds = DEFAULT_TYPING_TIMEOUT_SECONDS,
9390
messageStatusEnable = true,
94-
customLinkPreviewStyles,
9591
customLinkPreview,
96-
enableLinkPreview = true,
9792
...props
9893
}) => {
9994
const { userInfo, chatDispatch } = useChatContext();
@@ -318,9 +313,7 @@ export const ChatScreen: React.FC<ChatScreenProps> = ({
318313
unReadSeenMessage={props.unReadSeenMessage}
319314
customMessageStatus={props.customMessageStatus}
320315
messageStatusEnable={messageStatusEnable}
321-
customLinkPreviewStyles={customLinkPreviewStyles}
322316
customLinkPreview={customLinkPreview}
323-
enableLinkPreview={enableLinkPreview}
324317
/>
325318
);
326319
};

src/chat/components/bubble/CustomBubble.tsx

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { ReactNode } from 'react';
22
import { StyleProp, StyleSheet, Text, View, ViewStyle } from 'react-native';
33
import { MessageTypes, type MessageProps } from '../../../interfaces';
44
import { Bubble, BubbleProps } from 'react-native-gifted-chat';
@@ -7,11 +7,6 @@ import {
77
CustomImageVideoBubbleProps,
88
} from './CustomImageVideoBubble';
99
import MessageStatus from '../MessageStatus';
10-
import {
11-
ICustomBubbleWithLinkPreviewStyles,
12-
CustomBubbleWithLinkPreview,
13-
} from './CustomBubbleWithLinkPreview';
14-
1510
interface CustomBubbleProps {
1611
bubbleMessage: Bubble<MessageProps>['props'];
1712
position: 'left' | 'right';
@@ -24,12 +19,10 @@ interface CustomBubbleProps {
2419
unReadSeenMessage?: string;
2520
messageStatusEnable: boolean;
2621
customMessageStatus?: (hasUnread: boolean) => JSX.Element;
27-
customLinkPreviewStyles?: ICustomBubbleWithLinkPreviewStyles;
28-
customLinkPreview: (
29-
urls: string[],
30-
bubbleMessage: BubbleProps<MessageProps>
31-
) => JSX.Element;
32-
enableLinkPreview: boolean;
22+
customLinkPreview?: (
23+
bubbleMessage: BubbleProps<MessageProps>,
24+
viewMessageStatus: ReactNode
25+
) => React.ReactNode;
3326
}
3427

3528
export const CustomBubble: React.FC<CustomBubbleProps> = ({
@@ -44,9 +37,7 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
4437
unReadSentMessage,
4538
messageStatusEnable,
4639
customMessageStatus,
47-
customLinkPreviewStyles,
4840
customLinkPreview,
49-
enableLinkPreview,
5041
}) => {
5142
const styleBuble = {
5243
left: { backgroundColor: 'transparent' },
@@ -109,17 +100,11 @@ export const CustomBubble: React.FC<CustomBubbleProps> = ({
109100
);
110101

111102
default: {
112-
return (
113-
<View>
114-
<CustomBubbleWithLinkPreview
115-
bubbleMessage={bubbleMessage}
116-
customBubbleWithLinkPreviewStyles={customLinkPreviewStyles}
117-
customBubbleWithLinkPreview={customLinkPreview}
118-
enableLinkPreview={enableLinkPreview}
119-
/>
120-
{ViewMessageStatus}
121-
</View>
122-
);
103+
if (customLinkPreview) {
104+
return customLinkPreview(bubbleMessage, ViewMessageStatus);
105+
}
106+
107+
return <Bubble {...bubbleMessage} />;
123108
}
124109
}
125110
};
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './CustomBubble';
22
export * from './CustomImageVideoBubble';
3-
export * from './CustomBubbleWithLinkPreview';

0 commit comments

Comments
 (0)