chore: migrate WorkspaceView to hooks (#5041)

* chore: migrate WorkspaceView to hooks

* minor tweak

* refactor registerDisabledComponent

* minor tweak

---------

Co-authored-by: Gleidson Daniel Silva <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2023-07-05 15:28:01 -03:00 committed by GitHub
parent 223550d88c
commit a76c5464cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 101 deletions

View File

@ -24,7 +24,7 @@ const _OutsideStack = () => {
<Outside.Navigator screenOptions={{ ...defaultHeader, ...themedHeader(theme), ...StackAnimation } as StackNavigationOptions}>
{/* @ts-ignore */}
<Outside.Screen name='NewServerView' component={NewServerView} options={NewServerView.navigationOptions} />
<Outside.Screen name='WorkspaceView' component={WorkspaceView} options={WorkspaceView.navigationOptions} />
<Outside.Screen name='WorkspaceView' component={WorkspaceView} />
{/* @ts-ignore */}
<Outside.Screen name='LoginView' component={LoginView} options={LoginView.navigationOptions} />
<Outside.Screen name='ForgotPasswordView' component={ForgotPasswordView} />

View File

@ -0,0 +1,23 @@
import React from 'react';
import { Text } from 'react-native';
import { useAppSelector } from '../../lib/hooks';
import { useTheme } from '../../theme';
import styles from './styles';
const RegisterDisabledComponent = () => {
const { colors } = useTheme();
const { Accounts_iframe_enabled, registrationText } = useAppSelector(state => ({
registrationText: state.settings.Accounts_RegistrationForm_LinkReplacementText as string,
Accounts_iframe_enabled: state.settings.Accounts_iframe_enabled as boolean
}));
if (Accounts_iframe_enabled) {
return null;
}
return <Text style={[styles.registrationText, { color: colors.auxiliaryText }]}>{registrationText}</Text>;
};
export default RegisterDisabledComponent;

View File

@ -2,9 +2,8 @@ import React from 'react';
import { StyleSheet, View } from 'react-native';
import FastImage from 'react-native-fast-image';
import { themes } from '../../lib/constants';
import { isTablet } from '../../lib/methods/helpers';
import { TSupportedThemes } from '../../theme';
import { useTheme } from '../../theme';
const SIZE = 96;
const MARGIN_TOP = isTablet ? 0 : 64;
@ -26,20 +25,19 @@ const styles = StyleSheet.create({
});
interface IServerAvatar {
theme: TSupportedThemes;
url: string;
image: string;
}
// TODO: missing skeleton
const ServerAvatar = React.memo(({ theme, url, image }: IServerAvatar) => (
<View style={styles.container}>
{image && (
<FastImage style={[styles.image, { borderColor: themes[theme].borderColor }]} source={{ uri: `${url}/${image}` }} />
)}
</View>
));
const ServerAvatar = React.memo(({ url, image }: IServerAvatar) => {
const { colors } = useTheme();
ServerAvatar.displayName = 'ServerAvatar';
return (
<View style={styles.container}>
{image && <FastImage style={[styles.image, { borderColor: colors.borderColor }]} source={{ uri: `${url}/${image}` }} />}
</View>
);
});
export default ServerAvatar;

View File

@ -1,53 +1,66 @@
import React from 'react';
import React, { useLayoutEffect } from 'react';
import { Text, View } from 'react-native';
import { StackNavigationProp, StackNavigationOptions } from '@react-navigation/stack';
import { connect } from 'react-redux';
import { useNavigation } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import { CompositeNavigationProp } from '@react-navigation/core';
import { OutsideModalParamList, OutsideParamList } from '../../stacks/types';
import I18n from '../../i18n';
import Button from '../../containers/Button';
import { themes } from '../../lib/constants';
import { TSupportedThemes, withTheme } from '../../theme';
import { useTheme } from '../../theme';
import FormContainer, { FormContainerInner } from '../../containers/FormContainer';
import { IApplicationState } from '../../definitions';
import { IAssetsFavicon512 } from '../../definitions/IAssetsFavicon512';
import { getShowLoginButton } from '../../selectors/login';
import ServerAvatar from './ServerAvatar';
import styles from './styles';
import { useAppSelector } from '../../lib/hooks';
import RegisterDisabledComponent from './RegisterDisabledComponent';
interface IWorkSpaceProp {
navigation: CompositeNavigationProp<
StackNavigationProp<OutsideParamList, 'WorkspaceView'>,
StackNavigationProp<OutsideModalParamList>
>;
theme?: TSupportedThemes;
Site_Name: string;
Site_Url: string;
server: string;
Assets_favicon_512: IAssetsFavicon512;
registrationForm: string;
registrationText: string;
showLoginButton: boolean;
Accounts_iframe_enabled: boolean;
inviteLinkToken: string;
}
type TNavigation = CompositeNavigationProp<
StackNavigationProp<OutsideParamList, 'WorkspaceView'>,
StackNavigationProp<OutsideModalParamList>
>;
class WorkspaceView extends React.Component<IWorkSpaceProp, any> {
static navigationOptions = (): StackNavigationOptions => ({
title: I18n.t('Your_workspace')
});
const useWorkspaceViewSelector = () =>
useAppSelector(state => ({
server: state.server.server,
Site_Name: state.settings.Site_Name as string,
Site_Url: state.settings.Site_Url as string,
Assets_favicon_512: state.settings.Assets_favicon_512 as IAssetsFavicon512,
registrationForm: state.settings.Accounts_RegistrationForm as string,
Accounts_iframe_enabled: state.settings.Accounts_iframe_enabled as boolean,
showLoginButton: getShowLoginButton(state),
inviteLinkToken: state.inviteLinks.token
}));
get showRegistrationButton() {
const { registrationForm, inviteLinkToken, Accounts_iframe_enabled } = this.props;
return (
!Accounts_iframe_enabled &&
(registrationForm === 'Public' || (registrationForm === 'Secret URL' && inviteLinkToken?.length))
);
}
const WorkspaceView = () => {
const navigation = useNavigation<TNavigation>();
login = () => {
const { navigation, server, Site_Name, Accounts_iframe_enabled } = this.props;
const { colors } = useTheme();
const {
Accounts_iframe_enabled,
Assets_favicon_512,
Site_Name,
Site_Url,
inviteLinkToken,
registrationForm,
server,
showLoginButton
} = useWorkspaceViewSelector();
useLayoutEffect(() => {
navigation.setOptions({
title: I18n.t('Your_workspace')
});
}, [navigation]);
const showRegistrationButton = !!(
!Accounts_iframe_enabled &&
(registrationForm === 'Public' || (registrationForm === 'Secret URL' && inviteLinkToken?.length))
);
const login = () => {
if (Accounts_iframe_enabled) {
navigation.navigate('AuthenticationWebView', { url: server, authType: 'iframe' });
return;
@ -55,61 +68,33 @@ class WorkspaceView extends React.Component<IWorkSpaceProp, any> {
navigation.navigate('LoginView', { title: Site_Name });
};
register = () => {
const { navigation, Site_Name } = this.props;
const register = () => {
navigation.navigate('RegisterView', { title: Site_Name });
};
renderRegisterDisabled = () => {
const { Accounts_iframe_enabled, registrationText, theme } = this.props;
if (Accounts_iframe_enabled) {
return null;
}
return (
<FormContainer testID='workspace-view'>
<FormContainerInner>
<View style={styles.alignItemsCenter}>
<ServerAvatar url={server} image={Assets_favicon_512?.url ?? Assets_favicon_512?.defaultUrl} />
<Text style={[styles.serverName, { color: colors.titleText }]}>{Site_Name}</Text>
<Text style={[styles.serverUrl, { color: colors.auxiliaryText }]}>{Site_Url}</Text>
</View>
{showLoginButton ? <Button title={I18n.t('Login')} type='primary' onPress={login} testID='workspace-view-login' /> : null}
{showRegistrationButton ? (
<Button
title={I18n.t('Create_account')}
type='secondary'
backgroundColor={colors.chatComponentBackground}
onPress={register}
testID='workspace-view-register'
/>
) : (
<RegisterDisabledComponent />
)}
</FormContainerInner>
</FormContainer>
);
};
return <Text style={[styles.registrationText, { color: themes[theme!].auxiliaryText }]}>{registrationText}</Text>;
};
render() {
const { theme, Site_Name, Site_Url, Assets_favicon_512, server, showLoginButton } = this.props;
return (
<FormContainer testID='workspace-view'>
<FormContainerInner>
<View style={styles.alignItemsCenter}>
<ServerAvatar theme={theme!} url={server} image={Assets_favicon_512?.url ?? Assets_favicon_512?.defaultUrl} />
<Text style={[styles.serverName, { color: themes[theme!].titleText }]}>{Site_Name}</Text>
<Text style={[styles.serverUrl, { color: themes[theme!].auxiliaryText }]}>{Site_Url}</Text>
</View>
{showLoginButton ? (
<Button title={I18n.t('Login')} type='primary' onPress={this.login} testID='workspace-view-login' />
) : null}
{this.showRegistrationButton ? (
<Button
title={I18n.t('Create_account')}
type='secondary'
backgroundColor={themes[theme!].chatComponentBackground}
onPress={this.register}
testID='workspace-view-register'
/>
) : (
this.renderRegisterDisabled()
)}
</FormContainerInner>
</FormContainer>
);
}
}
const mapStateToProps = (state: IApplicationState) => ({
server: state.server.server,
Site_Name: state.settings.Site_Name as string,
Site_Url: state.settings.Site_Url as string,
Assets_favicon_512: state.settings.Assets_favicon_512 as IAssetsFavicon512,
registrationForm: state.settings.Accounts_RegistrationForm as string,
registrationText: state.settings.Accounts_RegistrationForm_LinkReplacementText as string,
Accounts_iframe_enabled: state.settings.Accounts_iframe_enabled as boolean,
showLoginButton: getShowLoginButton(state),
inviteLinkToken: state.inviteLinks.token
});
export default connect(mapStateToProps)(withTheme(WorkspaceView));
export default WorkspaceView;