import { connect } from 'react-redux';
import StatusList from '../../../components/status_list';
import { scrollTopTimeline } from '../../../actions/timelines';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import { createSelector } from 'reselect';
import { debounce } from 'lodash';

const makeGetStatusIds = () => createSelector([
  (state, { type }) => state.getIn(['settings', type], ImmutableMap()),
  (state, { type }) => state.getIn(['timelines', type, 'items'], ImmutableList()),
  (state)           => state.get('statuses'),
  (state)           => state.getIn(['meta', 'me']),
], (columnSettings, statusIds, statuses, me) => statusIds.filter(id => {
  const statusForId = statuses.get(id);
  let showStatus    = true;

  if (columnSettings.getIn(['shows', 'reblog']) === false) {
    showStatus = showStatus && statusForId.get('reblog') === null;
  }

  if (columnSettings.getIn(['shows', 'reply']) === false) {
    showStatus = showStatus && (statusForId.get('in_reply_to_id') === null || statusForId.get('in_reply_to_account_id') === me);
  }

  if (columnSettings.getIn(['regex', 'body'], '').trim().length > 0) {
    try {
      if (showStatus) {
        const regex = new RegExp(columnSettings.getIn(['regex', 'body']).trim(), 'i');
        showStatus = !regex.test(statusForId.get('reblog') ? statuses.getIn([statusForId.get('reblog'), 'search_index']) : statusForId.get('search_index'));
      }
    } catch(e) {
      // Bad regex, don't affect filters
    }
  }

  return showStatus;
}));

const makeMapStateToProps = () => {
  const getStatusIds = makeGetStatusIds();

  const mapStateToProps = (state, { timelineId }) => ({
    statusIds: getStatusIds(state, { type: timelineId }),
    isLoading: state.getIn(['timelines', timelineId, 'isLoading'], true),
    hasMore: !!state.getIn(['timelines', timelineId, 'next']),
  });

  return mapStateToProps;
};

const mapDispatchToProps = (dispatch, { timelineId, loadMore }) => ({

  onScrollToBottom: debounce(() => {
    dispatch(scrollTopTimeline(timelineId, false));
    loadMore();
  }, 300, { leading: true }),

  onScrollToTop: debounce(() => {
    dispatch(scrollTopTimeline(timelineId, true));
  }, 100),

  onScroll: debounce(() => {
    dispatch(scrollTopTimeline(timelineId, false));
  }, 100),

});

export default connect(makeMapStateToProps, mapDispatchToProps)(StatusList);

@@ -649,24 +649,251 @@
 uses, me) =%3E
+ %7B%0A  const rawRegex = columnSettings.getIn(%5B'regex', 'body'%5D, '').trim();%0A  let regex      = null;%0A%0A  try %7B%0A    regex = rawRegex && new RegExp(rawRegex, 'i');%0A  %7D catch (e) %7B%0A    // Bad regex, don't affect filters%0A  %7D%0A%0A  return
  statusIds.f
@@ -904,24 +904,26 @@
 r(id =%3E %7B%0A  
+ 
+ 
 const status
@@ -948,16 +948,18 @@
 et(id);%0A
+  
   let sh
@@ -977,24 +977,26 @@
  = true;%0A%0A  
+ 
+ 
 if (columnSe
@@ -1034,32 +1034,34 @@
 %5D) === false) %7B%0A
+  
     showStatus =
@@ -1111,21 +1111,25 @@
 = null;%0A
+  
   %7D%0A%0A
+  
   if (co
@@ -1176,24 +1176,26 @@
 == false) %7B%0A
+  
     showStat
@@ -1317,236 +1317,107 @@
 ;%0A  
-%7D%0A%0A  if (columnSettings.getIn(%5B'regex', 'body'%5D, '').trim().length %3E 0) %7B%0A    try %7B%0A      if (showStatus) %7B%0A        const regex = new RegExp(columnSettings.getIn(%5B'regex', 'body'%5D).trim(), 'i');%0A        showStatus = !regex.test(
+  %7D%0A%0A    if (showStatus && regex && statusForId.get('account') !== me) %7B%0A      const searchIndex = 
 stat
@@ -1537,17 +1537,16 @@
 ex')
-)
 ;%0A      
 %7D%0A  
@@ -1545,79 +1545,56 @@
     
-%7D%0A    %7D catch(e) %7B%0A      // Bad regex, don't affect filters
+showStatus = !regex.test(searchIndex);
 %0A    %7D%0A
+%0A
   
-%7D%0A%0A
   re
@@ -1610,18 +1610,23 @@
 Status;%0A
+  %7D);%0A
 %7D
-)
 );%0A%0Acons

Exclude self toots from regular expression filter (#4245)