fix: preserve localized redirects
This commit is contained in:
@@ -2,6 +2,7 @@ import { useEffect } from "react";
|
|||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { getJSON } from "../../api";
|
import { getJSON } from "../../api";
|
||||||
import { langQuery, useI18n } from "../../i18n";
|
import { langQuery, useI18n } from "../../i18n";
|
||||||
|
import { useLocalizedPath } from "../../useLocalizedPath";
|
||||||
import { MOCK_POSTS } from "../../mocks/mockPosts";
|
import { MOCK_POSTS } from "../../mocks/mockPosts";
|
||||||
import { POST_STREAM_USES_MOCK } from "../../components/messageStream/hooks/usePostStream";
|
import { POST_STREAM_USES_MOCK } from "../../components/messageStream/hooks/usePostStream";
|
||||||
import type { Post } from "../../types/post";
|
import type { Post } from "../../types/post";
|
||||||
@@ -10,17 +11,18 @@ export function PostRedirect() {
|
|||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
const { lang } = useI18n();
|
const { lang } = useI18n();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const lp = useLocalizedPath();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
navigate("/browse", { replace: true });
|
navigate(lp("/browse"), { replace: true });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (POST_STREAM_USES_MOCK) {
|
if (POST_STREAM_USES_MOCK) {
|
||||||
const post = MOCK_POSTS.find((p) => p.id === id);
|
const post = MOCK_POSTS.find((p) => p.id === id);
|
||||||
navigate(
|
navigate(
|
||||||
post ? `/browse?post=${encodeURIComponent(post.id)}` : "/browse",
|
lp(post ? `/browse?post=${encodeURIComponent(post.id)}` : "/browse"),
|
||||||
{
|
{
|
||||||
replace: true,
|
replace: true,
|
||||||
},
|
},
|
||||||
@@ -32,12 +34,12 @@ export function PostRedirect() {
|
|||||||
`/api/posts/${id}?lang=${encodeURIComponent(langQuery(lang))}`,
|
`/api/posts/${id}?lang=${encodeURIComponent(langQuery(lang))}`,
|
||||||
)
|
)
|
||||||
.then((post) => {
|
.then((post) => {
|
||||||
navigate(`/browse?post=${encodeURIComponent(post.id)}`, {
|
navigate(lp(`/browse?post=${encodeURIComponent(post.id)}`), {
|
||||||
replace: true,
|
replace: true,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(() => navigate("/browse", { replace: true }));
|
.catch(() => navigate(lp("/browse"), { replace: true }));
|
||||||
}, [id, lang, navigate]);
|
}, [id, lang, navigate, lp]);
|
||||||
|
|
||||||
return <div className="text-neutral-400">…</div>;
|
return <div className="text-neutral-400">…</div>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { Navigate, useSearchParams } from "react-router-dom";
|
import { Navigate, useSearchParams } from "react-router-dom";
|
||||||
|
import { useLocalizedPath } from "../../useLocalizedPath";
|
||||||
|
|
||||||
export function SearchPage() {
|
export function SearchPage() {
|
||||||
const [sp] = useSearchParams();
|
const [sp] = useSearchParams();
|
||||||
|
const lp = useLocalizedPath();
|
||||||
const query = sp.toString();
|
const query = sp.toString();
|
||||||
return <Navigate to={`/browse${query ? `?${query}` : ""}`} replace />;
|
return <Navigate to={lp(`/browse${query ? `?${query}` : ""}`)} replace />;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user