fix: preserve localized redirects

This commit is contained in:
TerryM
2026-06-01 16:36:36 +08:00
parent a968f47640
commit da4c13f304
2 changed files with 10 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ import { useEffect } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { getJSON } from "../../api";
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 type { Post } from "../../types/post";
@@ -10,17 +11,18 @@ export function PostRedirect() {
const { id } = useParams();
const { lang } = useI18n();
const navigate = useNavigate();
const lp = useLocalizedPath();
useEffect(() => {
if (!id) {
navigate("/browse", { replace: true });
navigate(lp("/browse"), { replace: true });
return;
}
if (POST_STREAM_USES_MOCK) {
const post = MOCK_POSTS.find((p) => p.id === id);
navigate(
post ? `/browse?post=${encodeURIComponent(post.id)}` : "/browse",
lp(post ? `/browse?post=${encodeURIComponent(post.id)}` : "/browse"),
{
replace: true,
},
@@ -32,12 +34,12 @@ export function PostRedirect() {
`/api/posts/${id}?lang=${encodeURIComponent(langQuery(lang))}`,
)
.then((post) => {
navigate(`/browse?post=${encodeURIComponent(post.id)}`, {
navigate(lp(`/browse?post=${encodeURIComponent(post.id)}`), {
replace: true,
});
})
.catch(() => navigate("/browse", { replace: true }));
}, [id, lang, navigate]);
.catch(() => navigate(lp("/browse"), { replace: true }));
}, [id, lang, navigate, lp]);
return <div className="text-neutral-400"></div>;
}

View File

@@ -1,7 +1,9 @@
import { Navigate, useSearchParams } from "react-router-dom";
import { useLocalizedPath } from "../../useLocalizedPath";
export function SearchPage() {
const [sp] = useSearchParams();
const lp = useLocalizedPath();
const query = sp.toString();
return <Navigate to={`/browse${query ? `?${query}` : ""}`} replace />;
return <Navigate to={lp(`/browse${query ? `?${query}` : ""}`)} replace />;
}