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';
|
2021-12-02 13:27:44 +00:00
|
|
|
import { Q, Model } from '@nozbe/watermelondb';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import ServerItem, { ROW_HEIGHT } from '../presentation/ServerItem';
|
2022-04-07 16:53:07 +00:00
|
|
|
import { shareExtensionInit } from '../lib/services/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';
|
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 });
|
|
|
|
const keyExtractor = (item: IServer) => item.id;
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
interface IServer extends Model {
|
|
|
|
id: string;
|
2022-01-17 16:10:39 +00:00
|
|
|
iconURL: string;
|
|
|
|
name: string;
|
2021-12-02 13:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectServerViewState {
|
|
|
|
servers: IServer[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectServerViewProps {
|
2022-01-17 16:10:39 +00:00
|
|
|
navigation: StackNavigationProp<ShareInsideStackParamList, 'SelectServerView'>;
|
2021-12-02 13:27:44 +00:00
|
|
|
server: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
state = { servers: [] as IServer[] };
|
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-01-17 16:10:39 +00:00
|
|
|
const servers = (await serversCollection.query(Q.where('rooms_updated_at', Q.notEq(null))).fetch()) as IServer[];
|
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
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
renderItem = ({ item }: { item: IServer }) => {
|
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
|
|
|
|
2021-12-02 13:27:44 +00:00
|
|
|
const mapStateToProps = ({ share }: any) => ({
|
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);
|