Fix types during label creation in PwiOptOut (#7346)

This commit is contained in:
Eric Bailey
2025-01-07 17:13:11 -06:00
parent e0344434c9
commit 94d82535aa
+15 -10
View File
@@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {ComAtprotoLabelDefs} from '@atproto/api' import {ComAtprotoLabelDefs} from '@atproto/api'
import {$Typed} from '@atproto/api/dist/client/util'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@@ -33,31 +34,35 @@ export function PwiOptOut() {
profile, profile,
updates: existing => { updates: existing => {
// create labels attr if needed // create labels attr if needed
existing.labels = ComAtprotoLabelDefs.isSelfLabels(existing.labels) const labels: $Typed<ComAtprotoLabelDefs.SelfLabels> =
? existing.labels ComAtprotoLabelDefs.isValidSelfLabels(existing.labels)
: { ? existing.labels
$type: 'com.atproto.label.defs#selfLabels', : {
values: [], $type: 'com.atproto.label.defs#selfLabels',
} values: [],
}
// toggle the label // toggle the label
const hasLabel = existing.labels.values.some( const hasLabel = labels.values.some(
l => l.val === '!no-unauthenticated', l => l.val === '!no-unauthenticated',
) )
if (hasLabel) { if (hasLabel) {
wasAdded = false wasAdded = false
existing.labels.values = existing.labels.values.filter( labels.values = labels.values.filter(
l => l.val !== '!no-unauthenticated', l => l.val !== '!no-unauthenticated',
) )
} else { } else {
wasAdded = true wasAdded = true
existing.labels.values.push({val: '!no-unauthenticated'}) labels.values.push({val: '!no-unauthenticated'})
} }
// delete if no longer needed // delete if no longer needed
if (existing.labels.values.length === 0) { if (labels.values.length === 0) {
delete existing.labels delete existing.labels
} else {
existing.labels = labels
} }
return existing return existing
}, },
checkCommitted: res => { checkCommitted: res => {