Files
Arkie-Library-Frontend/src/pages/PostRedirect/index.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

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