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