-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathUserGeneratedText.tsx
More file actions
395 lines (364 loc) · 11.2 KB
/
Copy pathUserGeneratedText.tsx
File metadata and controls
395 lines (364 loc) · 11.2 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import type { ReactNode } from 'react'
import { useMemo, useCallback, useEffect, useRef, useState } from 'react'
import { useUserByHandle } from '@audius/common/api'
import {
formatCollectionName,
formatTrackName,
formatUserName,
handleRegex,
isAudiusUrl,
restrictedHandles,
squashNewLines
} from '@audius/common/utils'
import type { CommentMention } from '@audius/sdk'
import { HashId, ResolveApi } from '@audius/sdk'
import { css } from '@emotion/native'
import type { NavigationProp, ParamListBase } from '@react-navigation/native'
import type { Match } from 'autolinker/dist/es2015'
import { View } from 'react-native'
import type {
GestureResponderEvent,
LayoutRectangle,
Text as TextRef
} from 'react-native'
import type { AutolinkProps } from 'react-native-autolink'
import Autolink from 'react-native-autolink'
import { useAsync } from 'react-use'
import { Text } from '@audius/harmony-native'
import type { TextLinkProps, TextProps } from '@audius/harmony-native'
import { TextLink } from 'app/harmony-native/components/TextLink/TextLink'
import { useNavigation } from 'app/hooks/useNavigation'
import { audiusSdk } from 'app/services/sdk/audius-sdk'
type NavigationTarget<ParamList extends ReactNavigation.RootParamList> = {
screen: keyof ParamList
params?: ParamList[keyof ParamList]
}
const {
instanceOfTrackResponse,
instanceOfUserResponse,
instanceOfPlaylistResponse
} = ResolveApi
type PositionedLink = {
text: string
match: Match
}
export type UserGeneratedTextProps = Omit<TextProps, 'children'> &
Pick<AutolinkProps, 'matchers'> & {
children: string | null | undefined
source?: 'profile page' | 'track page' | 'collection page'
// Pass touches through text elements
allowPointerEventsToPassThrough?: boolean
linkProps?: Partial<TextLinkProps>
mentions?: CommentMention[]
// If true, only linkify Audius URLs
internalLinksOnly?: boolean
// Suffix to append after the text. Used for "edited" text in comments
suffix?: ReactNode
navigation?: NavigationProp<ParamListBase>
}
const Link = ({
children,
url,
navigation: navigationProp,
...other
}: TextLinkProps & {
url: string
navigation?: NavigationProp<ParamListBase>
}) => {
const [unfurledContent, setUnfurledContent] = useState<string>()
const [to, setTo] = useState<NavigationTarget<any> | undefined>(undefined)
const shouldUnfurl = isAudiusUrl(url)
const currentNavigation = useNavigation()
const navigation = navigationProp ?? currentNavigation
useAsync(async () => {
if (shouldUnfurl && !unfurledContent) {
const sdk = await audiusSdk()
const res = await sdk.resolve({ url })
if (res.data) {
if (instanceOfTrackResponse(res)) {
setUnfurledContent(formatTrackName({ track: res.data }))
setTo({
screen: 'Track',
params: { trackId: HashId.parse(res.data.id) }
})
} else if (instanceOfPlaylistResponse(res)) {
setUnfurledContent(formatCollectionName({ collection: res.data[0] }))
setTo({
screen: 'Collection',
params: { id: HashId.parse(res.data[0].id) }
})
} else if (instanceOfUserResponse(res)) {
const user = Array.isArray(res.data) ? res.data[0] : res.data
if (user) {
setUnfurledContent(formatUserName({ user }))
setTo({
screen: 'Profile',
params: { id: HashId.parse(user.id) }
})
}
}
}
}
}, [url, shouldUnfurl, unfurledContent, setUnfurledContent])
const handlePress = useCallback(
(e: GestureResponderEvent) => {
if (to) {
if ('push' in navigation) {
// @ts-ignore
navigation.push(to.screen, to.params)
// @ts-ignore
other.onPress?.(e, to.screen.toLowerCase(), to.params.id)
} else {
// @ts-ignore
navigation.navigate(to.screen, to.params)
other.onPress?.(e, 'other')
}
}
},
[to, other, navigation]
)
const linkProps = {
...other,
...(to ? { onPress: handlePress } : { url })
}
return (
<TextLink {...linkProps}>{unfurledContent ?? children ?? url}</TextLink>
)
}
const HandleLink = ({
handle,
onPress,
...other
}: Omit<TextLinkProps, 'to'> & { handle: string }) => {
const { data: userId } = useUserByHandle(handle.replace('@', ''), {
select: (user) => user.user_id
})
const handlePress = useCallback(
(e: GestureResponderEvent) => {
onPress?.(e, 'mention', userId)
},
[onPress, userId]
)
return userId ? (
<TextLink
{...other}
onPress={handlePress}
to={{ screen: 'Profile', params: { id: userId } }}
>
{handle}
</TextLink>
) : (
<Text {...other} variant={other.textVariant}>
{handle}
</Text>
)
}
export const UserGeneratedText = (props: UserGeneratedTextProps) => {
const {
allowPointerEventsToPassThrough,
source,
style,
children,
linkProps,
mentions,
suffix,
matchers,
internalLinksOnly,
navigation,
onTextLayout,
numberOfLines,
...other
} = props
const linkContainerRef = useRef<View>(null)
const [linkRefs, setLinkRefs] = useState<Record<number, TextRef>>({})
const [links, setLinks] = useState<Record<number, PositionedLink>>({})
const [linkLayouts, setLinkLayouts] = useState<
Record<number, LayoutRectangle>
>({})
const [linkContainerLayout, setLinkContainerLayout] =
useState<LayoutRectangle>()
const mentionRegex = useMemo(() => {
const nullRegex = /(?!)/
if (!mentions) return nullRegex
const regexString = [...mentions.map((mention) => `@${mention.handle}`)]
.sort((a, b) => b.length - a.length)
.join('|')
return regexString.length ? new RegExp(regexString, 'g') : nullRegex
}, [mentions])
useEffect(() => {
if (allowPointerEventsToPassThrough) {
let layouts = {}
const linkKeys = Object.keys(links)
// Measure the layout of each link
linkKeys.forEach((key) => {
const linkRef = linkRefs[key]
if (linkRef) {
// Need to use `measureInWindow` instead of `onLayout` or `measure` because
// android doesn't return the correct layout for nested text elements
linkRef.measureInWindow((x, y, width, height) => {
layouts = { ...layouts, [key]: { x, y, width, height } }
// If all the links have been measured, update state
if (linkKeys.length === Object.keys(layouts).length) {
setLinkLayouts(layouts)
}
})
}
})
if (linkContainerRef.current) {
linkContainerRef.current.measureInWindow((x, y, width, height) =>
setLinkContainerLayout({ x, y, width, height })
)
}
}
}, [allowPointerEventsToPassThrough, links, linkRefs, linkContainerRef])
// We let Autolink lay out each link invisibly, and capture their position and data
const renderHiddenLink = useCallback(
(text: string, match: Match, index: number) => (
<View
onLayout={() => {
setLinks((links) => ({
...links,
[index]: {
text,
match
}
}))
}}
ref={(el) => {
if (el) {
setLinkRefs((linkRefs) => {
if (linkRefs[index]) {
return linkRefs
}
return { ...linkRefs, [index]: el }
})
}
}}
// Negative margin needed to handle View overflow
style={css({ opacity: 0, marginTop: -3 })}
>
<Text {...other}>{text}</Text>
</View>
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
const renderLink = useCallback(
(text: string, match: Match) => {
const url = match.getAnchorHref()
const shouldLinkify = !internalLinksOnly || isAudiusUrl(url)
return shouldLinkify ? (
<Link
{...other}
variant='visible'
textVariant={other.variant}
url={url}
navigation={navigation}
{...linkProps}
/>
) : (
renderText(text)
)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
const renderHandleLink = useCallback((text: string) => {
const isHandleUnrestricted = !restrictedHandles.has(text.toLowerCase())
return isHandleUnrestricted ? (
<HandleLink
{...other}
variant='visible'
textVariant={other.variant}
handle={text}
{...linkProps}
/>
) : (
renderText(text)
)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const renderText = useCallback(
(text: string) => {
return (
<Text suppressHighlighting {...other}>
{text}
</Text>
)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
return (
<View>
<View
pointerEvents={allowPointerEventsToPassThrough ? 'none' : undefined}
ref={linkContainerRef}
>
<Text onTextLayout={onTextLayout} numberOfLines={numberOfLines}>
<Autolink
renderLink={
allowPointerEventsToPassThrough ? renderHiddenLink : renderLink
}
renderText={renderText}
email
url={false}
style={[{ marginBottom: 3 }, style]}
text={squashNewLines(children, 10) as string}
matchers={[
// Handle matcher e.g. @handle
...(mentions
? [
{
pattern: mentionRegex,
renderLink: renderHandleLink
}
]
: [
{
pattern: handleRegex,
renderLink: renderHandleLink
}
]),
// URL match
// Intentionally not using the default URL matcher to avoid conflict with the handle matcher. See: https://github.com/joshswan/react-native-autolink/issues/78
{
pattern: /(https?:\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\S]*)/g
},
// custom matchers provided via props
...(matchers ?? [])
]}
/>
{suffix}
</Text>
</View>
{/* We overlay copies of each link on top of the invisible links */}
<View style={{ position: 'absolute' }}>
{Object.entries(links).map(([index, { text, match }]) => {
const linkLayout = linkLayouts[index]
return linkLayout && linkContainerLayout ? (
<View
style={{
position: 'absolute',
top: linkLayout.y - linkContainerLayout.y,
left: linkLayout.x - linkContainerLayout.x
}}
>
<Link
{...other}
variant='visible'
textVariant={other.variant}
key={`${linkLayout.x} ${linkLayout.y} ${index}`}
url={match.getAnchorHref()}
source={source}
{...linkProps}
>
{text}
</Link>
</View>
) : null
})}
</View>
</View>
)
}