BufferList.tsx (2016B)
1 import * as React from 'react'; 2 import { connect } from 'react-redux'; 3 import { StyleSheet, FlatList, View, ListRenderItem } from 'react-native'; 4 import BufferListItem from './BufferListItem'; 5 import { StoreState } from '../../../store'; 6 import { getHotlistForBufferId } from '../../../store/selectors'; 7 import { HotListState } from '../../../store/hotlists'; 8 9 interface Props { 10 buffers: WeechatBuffer[]; 11 currentBufferId: string | null; 12 onSelectBuffer: (b: WeechatBuffer) => void; 13 hotlists: HotListState; 14 filterBuffers: boolean; 15 } 16 17 const keyExtractor = (buffer: WeechatBuffer): string => buffer.id; 18 19 class BufferList extends React.Component<Props> { 20 renderListItem: ListRenderItem<WeechatBuffer> = ({ item }) => { 21 const { onSelectBuffer, currentBufferId, hotlists } = this.props; 22 23 return ( 24 <BufferListItem 25 buffer={item} 26 onSelectBuffer={onSelectBuffer} 27 currentBufferId={currentBufferId} 28 hotlist={getHotlistForBufferId(hotlists, item.id)} 29 /> 30 ); 31 }; 32 33 visibleBuffer = (buffer: WeechatBuffer) => { 34 const { filterBuffers, hotlists } = this.props; 35 36 if (filterBuffers) { 37 return ( 38 (buffer.local_variables.type != 'server' && 39 buffer.local_variables.type != null) || 40 (hotlists[buffer.id] && hotlists[buffer.id].sum != 0) 41 ); 42 } else { 43 return true; 44 } 45 }; 46 47 render() { 48 const { buffers } = this.props; 49 50 return ( 51 <View style={styles.container}> 52 <View style={styles.topbar} /> 53 <FlatList 54 style={styles.container} 55 data={buffers.filter(this.visibleBuffer)} 56 keyExtractor={keyExtractor} 57 renderItem={this.renderListItem} 58 /> 59 </View> 60 ); 61 } 62 } 63 64 export default connect((state: StoreState) => ({ 65 hotlists: state.hotlists, 66 filterBuffers: state.connection.filterBuffers 67 }))(BufferList); 68 69 const styles = StyleSheet.create({ 70 container: { 71 flex: 1, 72 backgroundColor: '#121212' 73 }, 74 topbar: { 75 height: 30 76 } 77 });