refactor(App, navigation): migrate to Expo Router and clean up navigation structure
- Updated App entry point to utilize Expo Router, simplifying the navigation setup. - Removed legacy navigation components and services to streamline the codebase. - Adjusted package.json to reflect the new entry point and updated dependencies for compatibility. - Enhanced overall application structure by consolidating navigation logic and improving maintainability.
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
||||
KeyboardEvent,
|
||||
Alert,
|
||||
} from 'react-native';
|
||||
import { useRoute, RouteProp, useNavigation } from '@react-navigation/native';
|
||||
import { useLocalSearchParams, router } from 'expo-router';
|
||||
import { formatDistanceToNow } from 'date-fns';
|
||||
import { zhCN } from 'date-fns/locale';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
@@ -30,8 +30,8 @@ import { useChat, useGroupTyping, useGroupMuted, messageManager } from '../../..
|
||||
import { groupService } from '../../../../services/groupService';
|
||||
import { userManager } from '../../../../stores/userManager';
|
||||
import { groupManager } from '../../../../stores/groupManager';
|
||||
import { RootStackParamList } from '../../../../navigation/types';
|
||||
import { navigationService } from '../../../../infrastructure/navigation/navigationService';
|
||||
import * as hrefs from '../../../../navigation/hrefs';
|
||||
import { firstRouteParam } from '../../../../navigation/paramUtils';
|
||||
import {
|
||||
GroupMessage,
|
||||
PanelType,
|
||||
@@ -46,8 +46,6 @@ import {
|
||||
clearConversationMessages,
|
||||
} from '../../../../services/database';
|
||||
|
||||
type ChatRouteProp = RouteProp<RootStackParamList, 'Chat'>;
|
||||
|
||||
export const useChatScreen = () => {
|
||||
const getSendErrorMessage = useCallback((error: unknown, fallback: string) => {
|
||||
if (error instanceof ApiError && error.message) {
|
||||
@@ -56,17 +54,30 @@ export const useChatScreen = () => {
|
||||
return fallback;
|
||||
}, []);
|
||||
|
||||
const route = useRoute<ChatRouteProp>();
|
||||
const navigation = useNavigation();
|
||||
|
||||
// 路由参数
|
||||
const routeParams = useMemo(() => ({
|
||||
conversationId: route.params?.conversationId || null,
|
||||
userId: route.params?.userId || null,
|
||||
isGroupChat: route.params?.isGroupChat || false,
|
||||
groupId: route.params?.groupId,
|
||||
groupName: route.params?.groupName,
|
||||
}), [route.params?.conversationId, route.params?.userId, route.params?.isGroupChat, route.params?.groupId, route.params?.groupName]);
|
||||
const rawParams = useLocalSearchParams<{
|
||||
conversationId?: string | string[];
|
||||
userId?: string | string[];
|
||||
isGroupChat?: string | string[];
|
||||
groupId?: string | string[];
|
||||
groupName?: string | string[];
|
||||
}>();
|
||||
// 路由参数(动态段 + query);群 ID 勿用 Number(),避免超过 2^53-1 时精度丢失
|
||||
const routeParams = useMemo(() => {
|
||||
const isGroupFlag = firstRouteParam(rawParams.isGroupChat);
|
||||
return {
|
||||
conversationId: firstRouteParam(rawParams.conversationId) ?? null,
|
||||
userId: firstRouteParam(rawParams.userId) ?? null,
|
||||
isGroupChat: isGroupFlag === '1' || isGroupFlag === 'true',
|
||||
groupId: firstRouteParam(rawParams.groupId),
|
||||
groupName: firstRouteParam(rawParams.groupName),
|
||||
};
|
||||
}, [
|
||||
rawParams.conversationId,
|
||||
rawParams.userId,
|
||||
rawParams.isGroupChat,
|
||||
rawParams.groupId,
|
||||
rawParams.groupName,
|
||||
]);
|
||||
|
||||
const { conversationId: routeConversationId, userId: routeUserId, isGroupChat, groupId: routeGroupId, groupName: routeGroupName } = routeParams;
|
||||
|
||||
@@ -399,8 +410,30 @@ export const useChatScreen = () => {
|
||||
console.error('获取成员信息失败:', error);
|
||||
}
|
||||
} catch (error) {
|
||||
const isGroupNotFound =
|
||||
error instanceof ApiError &&
|
||||
(error.code === 404 ||
|
||||
error.message === '群组不存在' ||
|
||||
error.message.includes('群组不存在'));
|
||||
|
||||
if (isGroupNotFound) {
|
||||
Alert.alert('提示', '该群组不存在或已解散', [
|
||||
{
|
||||
text: '确定',
|
||||
onPress: () => {
|
||||
if (router.canGoBack()) {
|
||||
router.back();
|
||||
} else {
|
||||
router.replace(hrefs.hrefMessages());
|
||||
}
|
||||
},
|
||||
},
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
console.error('获取群组信息失败:', error);
|
||||
Alert.alert('错误', '无法获取群组信息');
|
||||
Alert.alert('错误', getSendErrorMessage(error, '无法获取群组信息'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1129,7 +1162,7 @@ export const useChatScreen = () => {
|
||||
// 点击头像跳转到用户主页
|
||||
const handleAvatarPress = useCallback((userId: string) => {
|
||||
if (userId && userId !== currentUserId) {
|
||||
navigationService.navigate('UserProfile', { userId: String(userId) });
|
||||
router.push(hrefs.hrefUserProfile(String(userId)));
|
||||
}
|
||||
}, [currentUserId]);
|
||||
|
||||
@@ -1198,31 +1231,27 @@ export const useChatScreen = () => {
|
||||
// 导航到群组信息或用户资料
|
||||
const navigateToInfo = useCallback(() => {
|
||||
if (isGroupChat && routeGroupId) {
|
||||
navigation.navigate('GroupInfo' as any, {
|
||||
groupId: routeGroupId,
|
||||
conversationId: conversationId || undefined,
|
||||
});
|
||||
router.push(hrefs.hrefGroupInfo(routeGroupId, conversationId || undefined));
|
||||
} else if (otherUser?.id) {
|
||||
navigationService.navigate('UserProfile', { userId: String(otherUser.id) });
|
||||
router.push(hrefs.hrefUserProfile(String(otherUser.id)));
|
||||
}
|
||||
}, [navigation, isGroupChat, routeGroupId, otherUser]);
|
||||
}, [isGroupChat, routeGroupId, otherUser, conversationId]);
|
||||
|
||||
// 导航到聊天管理页面
|
||||
const navigateToChatSettings = useCallback(() => {
|
||||
if (isGroupChat && routeGroupId) {
|
||||
navigation.navigate('GroupInfo' as any, {
|
||||
groupId: routeGroupId,
|
||||
conversationId: conversationId || undefined,
|
||||
});
|
||||
router.push(hrefs.hrefGroupInfo(routeGroupId, conversationId || undefined));
|
||||
} else if (otherUser?.id && conversationId) {
|
||||
navigation.navigate('PrivateChatInfo' as any, {
|
||||
conversationId: conversationId,
|
||||
userId: String(otherUser.id),
|
||||
userName: otherUser.nickname,
|
||||
userAvatar: otherUser.avatar,
|
||||
});
|
||||
router.push(
|
||||
hrefs.hrefPrivateChatInfo({
|
||||
conversationId,
|
||||
userId: String(otherUser.id),
|
||||
userName: otherUser.nickname,
|
||||
userAvatar: otherUser.avatar,
|
||||
})
|
||||
);
|
||||
}
|
||||
}, [navigation, isGroupChat, routeGroupId, otherUser, conversationId]);
|
||||
}, [isGroupChat, routeGroupId, otherUser, conversationId]);
|
||||
|
||||
return {
|
||||
// 状态
|
||||
|
||||
Reference in New Issue
Block a user