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:
@@ -21,17 +21,17 @@ import {
|
||||
Image,
|
||||
} from 'react-native';
|
||||
import { SafeAreaView } from 'react-native-safe-area-context';
|
||||
import { useNavigation, useRoute, RouteProp } from '@react-navigation/native';
|
||||
import { useRouter, useLocalSearchParams, useNavigation } from 'expo-router';
|
||||
import { MaterialCommunityIcons } from '@expo/vector-icons';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
import { colors, spacing, fontSizes, borderRadius, shadows } from '../../theme';
|
||||
import { Text, Button, ResponsiveContainer } from '../../components/common';
|
||||
import { Text, Button, ResponsiveContainer, AppBackButton } from '../../components/common';
|
||||
import { postService, showPrompt, voteService } from '../../services';
|
||||
import { ApiError } from '../../services/api';
|
||||
import { uploadService } from '../../services/uploadService';
|
||||
import VoteEditor from '../../components/business/VoteEditor';
|
||||
import { useResponsive, useResponsiveValue } from '../../hooks';
|
||||
import { RootStackParamList } from '../../navigation/types';
|
||||
import * as hrefs from '../../navigation/hrefs';
|
||||
|
||||
// Props 接口
|
||||
interface CreatePostScreenProps {
|
||||
@@ -80,10 +80,11 @@ const getPublishErrorMessage = (error: unknown): string => {
|
||||
|
||||
export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
const { onClose } = props;
|
||||
const router = useRouter();
|
||||
const navigation = useNavigation();
|
||||
const route = useRoute<RouteProp<RootStackParamList, 'CreatePost'>>();
|
||||
const isEditMode = route.params?.mode === 'edit' && !!route.params?.postId;
|
||||
const editPostID = route.params?.postId || '';
|
||||
const { mode, postId: postIdParam } = useLocalSearchParams<{ mode?: string; postId?: string }>();
|
||||
const isEditMode = mode === 'edit' && !!postIdParam;
|
||||
const editPostID = postIdParam || '';
|
||||
|
||||
// 响应式布局
|
||||
const { isWideScreen, width } = useResponsive();
|
||||
@@ -135,12 +136,11 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
navigation.setOptions({
|
||||
headerShown: true,
|
||||
title: isEditMode ? '编辑帖子' : '发布帖子',
|
||||
// 当作为 Modal 使用时,显示关闭按钮
|
||||
headerLeft: onClose ? () => (
|
||||
<TouchableOpacity onPress={onClose} style={{ padding: 8 }}>
|
||||
<MaterialCommunityIcons name="close" size={24} color={colors.text.primary} />
|
||||
</TouchableOpacity>
|
||||
<AppBackButton onPress={onClose} icon="close" />
|
||||
) : undefined,
|
||||
});
|
||||
}, [navigation, isEditMode, onClose]);
|
||||
@@ -156,7 +156,7 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
const existingPost = await postService.getPost(editPostID);
|
||||
if (!existingPost) {
|
||||
Alert.alert('提示', '帖子不存在或已被删除');
|
||||
navigation.goBack();
|
||||
router.back();
|
||||
return;
|
||||
}
|
||||
setTitle(existingPost.title || '');
|
||||
@@ -165,14 +165,14 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
} catch (error) {
|
||||
console.error('加载待编辑帖子失败:', error);
|
||||
Alert.alert('错误', '加载帖子失败,请稍后重试');
|
||||
navigation.goBack();
|
||||
router.back();
|
||||
} finally {
|
||||
setLoadingPost(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadPostForEdit();
|
||||
}, [isEditMode, editPostID, navigation]);
|
||||
}, [isEditMode, editPostID, router]);
|
||||
|
||||
// 选择图片
|
||||
const handlePickImage = async () => {
|
||||
@@ -381,10 +381,7 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
message: '投票帖已提交,内容审核中,稍后展示',
|
||||
duration: 2600,
|
||||
});
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Main' }],
|
||||
});
|
||||
router.replace(hrefs.hrefHome());
|
||||
} else {
|
||||
if (isEditMode && editPostID) {
|
||||
const updated = await postService.updatePost(editPostID, {
|
||||
@@ -401,10 +398,7 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
message: '帖子内容已更新',
|
||||
duration: 2200,
|
||||
});
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Main' }],
|
||||
});
|
||||
router.replace(hrefs.hrefHome());
|
||||
} else {
|
||||
// 创建普通帖子
|
||||
await postService.createPost({
|
||||
@@ -418,10 +412,7 @@ export const CreatePostScreen: React.FC<CreatePostScreenProps> = (props) => {
|
||||
message: '帖子已提交,内容审核中,稍后展示',
|
||||
duration: 2600,
|
||||
});
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Main' }],
|
||||
});
|
||||
router.replace(hrefs.hrefHome());
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user