-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathLinksExample.tsx
More file actions
232 lines (211 loc) · 6.73 KB
/
LinksExample.tsx
File metadata and controls
232 lines (211 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import React, { useCallback, useMemo, useState } from 'react'
import { Linking, StyleSheet, Text, View } from 'react-native'
import { ActionSheetProvider, useActionSheet } from '@expo/react-native-action-sheet'
import { setStringAsync } from 'expo-clipboard'
import { isValidPhoneNumber, parsePhoneNumberWithError } from 'libphonenumber-js'
import { GiftedChat, IMessage, LinkMatcher } from 'react-native-gifted-chat'
import { useKeyboardVerticalOffset } from '../../hooks/useKeyboardVerticalOffset'
const LinksExample: React.FC = () => {
const { showActionSheetWithOptions } = useActionSheet()
const keyboardVerticalOffset = useKeyboardVerticalOffset()
const initialMessages: IMessage[] = useMemo(() => [
{
text: 'Welcome! 👋',
createdAt: new Date(),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'This example shows how GiftedChat handles different types of links in messages. Try tapping on any link!',
createdAt: new Date(Date.now() - 1 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'Phone numbers are also parsed:\n\n• +79931234567\n\n• 89931234567\n\n• +1-215-456-7890.\n\nIt shouldn\'t parse phone numbers in file names like IMG_20220101_123456.jpg, 89201234567_today.pdf, or in addresses like 1234 React St., JS City, 56789.',
createdAt: new Date(Date.now() - 2 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'Email addresses are clickable: cool.guy@example.com or contact@reactnative.dev',
createdAt: new Date(Date.now() - 3 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'Different link formats work:\n• www.google.com\n• google.com\n• https://google.com',
createdAt: new Date(Date.now() - 4 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'Use hashtags to categorize: #giftedchat #reactnative #opensource',
createdAt: new Date(Date.now() - 5 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'You can mention people like @kesha-antonov or @john-doe',
createdAt: new Date(Date.now() - 6 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
},
{
text: 'System message with link: Check out our documentation at https://github.com/FaridSafi/react-native-gifted-chat',
createdAt: new Date(Date.now() - 7 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
system: true,
},
{
text: 'System message with data and phone numbers: Contact support at +79931234567 or +1-215-456-7890. We have holidays on 2025-12-25 and until 2026-01-01 12:00.',
createdAt: new Date(Date.now() - 7 * 60000),
user: {
_id: 2,
name: 'John Doe',
},
system: true,
},
].map((message, index) => ({
...message,
_id: index + 1,
})).reverse(), [])
const [messages, setMessages] = useState<IMessage[]>(initialMessages)
const user = useMemo(() => ({
_id: 1,
name: 'Developer',
}), [])
const onSend = useCallback((newMessages: IMessage[] = []) => {
const messagesWithIds = newMessages.map(msg => ({
...msg,
_id: msg._id || Math.random().toString(36).substring(7),
user: msg.user || user,
}))
setMessages(previousMessages =>
GiftedChat.append(previousMessages, messagesWithIds)
)
}, [user])
const getValidPhoneNumber = useCallback((text: string): string | undefined => {
const cleaned = text.replace(/[\-\(\)\s\.]/g, '')
// Validate with libphonenumber-js
try {
// Try direct validation first
if (isValidPhoneNumber(cleaned))
return cleaned
// Try with RU region for local numbers
if (isValidPhoneNumber(cleaned, 'RU'))
return cleaned
// Try parsing to check validity
const phoneNumber = parsePhoneNumberWithError(cleaned, 'RU')
if (phoneNumber && phoneNumber.isValid())
return cleaned
} catch (error) {
console.warn('Invalid phone number:', error)
}
return undefined
}, [])
const handlePressPhoneNumber = useCallback((url: string) => {
if (!url)
return // Skip if validation failed
const options: {
title: string
action?: () => void
}[] = [
{ title: 'Call', action: () => Linking.openURL(`tel:${url}`) },
{ title: 'Send SMS', action: () => Linking.openURL(`sms:${url}`) },
{ title: 'Copy Phone Number', action: () => setStringAsync(url) },
{ title: 'Cancel' },
]
showActionSheetWithOptions({
options: options.map(o => o.title),
cancelButtonIndex: options.length - 1,
}, (buttonIndex?: number) => {
if (buttonIndex === undefined)
return
const option = options[buttonIndex]
option.action?.()
})
}, [showActionSheetWithOptions])
const matchers = useMemo<LinkMatcher[]>(() => [
{
type: 'phone',
// Pattern that excludes numbers adjacent to underscores or part of filenames
pattern: /(?<![A-Za-z0-9_])(?:\+?\d{1,3}[\s.-]?)?\(?\d{1,4}\)?[\s.-]?\d{1,4}[\s.-]?\d{1,9}(?![A-Za-z0-9_]|\.[a-z]{2,4})/gi,
getLinkUrl: (matchedText: string): string => {
return getValidPhoneNumber(matchedText) || ''
},
getLinkText: (text: string): string => {
return text
},
renderLink: (text: string, url: string, index: number) => {
const validPhoneNumber = getValidPhoneNumber(text)
const isDisabled = !validPhoneNumber || !url
return (
<Text
key={index}
style={[styles.link, isDisabled && styles.linkDisabled]}
onPress={() => !isDisabled && handlePressPhoneNumber(url)}
>
{text}
</Text>
)
},
},
], [getValidPhoneNumber, handlePressPhoneNumber])
return (
<ActionSheetProvider>
<View style={styles.container}>
<GiftedChat
messages={messages}
onSend={onSend}
user={user}
messageTextProps={{
phone: false,
url: true,
matchers,
mention: true,
hashtag: true,
mentionUrl: 'https://x.com',
hashtagUrl: 'https://x.com/hashtag',
}}
keyboardAvoidingViewProps={{ keyboardVerticalOffset }}
colorScheme='light'
/>
</View>
</ActionSheetProvider>
)
}
const ExampleContainer = () => (
<ActionSheetProvider>
<LinksExample />
</ActionSheetProvider>
)
export default ExampleContainer
const styles = StyleSheet.create({
container: {
flex: 1,
},
link: {
textDecorationLine: 'underline',
},
linkDisabled: {
textDecorationLine: 'none',
},
})