mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {StyleProp, StyleSheet, View, ViewStyle, Pressable} from 'react-native'
3import {RepostIcon} from 'lib/icons'
4import {colors} from 'lib/styles'
5import {useTheme} from 'lib/ThemeContext'
6import {Text} from '../text/Text'
7
8import {
9 NativeDropdown,
10 DropdownItem as NativeDropdownItem,
11} from '../forms/NativeDropdown'
12import {EventStopper} from '../EventStopper'
13import {useLingui} from '@lingui/react'
14import {msg} from '@lingui/macro'
15import {useRequireAuth} from '#/state/session'
16import {useSession} from '#/state/session'
17
18interface Props {
19 isReposted: boolean
20 repostCount?: number
21 big?: boolean
22 onRepost: () => void
23 onQuote: () => void
24 style?: StyleProp<ViewStyle>
25}
26
27export const RepostButton = ({
28 isReposted,
29 repostCount,
30 big,
31 onRepost,
32 onQuote,
33}: Props) => {
34 const theme = useTheme()
35 const {_} = useLingui()
36 const {hasSession} = useSession()
37 const requireAuth = useRequireAuth()
38
39 const defaultControlColor = React.useMemo(
40 () => ({
41 color: theme.palette.default.postCtrl,
42 }),
43 [theme],
44 )
45
46 const dropdownItems: NativeDropdownItem[] = [
47 {
48 label: isReposted ? _(msg`Undo repost`) : _(msg`Repost`),
49 testID: 'repostDropdownRepostBtn',
50 icon: {
51 ios: {name: 'repeat'},
52 android: '',
53 web: 'retweet',
54 },
55 onPress: onRepost,
56 },
57 {
58 label: _(msg`Quote post`),
59 testID: 'repostDropdownQuoteBtn',
60 icon: {
61 ios: {name: 'quote.bubble'},
62 android: '',
63 web: 'quote-left',
64 },
65 onPress: onQuote,
66 },
67 ]
68
69 const inner = (
70 <View
71 style={[
72 styles.control,
73 !big && styles.controlPad,
74 (isReposted
75 ? styles.reposted
76 : defaultControlColor) as StyleProp<ViewStyle>,
77 ]}>
78 <RepostIcon strokeWidth={2.2} size={big ? 24 : 20} />
79 {typeof repostCount !== 'undefined' ? (
80 <Text
81 testID="repostCount"
82 type={isReposted ? 'md-bold' : 'md'}
83 style={styles.repostCount}>
84 {repostCount ?? 0}
85 </Text>
86 ) : undefined}
87 </View>
88 )
89
90 return hasSession ? (
91 <EventStopper>
92 <NativeDropdown
93 items={dropdownItems}
94 accessibilityLabel={_(msg`Repost or quote post`)}
95 accessibilityHint="">
96 {inner}
97 </NativeDropdown>
98 </EventStopper>
99 ) : (
100 <Pressable
101 accessibilityRole="button"
102 onPress={() => {
103 requireAuth(() => {})
104 }}
105 accessibilityLabel={_(msg`Repost or quote post`)}
106 accessibilityHint="">
107 {inner}
108 </Pressable>
109 )
110}
111
112const styles = StyleSheet.create({
113 control: {
114 display: 'flex',
115 flexDirection: 'row',
116 alignItems: 'center',
117 gap: 4,
118 },
119 controlPad: {
120 padding: 5,
121 },
122 reposted: {
123 color: colors.green3,
124 },
125 repostCount: {
126 color: 'currentColor',
127 },
128})