2026-05-25 05:25:57 +08:00
|
|
|
import { useEffect } from "react";
|
2026-05-26 12:07:13 +08:00
|
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
|
|
|
import { getJSON } from "../api";
|
|
|
|
|
import { langQuery, useI18n } from "../i18n";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { MOCK_POSTS } from "../mocks/mockPosts";
|
|
|
|
|
import { POST_STREAM_USES_MOCK } from "../components/messageStream/hooks/usePostStream";
|
2026-05-26 12:07:13 +08:00
|
|
|
import type { Post } from "../types/post";
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
export function PostRedirect() {
|
|
|
|
|
const { id } = useParams();
|
2026-05-26 12:07:13 +08:00
|
|
|
const { lang } = useI18n();
|
|
|
|
|
const navigate = useNavigate();
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-26 12:07:13 +08:00
|
|
|
if (!id) {
|
|
|
|
|
navigate("/browse", { replace: true });
|
|
|
|
|
return;
|
2026-05-25 05:25:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-26 12:07:13 +08:00
|
|
|
if (POST_STREAM_USES_MOCK) {
|
|
|
|
|
const post = MOCK_POSTS.find((p) => p.id === id);
|
|
|
|
|
navigate(
|
|
|
|
|
post ? `/category/${post.categorySlug}#post-${post.id}` : "/browse",
|
|
|
|
|
{ replace: true },
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getJSON<Post>(
|
|
|
|
|
`/api/posts/${id}?lang=${encodeURIComponent(langQuery(lang))}`,
|
|
|
|
|
)
|
|
|
|
|
.then((post) => {
|
|
|
|
|
navigate(`/category/${post.categorySlug}#post-${post.id}`, {
|
|
|
|
|
replace: true,
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(() => navigate("/browse", { replace: true }));
|
|
|
|
|
}, [id, lang, navigate]);
|
|
|
|
|
|
|
|
|
|
return <div className="text-neutral-400">…</div>;
|
2026-05-25 05:25:57 +08:00
|
|
|
}
|