mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at verify-code 5.3 kB view raw
1import React, {memo, useCallback} from 'react' 2import {View} from 'react-native' 3import {msg, plural} from '@lingui/macro' 4import {useLingui} from '@lingui/react' 5 6import {POST_CTRL_HITSLOP} from '#/lib/constants' 7import {useHaptics} from '#/lib/haptics' 8import {useRequireAuth} from '#/state/session' 9import {atoms as a, useTheme} from '#/alf' 10import {Button, ButtonText} from '#/components/Button' 11import * as Dialog from '#/components/Dialog' 12import {CloseQuote_Stroke2_Corner1_Rounded as Quote} from '#/components/icons/Quote' 13import {Repost_Stroke2_Corner2_Rounded as Repost} from '#/components/icons/Repost' 14import {Text} from '#/components/Typography' 15import {formatCount} from '../numeric/format' 16 17interface Props { 18 isReposted: boolean 19 repostCount?: number 20 onRepost: () => void 21 onQuote: () => void 22 big?: boolean 23 embeddingDisabled: boolean 24} 25 26let RepostButton = ({ 27 isReposted, 28 repostCount, 29 onRepost, 30 onQuote, 31 big, 32 embeddingDisabled, 33}: Props): React.ReactNode => { 34 const t = useTheme() 35 const {_, i18n} = useLingui() 36 const requireAuth = useRequireAuth() 37 const dialogControl = Dialog.useDialogControl() 38 const playHaptic = useHaptics() 39 40 const color = React.useMemo( 41 () => ({ 42 color: isReposted ? t.palette.positive_600 : t.palette.contrast_500, 43 }), 44 [t, isReposted], 45 ) 46 47 const close = useCallback(() => dialogControl.close(), [dialogControl]) 48 49 return ( 50 <> 51 <Button 52 testID="repostBtn" 53 onPress={() => { 54 requireAuth(() => dialogControl.open()) 55 }} 56 onLongPress={() => { 57 requireAuth(() => onQuote()) 58 }} 59 style={[ 60 a.flex_row, 61 a.align_center, 62 a.gap_xs, 63 a.bg_transparent, 64 {padding: 5}, 65 ]} 66 hoverStyle={t.atoms.bg_contrast_25} 67 label={`${ 68 isReposted 69 ? _(msg`Undo repost`) 70 : _(msg({message: 'Repost', context: 'action'})) 71 } (${plural(repostCount || 0, {one: '# repost', other: '# reposts'})})`} 72 shape="round" 73 variant="ghost" 74 color="secondary" 75 hitSlop={POST_CTRL_HITSLOP}> 76 <Repost style={color} width={big ? 22 : 18} /> 77 {typeof repostCount !== 'undefined' && repostCount > 0 ? ( 78 <Text 79 testID="repostCount" 80 style={[ 81 color, 82 big ? a.text_md : {fontSize: 15}, 83 isReposted && a.font_bold, 84 ]}> 85 {formatCount(i18n, repostCount)} 86 </Text> 87 ) : undefined} 88 </Button> 89 <Dialog.Outer control={dialogControl}> 90 <Dialog.Handle /> 91 <Dialog.Inner label={_(msg`Repost or quote post`)}> 92 <View style={a.gap_xl}> 93 <View style={a.gap_xs}> 94 <Button 95 style={[a.justify_start, a.px_md]} 96 label={ 97 isReposted 98 ? _(msg`Remove repost`) 99 : _(msg({message: `Repost`, context: 'action'})) 100 } 101 onPress={() => { 102 if (!isReposted) playHaptic() 103 104 dialogControl.close(() => { 105 onRepost() 106 }) 107 }} 108 size="large" 109 variant="ghost" 110 color="primary"> 111 <Repost size="lg" fill={t.palette.primary_500} /> 112 <Text style={[a.font_bold, a.text_xl]}> 113 {isReposted 114 ? _(msg`Remove repost`) 115 : _(msg({message: `Repost`, context: 'action'}))} 116 </Text> 117 </Button> 118 <Button 119 disabled={embeddingDisabled} 120 testID="quoteBtn" 121 style={[a.justify_start, a.px_md]} 122 label={ 123 embeddingDisabled 124 ? _(msg`Quote posts disabled`) 125 : _(msg`Quote post`) 126 } 127 onPress={() => { 128 playHaptic() 129 dialogControl.close(() => { 130 onQuote() 131 }) 132 }} 133 size="large" 134 variant="ghost" 135 color="primary"> 136 <Quote 137 size="lg" 138 fill={ 139 embeddingDisabled 140 ? t.atoms.text_contrast_low.color 141 : t.palette.primary_500 142 } 143 /> 144 <Text 145 style={[ 146 a.font_bold, 147 a.text_xl, 148 embeddingDisabled && t.atoms.text_contrast_low, 149 ]}> 150 {embeddingDisabled 151 ? _(msg`Quote posts disabled`) 152 : _(msg`Quote post`)} 153 </Text> 154 </Button> 155 </View> 156 <Button 157 label={_(msg`Cancel quote post`)} 158 onAccessibilityEscape={close} 159 onPress={close} 160 size="medium" 161 variant="solid" 162 color="primary"> 163 <ButtonText>{_(msg`Cancel`)}</ButtonText> 164 </Button> 165 </View> 166 </Dialog.Inner> 167 </Dialog.Outer> 168 </> 169 ) 170} 171RepostButton = memo(RepostButton) 172export {RepostButton}