feat(profile): add chat settings and language options to profile settings
- Introduced hrefProfileChatSettings() for navigation to chat settings. - Updated SettingsScreen to include new settings items for chat settings, language selection, data storage, terms, and privacy policy. - Enhanced user experience by providing alerts for language and data storage options. This update improves the profile settings interface by adding more customization options for users.
This commit is contained in:
102
src/stores/post/hooks.ts
Normal file
102
src/stores/post/hooks.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* 帖子相关 React Hooks
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { usePostManagerStore } from './postStore';
|
||||
import { postManager } from './PostManager';
|
||||
import type { Post } from '../../types';
|
||||
import { POST_LIST_PAGE_SIZE } from '../postListSources';
|
||||
import type { PostListTab } from '../postListSources';
|
||||
|
||||
// ==================== usePostList ====================
|
||||
|
||||
export interface UsePostListResult {
|
||||
posts: Post[];
|
||||
isLoading: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帖子列表 hook
|
||||
*/
|
||||
export function usePostList(
|
||||
type: PostListTab = 'hot',
|
||||
page = 1,
|
||||
pageSize = POST_LIST_PAGE_SIZE
|
||||
): UsePostListResult {
|
||||
const posts = usePostManagerStore(state => {
|
||||
const key = `list:${type}:${page}:${pageSize}`;
|
||||
return state.listsMap.get(key)?.data ?? [];
|
||||
});
|
||||
const isLoading = usePostManagerStore(state => {
|
||||
const key = `list:${type}:${page}:${pageSize}`;
|
||||
return state.loadingListKeys.has(key);
|
||||
});
|
||||
const [hasFetched, setHasFetched] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasFetched) {
|
||||
setHasFetched(true);
|
||||
usePostManagerStore.getState().setLoadingList(type, page, pageSize, true);
|
||||
postManager.getPosts(type, page, pageSize).finally(() => {
|
||||
usePostManagerStore.getState().setLoadingList(type, page, pageSize, false);
|
||||
});
|
||||
}
|
||||
}, [type, page, pageSize, hasFetched]);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
usePostManagerStore.getState().setLoadingList(type, page, pageSize, true);
|
||||
try {
|
||||
await postManager.getPosts(type, page, pageSize, true);
|
||||
} finally {
|
||||
usePostManagerStore.getState().setLoadingList(type, page, pageSize, false);
|
||||
}
|
||||
}, [type, page, pageSize]);
|
||||
|
||||
return { posts, isLoading, refresh };
|
||||
}
|
||||
|
||||
// ==================== usePostDetail ====================
|
||||
|
||||
export interface UsePostDetailResult {
|
||||
post: Post | null;
|
||||
isLoading: boolean;
|
||||
refresh: () => Promise<Post | null>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帖子详情 hook
|
||||
*/
|
||||
export function usePostDetail(postId: string | undefined | null): UsePostDetailResult {
|
||||
const post = usePostManagerStore(state =>
|
||||
postId ? (state.detailsMap.get(postId)?.data ?? null) : null
|
||||
);
|
||||
const isLoading = usePostManagerStore(state =>
|
||||
postId ? state.loadingDetailIds.has(postId) : false
|
||||
);
|
||||
const [hasFetched, setHasFetched] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!postId) return;
|
||||
if (!hasFetched) {
|
||||
setHasFetched(true);
|
||||
usePostManagerStore.getState().setLoadingDetail(postId, true);
|
||||
postManager.getPostDetail(postId).finally(() => {
|
||||
usePostManagerStore.getState().setLoadingDetail(postId, false);
|
||||
});
|
||||
}
|
||||
}, [postId, hasFetched]);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!postId) return null;
|
||||
usePostManagerStore.getState().setLoadingDetail(postId, true);
|
||||
try {
|
||||
return await postManager.getPostDetail(postId, true);
|
||||
} finally {
|
||||
usePostManagerStore.getState().setLoadingDetail(postId, false);
|
||||
}
|
||||
}, [postId]);
|
||||
|
||||
return { post, isLoading, refresh };
|
||||
}
|
||||
Reference in New Issue
Block a user