2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-04-03 02:05:49 +01:00
|
|
|
import { List as ImmutableList } from 'immutable'
|
2020-04-24 04:17:27 +01:00
|
|
|
import { makeGetNotification } from '../selectors'
|
2020-04-09 20:18:14 +01:00
|
|
|
import Notification from '../components/notification'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-03 02:05:49 +01:00
|
|
|
const getAccountFromState = (state, accountId) => {
|
|
|
|
return state.getIn(['accounts', accountId])
|
|
|
|
}
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
const makeMapStateToProps = () => {
|
2020-03-04 03:45:16 +00:00
|
|
|
const getNotification = makeGetNotification()
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
const mapStateToProps = (state, props) => {
|
2020-04-03 02:05:49 +01:00
|
|
|
const isFollows = !!props.notification.get('follow')
|
|
|
|
const isLikes = !!props.notification.get('like')
|
|
|
|
const isReposts = !!props.notification.get('repost')
|
|
|
|
const isGrouped = isFollows || isLikes || isReposts
|
2020-05-21 20:37:40 +01:00
|
|
|
const lastReadId = state.getIn(['notifications', 'lastReadId'])
|
2020-12-08 05:07:04 +00:00
|
|
|
const isDeckConnected = state.getIn(['deck', 'connected'], false)
|
2020-03-04 03:45:16 +00:00
|
|
|
|
2020-04-03 02:05:49 +01:00
|
|
|
if (isFollows) {
|
2020-05-21 20:37:40 +01:00
|
|
|
let lastUpdated
|
2020-05-21 22:41:05 +01:00
|
|
|
let isUnread
|
|
|
|
|
2020-04-03 02:05:49 +01:00
|
|
|
const list = props.notification.get('follow')
|
|
|
|
let accounts = ImmutableList()
|
|
|
|
list.forEach((item) => {
|
|
|
|
const account = getAccountFromState(state, item.get('account'))
|
|
|
|
accounts = accounts.set(accounts.size, account)
|
2020-05-21 22:41:05 +01:00
|
|
|
if (!lastUpdated) {
|
|
|
|
isUnread = parseInt(item.get('id')) > parseInt(lastReadId)
|
|
|
|
lastUpdated = item.get('created_at')
|
|
|
|
}
|
2020-04-03 02:05:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'follow',
|
2020-05-21 20:37:40 +01:00
|
|
|
accounts: accounts,
|
|
|
|
createdAt: lastUpdated,
|
2020-05-21 22:41:05 +01:00
|
|
|
isUnread: isUnread,
|
2020-04-03 02:05:49 +01:00
|
|
|
statusId: undefined,
|
2020-12-08 05:07:04 +00:00
|
|
|
isDeckConnected,
|
2020-04-03 02:05:49 +01:00
|
|
|
}
|
|
|
|
} else if (isLikes || isReposts) {
|
|
|
|
const theType = isLikes ? 'like' : 'repost'
|
|
|
|
const list = props.notification.get(theType)
|
2020-05-21 20:37:40 +01:00
|
|
|
let lastUpdated = list.get('lastUpdated')
|
2020-05-21 22:41:05 +01:00
|
|
|
let isUnread = list.get('isUnread')
|
2020-03-04 03:45:16 +00:00
|
|
|
|
2020-04-03 02:05:49 +01:00
|
|
|
let accounts = ImmutableList()
|
|
|
|
const accountIdArr = list.get('accounts')
|
|
|
|
|
|
|
|
for (let i = 0; i < accountIdArr.length; i++) {
|
|
|
|
const accountId = accountIdArr[i];
|
|
|
|
const account = getAccountFromState(state, accountId)
|
|
|
|
accounts = accounts.set(accounts.size, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: theType,
|
|
|
|
accounts: accounts,
|
2020-05-21 20:37:40 +01:00
|
|
|
createdAt: lastUpdated,
|
2020-05-21 22:41:05 +01:00
|
|
|
isUnread: isUnread,
|
2021-02-25 15:40:52 +00:00
|
|
|
status: state.getIn(['statuses', list.get('status')], null),
|
2020-12-08 05:07:04 +00:00
|
|
|
isDeckConnected,
|
2020-04-03 02:05:49 +01:00
|
|
|
}
|
|
|
|
} else if (!isGrouped) {
|
|
|
|
const notification = getNotification(state, props.notification, props.notification.get('account'))
|
|
|
|
const account = notification.get('account')
|
|
|
|
const statusId = notification.get('status')
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: notification.get('type'),
|
2020-05-21 20:37:40 +01:00
|
|
|
accounts: !!account ? ImmutableList([account]) : ImmutableList(),
|
2020-04-03 02:05:49 +01:00
|
|
|
createdAt: notification.get('created_at'),
|
2020-05-21 20:37:40 +01:00
|
|
|
isUnread: lastReadId < notification.get('id'),
|
2021-02-25 15:40:52 +00:00
|
|
|
status: state.getIn(['statuses', statusId], null),
|
2020-12-08 05:07:04 +00:00
|
|
|
isDeckConnected,
|
2020-04-03 02:05:49 +01:00
|
|
|
}
|
2020-03-04 03:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-03-04 03:45:16 +00:00
|
|
|
return mapStateToProps
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-05-21 20:37:40 +01:00
|
|
|
export default connect(makeMapStateToProps)(Notification)
|