mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {View} from 'react-native'
2import {msg} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4
5import {logger} from '#/logger'
6import {type Shadow} from '#/state/cache/types'
7import {atoms as a, useBreakpoints, useTheme} from '#/alf'
8import {Button} from '#/components/Button'
9import {useDialogControl} from '#/components/Dialog'
10import {useFullVerificationState} from '#/components/verification'
11import {type FullVerificationState} from '#/components/verification'
12import {VerificationCheck} from '#/components/verification/VerificationCheck'
13import {VerificationsDialog} from '#/components/verification/VerificationsDialog'
14import {VerifierDialog} from '#/components/verification/VerifierDialog'
15import type * as bsky from '#/types/bsky'
16
17export function shouldShowVerificationCheckButton(
18 state: FullVerificationState,
19) {
20 let ok = false
21
22 if (state.profile.role === 'default') {
23 if (state.profile.isVerified) {
24 ok = true
25 } else if (state.profile.isViewer && state.profile.wasVerified) {
26 ok = true
27 } else if (
28 state.viewer.role === 'verifier' &&
29 state.viewer.hasIssuedVerification
30 ) {
31 ok = true
32 }
33 } else if (state.profile.role === 'verifier') {
34 if (state.profile.isViewer) {
35 ok = true
36 } else if (state.profile.isVerified) {
37 ok = true
38 }
39 }
40
41 if (
42 !state.profile.showBadge &&
43 !state.profile.isViewer &&
44 !(state.viewer.role === 'verifier' && state.viewer.hasIssuedVerification)
45 ) {
46 ok = false
47 }
48
49 return ok
50}
51
52export function VerificationCheckButton({
53 profile,
54 size,
55}: {
56 profile: Shadow<bsky.profile.AnyProfileView>
57 size: 'lg' | 'md' | 'sm'
58}) {
59 const state = useFullVerificationState({
60 profile,
61 })
62
63 if (shouldShowVerificationCheckButton(state)) {
64 return <Badge profile={profile} verificationState={state} size={size} />
65 }
66
67 return null
68}
69
70export function Badge({
71 profile,
72 verificationState: state,
73 size,
74}: {
75 profile: Shadow<bsky.profile.AnyProfileView>
76 verificationState: FullVerificationState
77 size: 'lg' | 'md' | 'sm'
78}) {
79 const t = useTheme()
80 const {_} = useLingui()
81 const verificationsDialogControl = useDialogControl()
82 const verifierDialogControl = useDialogControl()
83 const {gtPhone} = useBreakpoints()
84 let dimensions = 12
85 if (size === 'lg') {
86 dimensions = gtPhone ? 20 : 18
87 } else if (size === 'md') {
88 dimensions = 14
89 }
90
91 const verifiedByHidden = !state.profile.showBadge && state.profile.isViewer
92
93 return (
94 <>
95 <Button
96 label={
97 state.profile.isViewer
98 ? _(msg`View your verifications`)
99 : _(msg`View this user's verifications`)
100 }
101 hitSlop={20}
102 onPress={evt => {
103 evt.preventDefault()
104 logger.metric('verification:badge:click', {}, {statsig: true})
105 if (state.profile.role === 'verifier') {
106 verifierDialogControl.open()
107 } else {
108 verificationsDialogControl.open()
109 }
110 }}>
111 {({hovered}) => (
112 <View
113 style={[
114 a.justify_end,
115 a.align_end,
116 a.transition_transform,
117 {
118 width: dimensions,
119 height: dimensions,
120 transform: [
121 {
122 scale: hovered ? 1.1 : 1,
123 },
124 ],
125 },
126 ]}>
127 <VerificationCheck
128 width={dimensions}
129 fill={
130 verifiedByHidden
131 ? t.atoms.bg_contrast_100.backgroundColor
132 : state.profile.isVerified
133 ? t.palette.primary_500
134 : t.atoms.bg_contrast_100.backgroundColor
135 }
136 verifier={state.profile.role === 'verifier'}
137 />
138 </View>
139 )}
140 </Button>
141
142 <VerificationsDialog
143 control={verificationsDialogControl}
144 profile={profile}
145 verificationState={state}
146 />
147
148 <VerifierDialog
149 control={verifierDialogControl}
150 profile={profile}
151 verificationState={state}
152 />
153 </>
154 )
155}