2017-06-12 20:26:23 +10:00
import { defineMessages , injectIntl } from 'react-intl' ;
2023-05-24 01:15:17 +10:00
import { connect } from 'react-redux' ;
2024-07-26 03:05:54 +10:00
import { openModal } from 'mastodon/actions/modal' ;
2024-07-19 00:36:09 +10:00
import { initializeNotifications } from 'mastodon/actions/notifications_migration' ;
2023-05-24 01:15:17 +10:00
import { showAlert } from '../../../actions/alerts' ;
2024-06-18 23:23:30 +10:00
import { updateNotificationsPolicy } from '../../../actions/notification_policies' ;
2024-07-19 00:36:09 +10:00
import { setFilter , requestBrowserPermission } from '../../../actions/notifications' ;
2017-12-24 14:47:35 +11:00
import { changeAlerts as changePushNotifications } from '../../../actions/push_notifications' ;
2023-05-24 01:15:17 +10:00
import { changeSetting } from '../../../actions/settings' ;
import ColumnSettings from '../components/column_settings' ;
2017-06-12 20:26:23 +10:00
const messages = defineMessages ( {
2020-10-16 01:24:47 +11:00
permissionDenied : { id : 'notifications.permission_denied_alert' , defaultMessage : 'Desktop notifications can\'t be enabled, as browser permission has been denied before' } ,
2017-06-12 20:26:23 +10:00
} ) ;
2017-01-03 00:09:57 +11:00
2024-06-18 23:23:30 +10:00
/ * *
* @ param { import ( 'mastodon/store' ) . RootState } state
* /
2017-01-03 00:09:57 +11:00
const mapStateToProps = state => ( {
2017-05-21 01:31:47 +10:00
settings : state . getIn ( [ 'settings' , 'notifications' ] ) ,
2017-07-14 06:15:32 +10:00
pushSettings : state . get ( 'push_notifications' ) ,
2020-10-13 09:37:21 +11:00
alertsEnabled : state . getIn ( [ 'settings' , 'notifications' , 'alerts' ] ) . includes ( true ) ,
browserSupport : state . getIn ( [ 'notifications' , 'browserSupport' ] ) ,
browserPermission : state . getIn ( [ 'notifications' , 'browserPermission' ] ) ,
2024-06-18 23:23:30 +10:00
notificationPolicy : state . notificationPolicy ,
2017-01-03 00:09:57 +11:00
} ) ;
2024-07-26 03:05:54 +10:00
const mapDispatchToProps = ( dispatch ) => ( {
2017-01-03 00:09:57 +11:00
2018-01-02 23:50:54 +11:00
onChange ( path , checked ) {
if ( path [ 0 ] === 'push' ) {
2020-10-13 09:37:21 +11:00
if ( checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
dispatch ( requestBrowserPermission ( ( permission ) => {
if ( permission === 'granted' ) {
dispatch ( changePushNotifications ( path . slice ( 1 ) , checked ) ) ;
} else {
2023-07-09 04:01:08 +10:00
dispatch ( showAlert ( { message : messages . permissionDenied } ) ) ;
2020-10-13 09:37:21 +11:00
}
} ) ) ;
} else {
dispatch ( changePushNotifications ( path . slice ( 1 ) , checked ) ) ;
}
2018-12-16 15:56:41 +11:00
} else if ( path [ 0 ] === 'quickFilter' ) {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
dispatch ( setFilter ( 'all' ) ) ;
2020-10-13 09:37:21 +11:00
} else if ( path [ 0 ] === 'alerts' && checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
if ( checked && typeof window . Notification !== 'undefined' && Notification . permission !== 'granted' ) {
dispatch ( requestBrowserPermission ( ( permission ) => {
if ( permission === 'granted' ) {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
} else {
2023-07-09 04:01:08 +10:00
dispatch ( showAlert ( { message : messages . permissionDenied } ) ) ;
2020-10-13 09:37:21 +11:00
}
} ) ) ;
} else {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
}
2024-07-19 00:36:09 +10:00
} else if ( path [ 0 ] === 'groupingBeta' ) {
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
dispatch ( initializeNotifications ( ) ) ;
2017-07-14 06:15:32 +10:00
} else {
2018-01-02 23:50:54 +11:00
dispatch ( changeSetting ( [ 'notifications' , ... path ] , checked ) ) ;
2017-07-14 06:15:32 +10:00
}
2017-01-11 03:25:10 +11:00
} ,
2017-06-12 20:26:23 +10:00
onClear ( ) {
2024-07-26 03:05:54 +10:00
dispatch ( openModal ( { modalType : 'CONFIRM_CLEAR_NOTIFICATIONS' } ) ) ;
2017-06-12 20:26:23 +10:00
} ,
2020-10-13 09:37:21 +11:00
onRequestNotificationPermission ( ) {
dispatch ( requestBrowserPermission ( ) ) ;
} ,
2024-03-12 02:02:21 +11:00
onChangePolicy ( param , checked ) {
dispatch ( updateNotificationsPolicy ( {
[ param ] : checked ,
} ) ) ;
} ,
2017-01-03 00:09:57 +11:00
} ) ;
2017-06-12 20:26:23 +10:00
export default injectIntl ( connect ( mapStateToProps , mapDispatchToProps ) ( ColumnSettings ) ) ;