2019-07-18 17:44:02 +00:00
|
|
|
import React from 'react';
|
2021-02-01 17:18:55 +00:00
|
|
|
import { FlatList } from 'react-native';
|
2021-12-02 13:27:44 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
2019-07-18 17:44:02 +00:00
|
|
|
import { connect } from 'react-redux';
|
2022-05-03 01:13:03 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2022-04-07 17:07:16 +00:00
|
|
|
import ServerItem, { ROW_HEIGHT } from '../containers/ServerItem';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { shareExtensionInit } from '../lib/methods/shareExtension';
|
2021-02-01 17:18:55 +00:00
|
|
|
import database from '../lib/database';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2021-02-01 17:18:55 +00:00
|
|
|
import * as List from '../containers/List';
|
2022-01-17 16:10:39 +00:00
|
|
|
import { ShareInsideStackParamList } from '../definitions/navigationTypes';
|
2022-05-03 01:13:03 +00:00
|
|
|
import { IApplicationState, TServerModel } from '../definitions';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
const getItemLayout = (data: any, index: number) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index });
|
2022-05-03 01:13:03 +00:00
|
|
|
const keyExtractor = (item: TServerModel) => item.id;
|
2021-12-02 13:27:44 +00:00
|
|
|
|
|
|
|
interface ISelectServerViewState {
|
2022-05-03 01:13:03 +00:00
|
|
|
servers: TServerModel[];
|
2021-12-02 13:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectServerViewProps {
|
2022-01-17 16:10:39 +00:00
|
|
|
navigation: StackNavigationProp<ShareInsideStackParamList, 'SelectServerView'>;
|
2022-05-03 01:13:03 +00:00
|
|
|
server?: string;
|
2021-12-02 13:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SelectServerView extends React.Component<ISelectServerViewProps, ISelectServerViewState> {
|
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
2019-07-18 17:44:02 +00:00
|
|
|
title: I18n.t('Select_Server')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2022-05-03 01:13:03 +00:00
|
|
|
state = { servers: [] };
|
2021-02-01 17:18:55 +00:00
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2022-05-03 01:13:03 +00:00
|
|
|
const servers = await serversCollection.query(Q.where('rooms_updated_at', Q.notEq(null))).fetch();
|
2021-02-01 17:18:55 +00:00
|
|
|
this.setState({ servers });
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
select = async (server: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { server: currentServer, navigation } = this.props;
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
navigation.navigate('ShareListView');
|
2019-07-18 17:44:02 +00:00
|
|
|
if (currentServer !== server) {
|
2022-03-16 20:40:32 +00:00
|
|
|
await shareExtensionInit(server);
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2022-05-03 01:13:03 +00:00
|
|
|
renderItem = ({ item }: { item: TServerModel }) => {
|
2021-02-01 17:18:55 +00:00
|
|
|
const { server } = this.props;
|
2021-09-13 20:41:05 +00:00
|
|
|
return <ServerItem onPress={() => this.select(item.id)} item={item} hasCheck={item.id === server} />;
|
|
|
|
};
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { servers } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView>
|
|
|
|
<StatusBar />
|
2021-02-01 17:18:55 +00:00
|
|
|
<FlatList
|
|
|
|
data={servers}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
getItemLayout={getItemLayout} // Refactor row_height
|
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
2019-07-18 17:44:02 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-05-03 01:13:03 +00:00
|
|
|
const mapStateToProps = ({ share }: IApplicationState) => ({
|
2020-11-04 16:53:44 +00:00
|
|
|
server: share.server.server
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2021-02-01 17:18:55 +00:00
|
|
|
export default connect(mapStateToProps)(SelectServerView);
|