2022-08-18 22:09:25 +00:00
|
|
|
import React, { useEffect, useLayoutEffect } from 'react';
|
2021-02-01 17:18:55 +00:00
|
|
|
import { FlatList } from 'react-native';
|
2022-08-18 22:09:25 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2022-05-03 01:13:03 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-08-18 22:09:25 +00:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
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-08-18 22:09:25 +00:00
|
|
|
import { TServerModel } from '../definitions';
|
|
|
|
import { useAppSelector } from '../lib/hooks';
|
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
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
const SelectServerView = () => {
|
|
|
|
const [servers, setServers] = React.useState<TServerModel[]>([]);
|
2021-12-02 13:27:44 +00:00
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
const server = useAppSelector(state => state.share.server.server);
|
|
|
|
const navigation = useNavigation<StackNavigationProp<ShareInsideStackParamList, 'SelectServerView'>>();
|
2021-12-02 13:27:44 +00:00
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Select_Server')
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const init = async () => {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.get('servers');
|
|
|
|
const serversResult = await serversCollection.query(Q.where('rooms_updated_at', Q.notEq(null))).fetch();
|
|
|
|
setServers(serversResult);
|
|
|
|
};
|
|
|
|
init();
|
|
|
|
}, []);
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
const select = async (serverSelected: string) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
navigation.navigate('ShareListView');
|
2022-08-18 22:09:25 +00:00
|
|
|
if (serverSelected !== server) {
|
|
|
|
await shareExtensionInit(serverSelected);
|
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-08-18 22:09:25 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView>
|
|
|
|
<StatusBar />
|
|
|
|
<FlatList
|
|
|
|
data={servers}
|
|
|
|
renderItem={({ item }: { item: TServerModel }) => (
|
|
|
|
<ServerItem onPress={() => select(item.id)} item={item} hasCheck={item.id === server} />
|
|
|
|
)}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
getItemLayout={getItemLayout} // Refactor row_height
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-08-18 22:09:25 +00:00
|
|
|
export default SelectServerView;
|