2022-07-06 11:20:20 -07:00
import React , { useEffect , useState } from 'react' ;
2022-06-13 08:35:54 -07:00
import { defineMessages , useIntl } from 'react-intl' ;
import {
followAccount ,
subscribeAccount ,
unsubscribeAccount ,
} from 'soapbox/actions/accounts' ;
import snackbar from 'soapbox/actions/snackbar' ;
import { IconButton } from 'soapbox/components/ui' ;
import { useAppDispatch , useFeatures } from 'soapbox/hooks' ;
import type { Account as AccountEntity } from 'soapbox/types/entities' ;
const messages = defineMessages ( {
subscribe : { id : 'account.subscribe' , defaultMessage : 'Subscribe to notifications from @{name}' } ,
unsubscribe : { id : 'account.unsubscribe' , defaultMessage : 'Unsubscribe to notifications from @{name}' } ,
subscribeSuccess : { id : 'account.subscribe.success' , defaultMessage : 'You have subscribed to this account.' } ,
2022-07-06 11:20:20 -07:00
subscribeSuccessNotice : { id : 'account.subscribe.successNotice' , defaultMessage : 'You have subscribed to this account, but your web notifications are disabled. Please enable them to receive notifications from @{name}.' } ,
2022-06-13 08:35:54 -07:00
unsubscribeSuccess : { id : 'account.unsubscribe.success' , defaultMessage : 'You have unsubscribed from this account.' } ,
subscribeFailure : { id : 'account.subscribe.failure' , defaultMessage : 'An error occurred trying to subscribed to this account.' } ,
unsubscribeFailure : { id : 'account.unsubscribe.failure' , defaultMessage : 'An error occurred trying to unsubscribed to this account.' } ,
} ) ;
interface ISubscriptionButton {
account : AccountEntity
}
const SubscriptionButton = ( { account } : ISubscriptionButton ) = > {
const dispatch = useAppDispatch ( ) ;
const features = useFeatures ( ) ;
const intl = useIntl ( ) ;
2022-07-06 11:20:20 -07:00
const [ hasWebNotificationsEnabled , setWebNotificationsEnabled ] = useState < boolean > ( true ) ;
const checkWebNotifications = ( ) = > {
Notification . requestPermission ( )
. then ( ( value ) = > setWebNotificationsEnabled ( value === 'granted' ) )
. catch ( ( ) = > null ) ;
} ;
2022-06-14 08:58:42 -07:00
const isFollowing = account . relationship ? . following ;
const isRequested = account . relationship ? . requested ;
2022-06-13 08:35:54 -07:00
const isSubscribed = features . accountNotifies ?
account . relationship ? . notifying :
account . relationship ? . subscribing ;
const title = isSubscribed ?
intl . formatMessage ( messages . unsubscribe , { name : account.get ( 'username' ) } ) :
intl . formatMessage ( messages . subscribe , { name : account.get ( 'username' ) } ) ;
2022-07-06 11:20:20 -07:00
const onSubscribeSuccess = ( ) = > {
if ( hasWebNotificationsEnabled ) {
dispatch ( snackbar . success ( intl . formatMessage ( messages . subscribeSuccess ) ) ) ;
} else {
dispatch ( snackbar . info ( intl . formatMessage ( messages . subscribeSuccessNotice , { name : account.get ( 'username' ) } ) ) ) ;
}
} ;
2022-06-13 08:35:54 -07:00
const onSubscribeFailure = ( ) = >
dispatch ( snackbar . error ( intl . formatMessage ( messages . subscribeFailure ) ) ) ;
const onUnsubscribeSuccess = ( ) = >
dispatch ( snackbar . success ( intl . formatMessage ( messages . unsubscribeSuccess ) ) ) ;
const onUnsubscribeFailure = ( ) = >
dispatch ( snackbar . error ( intl . formatMessage ( messages . unsubscribeFailure ) ) ) ;
const onNotifyToggle = ( ) = > {
if ( account . relationship ? . notifying ) {
dispatch ( followAccount ( account . get ( 'id' ) , { notify : false } as any ) )
? . then ( ( ) = > onUnsubscribeSuccess ( ) )
. catch ( ( ) = > onUnsubscribeFailure ( ) ) ;
} else {
dispatch ( followAccount ( account . get ( 'id' ) , { notify : true } as any ) )
? . then ( ( ) = > onSubscribeSuccess ( ) )
. catch ( ( ) = > onSubscribeFailure ( ) ) ;
}
} ;
const onSubscriptionToggle = ( ) = > {
if ( account . relationship ? . subscribing ) {
dispatch ( unsubscribeAccount ( account . get ( 'id' ) ) )
? . then ( ( ) = > onUnsubscribeSuccess ( ) )
. catch ( ( ) = > onUnsubscribeFailure ( ) ) ;
} else {
dispatch ( subscribeAccount ( account . get ( 'id' ) ) )
? . then ( ( ) = > onSubscribeSuccess ( ) )
. catch ( ( ) = > onSubscribeFailure ( ) ) ;
}
} ;
const handleToggle = ( ) = > {
if ( features . accountNotifies ) {
onNotifyToggle ( ) ;
} else {
onSubscriptionToggle ( ) ;
}
} ;
2022-07-06 11:20:20 -07:00
useEffect ( ( ) = > {
if ( features . accountSubscriptions || features . accountNotifies ) {
checkWebNotifications ( ) ;
}
} , [ ] ) ;
2022-06-14 08:58:42 -07:00
if ( ! features . accountSubscriptions && ! features . accountNotifies ) {
2022-06-13 08:35:54 -07:00
return null ;
}
2022-06-14 08:58:42 -07:00
if ( isRequested || isFollowing ) {
2022-06-13 08:35:54 -07:00
return (
< IconButton
src = { isSubscribed ? require ( '@tabler/icons/icons/bell-ringing.svg' ) : require ( '@tabler/icons/icons/bell.svg' ) }
onClick = { handleToggle }
title = { title }
2022-07-06 11:20:20 -07:00
className = 'text-primary-700 bg-primary-100 dark:!bg-slate-700 dark:!text-white hover:bg-primary-200 disabled:hover:bg-primary-100 p-2'
2022-06-13 08:35:54 -07:00
iconClassName = 'w-5 h-5'
/ >
) ;
}
return null ;
} ;
export default SubscriptionButton ;