import {useMemo} from 'react'
import {View} from 'react-native'
import {Trans, useLingui} from '@lingui/react/macro'
import {
type NotificationSettingsPreference,
type NotificationSettingsPreferenceName,
useNotificationSettingsUpdateMutation,
} from '#/state/queries/notifications/settings'
import {atoms as a, platform, useTheme} from '#/alf'
import * as Toggle from '#/components/forms/Toggle'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
import {useAnalytics} from '#/analytics'
import {Divider} from '../../components/SettingsList'
export function PreferenceControls({
name,
syncOthers,
preference,
allowDisableInApp = true,
}: {
name: NotificationSettingsPreferenceName
/**
* Keep other prefs in sync with `name`. For use in the "everything else" category
* which groups starterpack joins + verified + unverified notifications into a single toggle.
*/
syncOthers?: NotificationSettingsPreferenceName[]
preference?: NotificationSettingsPreference
allowDisableInApp?: boolean
}) {
if (!preference)
return (
)
return (
)
}
export function Inner({
name,
syncOthers = [],
preference,
allowDisableInApp,
}: {
name: NotificationSettingsPreferenceName
syncOthers?: NotificationSettingsPreferenceName[]
preference: NotificationSettingsPreference
allowDisableInApp: boolean
}) {
const t = useTheme()
const {t: l} = useLingui()
const ax = useAnalytics()
const {mutate} = useNotificationSettingsUpdateMutation()
const channels = useMemo(() => {
const arr = []
if ('list' in preference && preference.list) arr.push('list')
if (preference.push) arr.push('push')
return arr
}, [preference])
const onChangeChannels = (change: string[]) => {
const newPreference = {
...preference,
...('list' in preference ? {list: change.includes('list')} : {}),
push: change.includes('push'),
} as typeof preference
const metrics: {name: string; push: boolean; list?: boolean} = {
name,
push: newPreference.push,
}
if ('list' in newPreference) {
metrics.list = newPreference.list
}
ax.metric('activityPreference:changeChannels', metrics)
mutate({
[name]: newPreference,
...Object.fromEntries(syncOthers.map(key => [key, newPreference])),
})
}
const onChangeFilter = ([change]: string[]) => {
if (change !== 'all' && change !== 'follows')
throw new Error('Invalid filter')
const newPreference = {
...preference,
include: change,
} satisfies typeof preference
ax.metric('activityPreference:changeFilter', {name, value: change})
mutate({
[name]: newPreference,
...Object.fromEntries(syncOthers.map(key => [key, newPreference])),
})
}
return (
Push notifications
{allowDisableInApp && 'list' in preference && (
In-app notifications
)}
{'include' in preference && (
<>
From
{({selected}) => (
)}
{({selected}) => (
)}
>
)}
)
}