Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {useMemo} from 'react'
2
3import {useMaybeDeerVerificationProfileOverlay} from '#/state/queries/deer-verification'
4import {usePreferencesQuery} from '#/state/queries/preferences'
5import {useCurrentAccountProfile} from '#/state/queries/useCurrentAccountProfile'
6import {useSession} from '#/state/session'
7import type * as bsky from '#/types/bsky'
8
9export type FullVerificationState = {
10 profile: {
11 role: 'default' | 'verifier'
12 isVerified: boolean
13 wasVerified: boolean
14 isViewer: boolean
15 showBadge: boolean
16 }
17 viewer:
18 | {
19 role: 'default'
20 isVerified: boolean
21 }
22 | {
23 role: 'verifier'
24 isVerified: boolean
25 hasIssuedVerification: boolean
26 }
27}
28
29export function useFullVerificationState({
30 profile,
31}: {
32 profile: bsky.profile.AnyProfileView
33}): FullVerificationState {
34 const {currentAccount} = useSession()
35 const currentAccountProfile = useCurrentAccountProfile()
36 const profileState = useSimpleVerificationState({profile})
37 const viewerState = useSimpleVerificationState({
38 profile: currentAccountProfile,
39 })
40
41 return useMemo(() => {
42 const verifications = profile.verification?.verifications || []
43 const wasVerified =
44 profileState.role === 'default' &&
45 !profileState.isVerified &&
46 verifications.length > 0
47 const hasIssuedVerification = Boolean(
48 viewerState &&
49 viewerState.role === 'verifier' &&
50 // profileState.role === 'default' &&
51 verifications.find(v => v.issuer === currentAccount?.did),
52 )
53
54 return {
55 profile: {
56 ...profileState,
57 wasVerified,
58 isViewer: profile.did === currentAccount?.did,
59 showBadge: profileState.showBadge,
60 },
61 viewer:
62 viewerState.role === 'verifier'
63 ? {
64 role: 'verifier',
65 isVerified: viewerState.isVerified,
66 hasIssuedVerification,
67 }
68 : {
69 role: 'default',
70 isVerified: viewerState.isVerified,
71 },
72 }
73 }, [profile, currentAccount, profileState, viewerState])
74}
75
76export type SimpleVerificationState = {
77 role: 'default' | 'verifier'
78 isVerified: boolean
79 showBadge: boolean
80}
81
82export function useSimpleVerificationState({
83 profile: baseProfile,
84}: {
85 profile?: bsky.profile.AnyProfileView
86}): SimpleVerificationState {
87 const preferences = usePreferencesQuery()
88 const prefs = useMemo(
89 () => preferences.data?.verificationPrefs || {hideBadges: false},
90 [preferences.data?.verificationPrefs],
91 )
92 const profile = useMaybeDeerVerificationProfileOverlay(baseProfile)
93
94 return useMemo(() => {
95 if (!profile || !profile.verification) {
96 return {
97 role: 'default',
98 isVerified: false,
99 showBadge: false,
100 }
101 }
102
103 const {verifiedStatus, trustedVerifierStatus} = profile.verification
104 const isVerifiedUser = ['valid', 'invalid'].includes(verifiedStatus)
105 const isVerifierUser = ['valid', 'invalid'].includes(trustedVerifierStatus)
106 const isVerified =
107 (isVerifiedUser && verifiedStatus === 'valid') ||
108 (isVerifierUser && trustedVerifierStatus === 'valid')
109
110 return {
111 role: isVerifierUser ? 'verifier' : 'default',
112 isVerified,
113 showBadge: prefs.hideBadges ? false : isVerified,
114 }
115 }, [profile, prefs])
116}