mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {StyleProp, StyleSheet, TextStyle} from 'react-native'
2import {AppBskyActorGetProfile as GetProfile} from '@atproto/api'
3
4import {makeProfileLink} from '#/lib/routes/links'
5import {sanitizeDisplayName} from '#/lib/strings/display-names'
6import {sanitizeHandle} from '#/lib/strings/handles'
7import {TypographyVariant} from '#/lib/ThemeContext'
8import {STALE} from '#/state/queries'
9import {useProfileQuery} from '#/state/queries/profile'
10import {TextLinkOnWebOnly} from './Link'
11import {LoadingPlaceholder} from './LoadingPlaceholder'
12import {Text} from './text/Text'
13
14export function UserInfoText({
15 type = 'md',
16 did,
17 attr,
18 failed,
19 prefix,
20 style,
21}: {
22 type?: TypographyVariant
23 did: string
24 attr?: keyof GetProfile.OutputSchema
25 loading?: string
26 failed?: string
27 prefix?: string
28 style?: StyleProp<TextStyle>
29}) {
30 attr = attr || 'handle'
31 failed = failed || 'user'
32
33 const {data: profile, isError} = useProfileQuery({
34 did,
35 staleTime: STALE.INFINITY,
36 })
37
38 let inner
39 if (isError) {
40 inner = (
41 <Text type={type} style={style} numberOfLines={1}>
42 {failed}
43 </Text>
44 )
45 } else if (profile) {
46 inner = (
47 <TextLinkOnWebOnly
48 type={type}
49 style={style}
50 lineHeight={1.2}
51 numberOfLines={1}
52 href={makeProfileLink(profile)}
53 text={
54 <Text emoji type={type} style={style} lineHeight={1.2}>
55 {`${prefix || ''}${sanitizeDisplayName(
56 typeof profile[attr] === 'string' && profile[attr]
57 ? (profile[attr] as string)
58 : sanitizeHandle(profile.handle),
59 )}`}
60 </Text>
61 }
62 />
63 )
64 } else {
65 inner = (
66 <LoadingPlaceholder
67 width={80}
68 height={8}
69 style={styles.loadingPlaceholder}
70 />
71 )
72 }
73
74 return inner
75}
76
77const styles = StyleSheet.create({
78 loadingPlaceholder: {position: 'relative', top: 1, left: 2},
79})