Skip to content

Commit

Permalink
StreamList [nfc]: Convert to function component.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe authored and gnprice committed Jul 28, 2021
1 parent 2a55fba commit 6650e6e
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions src/streams/StreamList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { PureComponent } from 'react';
import React from 'react';
import { SectionList } from 'react-native';

import type { Stream, Subscription } from '../types';
Expand Down Expand Up @@ -38,67 +38,65 @@ type PseudoSubscription =
|}>;

type Props = $ReadOnly<{|
showDescriptions: boolean,
showSwitch: boolean,
streams: $ReadOnlyArray<PseudoSubscription>,
unreadByStream: $ReadOnly<{| [number]: number |}>,
showDescriptions?: boolean,
showSwitch?: boolean,
streams?: $ReadOnlyArray<PseudoSubscription>,
unreadByStream?: $ReadOnly<{| [number]: number |}>,
onPress: (streamName: string) => void,
onSwitch?: (streamName: string, newValue: boolean) => void,
|}>;

export default class StreamList extends PureComponent<Props> {
static defaultProps = {
showDescriptions: false,
showSwitch: false,
streams: [],
unreadByStream: {},
};
export default function StreamList(props: Props) {
const {
streams = [],
showDescriptions = false,
showSwitch = false,
unreadByStream = {},
onPress,
onSwitch,
} = props;

render() {
const { streams, showDescriptions, showSwitch, unreadByStream, onPress, onSwitch } = this.props;

if (streams.length === 0) {
return <SearchEmptyState text="No streams found" />;
}
if (streams.length === 0) {
return <SearchEmptyState text="No streams found" />;
}

const sortedStreams: PseudoSubscription[] = streams
.slice()
.sort((a, b) => caseInsensitiveCompareFunc(a.name, b.name));
const sections = [
{
key: 'Pinned',
data: sortedStreams.filter(x => x.pin_to_top),
},
{
key: 'Unpinned',
data: sortedStreams.filter(x => !x.pin_to_top),
},
];
const sortedStreams: PseudoSubscription[] = streams
.slice()
.sort((a, b) => caseInsensitiveCompareFunc(a.name, b.name));
const sections = [
{
key: 'Pinned',
data: sortedStreams.filter(x => x.pin_to_top),
},
{
key: 'Unpinned',
data: sortedStreams.filter(x => !x.pin_to_top),
},
];

return (
<SectionList
style={styles.list}
sections={sections}
extraData={unreadByStream}
initialNumToRender={20}
keyExtractor={item => item.stream_id}
renderItem={({ item }: { item: PseudoSubscription, ... }) => (
<StreamItem
name={item.name}
iconSize={16}
isPrivate={item.invite_only}
description={showDescriptions ? item.description : ''}
color={item.color}
unreadCount={unreadByStream[item.stream_id]}
isMuted={item.in_home_view === false} // if 'undefined' is not muted
showSwitch={showSwitch}
isSubscribed={item.subscribed}
onPress={onPress}
onSwitch={onSwitch}
/>
)}
SectionSeparatorComponent={SectionSeparatorBetween}
/>
);
}
return (
<SectionList
style={styles.list}
sections={sections}
extraData={unreadByStream}
initialNumToRender={20}
keyExtractor={item => item.stream_id}
renderItem={({ item }: { item: PseudoSubscription, ... }) => (
<StreamItem
name={item.name}
iconSize={16}
isPrivate={item.invite_only}
description={showDescriptions ? item.description : ''}
color={item.color}
unreadCount={unreadByStream[item.stream_id]}
isMuted={item.in_home_view === false} // if 'undefined' is not muted
showSwitch={showSwitch}
isSubscribed={item.subscribed}
onPress={onPress}
onSwitch={onSwitch}
/>
)}
SectionSeparatorComponent={SectionSeparatorBetween}
/>
);
}

0 comments on commit 6650e6e

Please sign in to comment.