mattermost-mobile-upstream/app/screens/edit_server/form.tsx
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

277 lines
10 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {Keyboard, Pressable, TouchableOpacity, View} from 'react-native';
import Animated, {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import Button from '@components/button';
import CompassIcon from '@components/compass_icon';
import FloatingTextInput, {type FloatingTextInputRef} from '@components/floating_input/floating_text_input_label';
import FormattedText from '@components/formatted_text';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
import {typography} from '@utils/typography';
import {removeProtocol, stripTrailingSlashes} from '@utils/url';
const ADVANCED_OPTIONS_COLLAPSED = 0;
const ADVANCED_OPTIONS_EXPANDED = 150; // Approximate height for the content
type Props = {
buttonDisabled: boolean;
connecting: boolean;
displayName?: string;
displayNameError?: string;
handleUpdate: () => void;
handleDisplayNameTextChanged: (text: string) => void;
handlePreauthSecretTextChanged: (text: string) => void;
preauthSecret?: string;
preauthSecretError?: string;
serverUrl: string;
setShowAdvancedOptions: React.Dispatch<React.SetStateAction<boolean>>;
showAdvancedOptions: boolean;
theme: Theme;
};
const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => ({
formContainer: {
alignItems: 'center',
maxWidth: 600,
width: '100%',
paddingHorizontal: 24,
},
fullWidth: {
width: '100%',
},
error: {
marginBottom: 18,
},
chooseText: {
alignSelf: 'flex-start',
color: changeOpacity(theme.centerChannelColor, 0.64),
marginTop: 8,
...typography('Body', 75, 'Regular'),
},
buttonContainer: {
width: '100%',
marginTop: 32,
},
advancedOptionsContainer: {
width: '100%',
marginTop: 16,
},
advancedOptionsHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
paddingVertical: 12,
paddingHorizontal: 4,
},
advancedOptionsTitle: {
color: theme.linkColor,
...typography('Body', 75, 'SemiBold'),
},
advancedOptionsContent: {
width: '100%',
overflow: 'visible',
},
endAdornment: {
top: 2,
},
}));
const hitSlop = {top: 8, right: 8, bottom: 8, left: 8};
const messages = defineMessages({
save: {
id: 'edit_server.save',
defaultMessage: 'Save',
},
saving: {
id: 'edit_server.saving',
defaultMessage: 'Saving',
},
advancedOptions: {
id: 'mobile.components.select_server_view.advancedOptions',
defaultMessage: 'Advanced Options',
},
preauthSecret: {
id: 'mobile.components.select_server_view.sharedSecret',
defaultMessage: 'Authentication secret',
},
preauthSecretHelp: {
id: 'mobile.components.select_server_view.sharedSecretHelp',
defaultMessage: 'The authentication secret shared by the administrator',
},
});
const EditServerForm = ({
buttonDisabled,
connecting,
displayName = '',
displayNameError,
handleUpdate,
handleDisplayNameTextChanged,
handlePreauthSecretTextChanged,
preauthSecret = '',
preauthSecretError,
serverUrl,
setShowAdvancedOptions,
showAdvancedOptions,
theme,
}: Props) => {
const {formatMessage} = useIntl();
const displayNameRef = useRef<FloatingTextInputRef>(null);
const preauthSecretRef = useRef<FloatingTextInputRef>(null);
const styles = getStyleSheet(theme);
const [isPreauthSecretVisible, setIsPreauthSecretVisible] = useState(false);
const togglePreauthSecretVisibility = useCallback(() => {
setIsPreauthSecretVisible((prevState) => !prevState);
}, []);
const preauthSecretEndAdornment = useMemo(() => (
<TouchableOpacity
onPress={togglePreauthSecretVisibility}
hitSlop={hitSlop}
style={styles.endAdornment}
>
<CompassIcon
name={isPreauthSecretVisible ? 'eye-off-outline' : 'eye-outline'}
size={20}
color={changeOpacity(theme.centerChannelColor, 0.64)}
/>
</TouchableOpacity>
), [isPreauthSecretVisible, styles.endAdornment, theme.centerChannelColor, togglePreauthSecretVisibility]);
const onUpdate = useCallback(() => {
Keyboard.dismiss();
handleUpdate();
}, [handleUpdate]);
const onDisplayNameSubmit = useCallback(() => {
if (showAdvancedOptions || preauthSecretError) {
preauthSecretRef.current?.focus();
} else {
onUpdate();
}
}, [showAdvancedOptions, preauthSecretError, onUpdate]);
const toggleAdvancedOptions = useCallback(() => {
setShowAdvancedOptions((prev: boolean) => !prev);
}, [setShowAdvancedOptions]);
const chevronRotation = useSharedValue(showAdvancedOptions ? 180 : 0);
const advancedOptionsStyle = useAnimatedStyle(() => ({
height: withTiming(showAdvancedOptions ? ADVANCED_OPTIONS_EXPANDED : ADVANCED_OPTIONS_COLLAPSED, {duration: 250}),
}));
useEffect(() => {
chevronRotation.value = withTiming(showAdvancedOptions ? 180 : 0, {duration: 250});
// chevronRotation is a Reanimated shared value; its reference is stable and does not need to be in the dependency array.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [showAdvancedOptions]);
const chevronAnimatedStyle = useAnimatedStyle(() => ({
transform: [{rotate: `${chevronRotation.value}deg`}],
}));
const saveButtonTestId = buttonDisabled ? 'edit_server_form.save.button.disabled' : 'edit_server_form.save.button';
return (
<View style={styles.formContainer}>
<View style={[styles.fullWidth, displayNameError?.length ? styles.error : undefined]}>
<FloatingTextInput
rawInput={true}
enablesReturnKeyAutomatically={true}
error={displayNameError}
label={formatMessage({
id: 'mobile.components.select_server_view.displayName',
defaultMessage: 'Display Name',
})}
onChangeText={handleDisplayNameTextChanged}
onSubmitEditing={onDisplayNameSubmit}
ref={displayNameRef}
returnKeyType={showAdvancedOptions || preauthSecretError ? 'next' : 'done'}
testID='edit_server_form.server_display_name.input'
theme={theme}
value={displayName}
/>
</View>
{!displayNameError &&
<FormattedText
defaultMessage={'Server: {url}'}
id={'edit_server.display_help'}
style={styles.chooseText}
testID={'edit_server_form.display_help'}
values={{url: removeProtocol(stripTrailingSlashes(serverUrl))}}
/>
}
<View style={styles.advancedOptionsContainer}>
<Pressable
onPress={toggleAdvancedOptions}
style={styles.advancedOptionsHeader}
testID='edit_server_form.advanced_options.toggle'
>
<Animated.View style={chevronAnimatedStyle}>
<CompassIcon
name={'chevron-down'}
size={20}
style={styles.advancedOptionsTitle}
/>
</Animated.View>
<FormattedText
defaultMessage='Advanced Options'
id='mobile.components.select_server_view.advancedOptions'
style={styles.advancedOptionsTitle}
/>
</Pressable>
<Animated.View style={[styles.advancedOptionsContent, advancedOptionsStyle]}>
{showAdvancedOptions && (
<>
<FloatingTextInput
rawInput={true}
enablesReturnKeyAutomatically={true}
endAdornment={preauthSecretEndAdornment}
error={preauthSecretError}
keyboardType={isPreauthSecretVisible ? 'visible-password' : 'default'}
label={formatMessage(messages.preauthSecret)}
onChangeText={handlePreauthSecretTextChanged}
onSubmitEditing={onUpdate}
ref={preauthSecretRef}
returnKeyType='done'
secureTextEntry={!isPreauthSecretVisible}
testID='edit_server_form.preauth_secret.input'
theme={theme}
value={preauthSecret}
/>
<FormattedText
{...messages.preauthSecretHelp}
style={styles.chooseText}
testID='edit_server_form.preauth_secret_help'
/>
</>
)}
</Animated.View>
</View>
<View style={styles.buttonContainer}>
<Button
disabled={buttonDisabled || connecting}
onPress={onUpdate}
testID={saveButtonTestId}
size='lg'
theme={theme}
text={formatMessage(connecting ? messages.saving : messages.save)}
showLoader={connecting}
/>
</View>
</View>
);
};
export default EditServerForm;