mattermost-mobile-upstream/app/screens/select_team/select_team.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

160 lines
4.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {useIntl} from 'react-intl';
import {View} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import {SafeAreaView, useSafeAreaInsets} from 'react-native-safe-area-context';
import {addCurrentUserToTeam, fetchTeamsForComponent, handleTeamChange} from '@actions/remote/team';
import Loading from '@components/loading';
import {Screens} from '@constants';
import {useServerUrl} from '@context/server';
import {useTheme} from '@context/theme';
import useDidMount from '@hooks/did_mount';
import {navigateToScreen} from '@screens/navigation';
import {logDebug} from '@utils/log';
import {alertTeamAddError} from '@utils/navigation';
import {makeStyleSheetFromTheme} from '@utils/theme';
import Header from './header';
import NoTeams from './no_teams';
import TeamList from './team_list';
const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => ({
container: {
flex: 1,
backgroundColor: theme.sidebarBg,
},
loading: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
}));
type Props = {
nTeams: number;
firstTeamId?: string;
}
const safeAreaEdges = ['left' as const, 'right' as const];
const safeAreaStyle = {flex: 1};
const SelectTeam = ({
nTeams,
firstTeamId,
}: Props) => {
const theme = useTheme();
const styles = getStyleSheet(theme);
const serverUrl = useServerUrl();
const intl = useIntl();
const insets = useSafeAreaInsets();
const resettingToHome = useRef(false);
const [loading, setLoading] = useState(true);
const [joining, setJoining] = useState(false);
const top = useAnimatedStyle(() => {
return {height: insets.top, backgroundColor: theme.sidebarBg};
});
const page = useRef(0);
const hasMore = useRef(true);
const mounted = useRef(false);
const [otherTeams, setOtherTeams] = useState<Team[]>([]);
const shouldRedirectToHome = (nTeams > 0) && firstTeamId;
const loadTeams = useCallback(async () => {
setLoading(true);
const resp = await fetchTeamsForComponent(serverUrl, page.current);
page.current = resp.page;
hasMore.current = resp.hasMore;
if (resp.teams.length && mounted.current) {
const teams = resp.teams.filter((t) => t.delete_at === 0);
setOtherTeams((cur) => [...cur, ...teams]);
}
setLoading(false);
}, [serverUrl]);
const onEndReached = useCallback(() => {
if (hasMore.current && !loading) {
loadTeams();
}
}, [loadTeams, loading]);
const onTeamPressed = useCallback(async (teamId: string) => {
setJoining(true);
const {error} = await addCurrentUserToTeam(serverUrl, teamId);
if (error) {
alertTeamAddError(error, intl);
logDebug('error joining a team:', error);
setJoining(false);
}
// Back to home handled in an effect
}, [serverUrl, intl]);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
useEffect(() => {
if (resettingToHome.current) {
return;
}
if (shouldRedirectToHome) {
resettingToHome.current = true;
handleTeamChange(serverUrl, firstTeamId).then(() => {
navigateToScreen(Screens.HOME, undefined, true);
});
}
// We only want to run this effect when the shouldRedirectToHome value changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [shouldRedirectToHome]);
useDidMount(() => {
loadTeams();
});
let body;
if (joining || (loading && !otherTeams.length)) {
body = <Loading containerStyle={styles.loading}/>;
} else if (otherTeams.length) {
body = (
<TeamList
teams={otherTeams}
onEndReached={onEndReached}
onPress={onTeamPressed}
loading={loading}
/>
);
} else {
body = (<NoTeams/>);
}
return (
<SafeAreaView
mode='margin'
edges={safeAreaEdges}
style={safeAreaStyle}
>
<Animated.View style={top}/>
<View style={styles.container}>
<Header/>
{body}
</View>
</SafeAreaView>
);
};
export default SelectTeam;