fix: fall back to original language on post redirect

This commit is contained in:
TerryM
2026-06-04 17:50:03 +08:00
parent ec98ff5a03
commit 1fcf2ea46d
9 changed files with 75 additions and 8 deletions

View File

@@ -5,13 +5,15 @@ import { langQuery, useI18n } from "../../i18n";
import { useLocalizedPath } from "../../useLocalizedPath";
import { MOCK_POSTS } from "../../mocks/mockPosts";
import { POST_STREAM_USES_MOCK } from "../../components/messageStream/hooks/usePostStream";
import { useToast } from "../../components/Toast";
import type { Post } from "../../types/post";
export function PostRedirect() {
const { id } = useParams();
const { lang } = useI18n();
const { lang, t } = useI18n();
const navigate = useNavigate();
const lp = useLocalizedPath();
const { showToast } = useToast();
useEffect(() => {
if (!id) {
@@ -30,16 +32,25 @@ export function PostRedirect() {
return;
}
const goToPost = (post: Post) => {
navigate(lp(`/browse?post=${encodeURIComponent(post.id)}`), {
replace: true,
});
};
getJSON<Post>(
`/api/posts/${id}?lang=${encodeURIComponent(langQuery(lang))}`,
)
.then((post) => {
navigate(lp(`/browse?post=${encodeURIComponent(post.id)}`), {
replace: true,
});
})
.catch(() => navigate(lp("/browse"), { replace: true }));
}, [id, lang, navigate, lp]);
.then(goToPost)
.catch(() => {
getJSON<Post>(`/api/posts/${id}`)
.then((post) => {
showToast(t("postShownInOriginalLanguage"));
goToPost(post);
})
.catch(() => navigate(lp("/browse"), { replace: true }));
});
}, [id, lang, navigate, lp, showToast, t]);
return <div className="text-neutral-400"></div>;
}