[redditmon] inbox: automatically focus on first post in tabs (#1575)

This commit is contained in:
Zhou Yuhang 2025-07-28 10:44:39 +08:00 committed by GitHub
parent 52da3f0b02
commit 66ec297eee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View File

@ -34,6 +34,8 @@ export default function InterceptedPostDetailClient({
handlePrevPost,
markPostRelevancy,
markPostReplyStatus,
posts,
selectedPostId,
} = usePostsContext();
const toggleRelevant = () => {
@ -68,6 +70,15 @@ export default function InterceptedPostDetailClient({
};
}, [disableScope, enableScope]);
// Check if the current post is still valid in the context
const isPostValidInCurrentContext =
selectedPostId === post.id && posts.some((p) => p.id === post.id);
// If the post is not valid in the current context, don't render the detail
if (!isPostValidInCurrentContext) {
return null;
}
return (
<div className="flex h-full flex-col">
<div className="flex-1 overflow-y-auto">

View File

@ -59,6 +59,8 @@ export function PostsProvider({
const params = useParams();
const utils = trpc.useUtils();
const prevActiveTabRef = useRef<PostListTab>(activeTab);
// Store navigation context before mutations to handle auto-navigation
const navigationContextRef = useRef<{
currentIndex: number;
@ -169,6 +171,25 @@ export function PostsProvider({
}
}, [params?.id]);
useEffect(() => {
const hasTabChanged = prevActiveTabRef.current !== activeTab;
prevActiveTabRef.current = activeTab;
if (hasTabChanged) {
setSelectedPostId(null);
return;
}
if (!isLoading && posts.length > 0 && !selectedPostId) {
const firstPostId = posts[0]!.id;
setSelectedPostId(firstPostId);
router.push(`/projects/${projectSlug}/posts/${firstPostId}`);
}
}, [posts, selectedPostId, isLoading, projectSlug, router, activeTab]);
// Auto-load next page when user reaches the last post
useEffect(() => {
if (