forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2import {View} from 'react-native'
3import {type $Typed, ComAtprotoLabelDefs} from '@atproto/api'
4import {msg, Trans} from '@lingui/macro'
5import {useLingui} from '@lingui/react'
6
7import {
8 useProfileQuery,
9 useProfileUpdateMutation,
10} from '#/state/queries/profile'
11import {useSession} from '#/state/session'
12import {atoms as a, useTheme} from '#/alf'
13import * as Toggle from '#/components/forms/Toggle'
14import {Text} from '#/components/Typography'
15import * as bsky from '#/types/bsky'
16
17export function PwiOptOut() {
18 const t = useTheme()
19 const {_} = useLingui()
20 const {currentAccount} = useSession()
21 const {data: profile} = useProfileQuery({did: currentAccount?.did})
22 const updateProfile = useProfileUpdateMutation()
23
24 const isOptedOut =
25 profile?.labels?.some(l => l.val === '!no-unauthenticated') || false
26 const canToggle = profile && !updateProfile.isPending
27
28 const onToggleOptOut = React.useCallback(() => {
29 if (!profile) {
30 return
31 }
32 let wasAdded = false
33 updateProfile.mutate({
34 profile,
35 updates: existing => {
36 // create labels attr if needed
37 const labels: $Typed<ComAtprotoLabelDefs.SelfLabels> = bsky.validate(
38 existing.labels,
39 ComAtprotoLabelDefs.validateSelfLabels,
40 )
41 ? existing.labels
42 : {
43 $type: 'com.atproto.label.defs#selfLabels',
44 values: [],
45 }
46
47 // toggle the label
48 const hasLabel = labels.values.some(
49 l => l.val === '!no-unauthenticated',
50 )
51 if (hasLabel) {
52 wasAdded = false
53 labels.values = labels.values.filter(
54 l => l.val !== '!no-unauthenticated',
55 )
56 } else {
57 wasAdded = true
58 labels.values.push({val: '!no-unauthenticated'})
59 }
60
61 // delete if no longer needed
62 if (labels.values.length === 0) {
63 delete existing.labels
64 } else {
65 existing.labels = labels
66 }
67
68 return existing
69 },
70 checkCommitted: res => {
71 const exists = !!res.data.labels?.some(
72 l => l.val === '!no-unauthenticated',
73 )
74 return exists === wasAdded
75 },
76 })
77 }, [updateProfile, profile])
78
79 return (
80 <View style={[a.flex_1, a.gap_sm]}>
81 <Toggle.Item
82 name="logged_out_visibility"
83 disabled={!canToggle || updateProfile.isPending}
84 value={isOptedOut}
85 onChange={onToggleOptOut}
86 label={_(
87 msg`Discourage apps from showing my account to logged-out users`,
88 )}
89 style={[a.w_full]}>
90 <Toggle.LabelText style={[a.flex_1]}>
91 <Trans>
92 Discourage apps from showing my account to logged-out users
93 </Trans>
94 </Toggle.LabelText>
95 <Toggle.Platform />
96 </Toggle.Item>
97
98 <Text style={[a.leading_snug, t.atoms.text_contrast_high]}>
99 <Trans>
100 Bluesky will not show your profile and posts to logged-out users.
101 Other apps may not honor this request. This does not make your account
102 private.
103 </Trans>
104 </Text>
105 </View>
106 )
107}