vn-verdnaturachat/app/index.js

244 lines
5.7 KiB
JavaScript

import React from 'react';
import {
createStackNavigator, createAppContainer, createSwitchNavigator, createDrawerNavigator
} from 'react-navigation';
import { Provider } from 'react-redux';
import { useScreens } from 'react-native-screens'; // eslint-disable-line import/no-unresolved
import { Linking } from 'react-native';
import { appInit } from './actions';
import { deepLinkingOpen } from './actions/deepLinking';
import OnboardingView from './views/OnboardingView';
import NewServerView from './views/NewServerView';
import LoginSignupView from './views/LoginSignupView';
import AuthLoadingView from './views/AuthLoadingView';
import RoomsListView from './views/RoomsListView';
import RoomView from './views/RoomView';
import NewMessageView from './views/NewMessageView';
import LoginView from './views/LoginView';
import Navigation from './lib/Navigation';
import Sidebar from './views/SidebarView';
import ProfileView from './views/ProfileView';
import SettingsView from './views/SettingsView';
import RoomActionsView from './views/RoomActionsView';
import RoomInfoView from './views/RoomInfoView';
import RoomInfoEditView from './views/RoomInfoEditView';
import RoomMembersView from './views/RoomMembersView';
import SearchMessagesView from './views/SearchMessagesView';
import ThreadMessagesView from './views/ThreadMessagesView';
import MessagesView from './views/MessagesView';
import SelectedUsersView from './views/SelectedUsersView';
import CreateChannelView from './views/CreateChannelView';
import LegalView from './views/LegalView';
import ForgotPasswordView from './views/ForgotPasswordView';
import RegisterView from './views/RegisterView';
import OAuthView from './views/OAuthView';
import SetUsernameView from './views/SetUsernameView';
import { HEADER_BACKGROUND, HEADER_TITLE, HEADER_BACK } from './constants/colors';
import parseQuery from './lib/methods/helpers/parseQuery';
import { initializePushNotifications, onNotification } from './push';
import store from './lib/createStore';
useScreens();
const parseDeepLinking = (url) => {
if (url) {
url = url.replace(/rocketchat:\/\/|https:\/\/go.rocket.chat\//, '');
const regex = /^(room|auth)\?/;
if (url.match(regex)) {
url = url.replace(regex, '').trim();
if (url) {
return parseQuery(url);
}
}
}
return null;
};
const defaultHeader = {
headerStyle: {
backgroundColor: HEADER_BACKGROUND
},
headerTitleStyle: {
color: HEADER_TITLE
},
headerBackTitle: null,
headerTintColor: HEADER_BACK
};
// Outside
const OutsideStack = createStackNavigator({
OnboardingView: {
screen: OnboardingView,
header: null
},
NewServerView,
LoginSignupView,
LoginView,
ForgotPasswordView,
RegisterView,
LegalView
}, {
defaultNavigationOptions: defaultHeader
});
const OAuthStack = createStackNavigator({
OAuthView
}, {
defaultNavigationOptions: defaultHeader
});
const OutsideStackModal = createStackNavigator({
OutsideStack,
OAuthStack
},
{
mode: 'modal',
headerMode: 'none'
});
// Inside
const ChatsStack = createStackNavigator({
RoomsListView,
RoomView,
RoomActionsView,
RoomInfoView,
RoomInfoEditView,
RoomMembersView,
SearchMessagesView,
SelectedUsersView,
ThreadMessagesView,
MessagesView
}, {
defaultNavigationOptions: defaultHeader
});
ChatsStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if (navigation.state.index > 0) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode
};
};
const ProfileStack = createStackNavigator({
ProfileView
}, {
defaultNavigationOptions: defaultHeader
});
ProfileStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if (navigation.state.index > 0) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode
};
};
const SettingsStack = createStackNavigator({
SettingsView
}, {
defaultNavigationOptions: defaultHeader
});
SettingsStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if (navigation.state.index > 0) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode
};
};
const ChatsDrawer = createDrawerNavigator({
ChatsStack,
ProfileStack,
SettingsStack
}, {
contentComponent: Sidebar
});
const NewMessageStack = createStackNavigator({
NewMessageView,
SelectedUsersViewCreateChannel: SelectedUsersView,
CreateChannelView
}, {
defaultNavigationOptions: defaultHeader
});
const InsideStackModal = createStackNavigator({
Main: ChatsDrawer,
NewMessageStack
},
{
mode: 'modal',
headerMode: 'none'
});
const SetUsernameStack = createStackNavigator({
SetUsernameView
});
const App = createAppContainer(createSwitchNavigator(
{
OutsideStack: OutsideStackModal,
InsideStack: InsideStackModal,
AuthLoading: AuthLoadingView,
SetUsernameStack
},
{
initialRouteName: 'AuthLoading'
}
));
export default class Root extends React.Component {
constructor(props) {
super(props);
this.init();
}
componentDidMount() {
this.listenerTimeout = setTimeout(() => {
Linking.addEventListener('url', ({ url }) => {
const parsedDeepLinkingURL = parseDeepLinking(url);
if (parsedDeepLinkingURL) {
store.dispatch(deepLinkingOpen(parsedDeepLinkingURL));
}
});
}, 5000);
}
componentWillUnmount() {
clearTimeout(this.listenerTimeout);
}
init = async() => {
const [notification, deepLinking] = await Promise.all([initializePushNotifications(), Linking.getInitialURL()]);
const parsedDeepLinkingURL = parseDeepLinking(deepLinking);
if (notification) {
onNotification(notification);
} else if (parsedDeepLinkingURL) {
store.dispatch(deepLinkingOpen(parsedDeepLinkingURL));
} else {
store.dispatch(appInit());
}
}
render() {
return (
<Provider store={store}>
<App
ref={(navigatorRef) => {
Navigation.setTopLevelNavigator(navigatorRef);
}}
/>
</Provider>
);
}
}