2026-05-25 05:25:57 +08:00
|
|
|
import { useEffect, useMemo, useRef } from "react";
|
2026-05-29 11:50:27 +08:00
|
|
|
import { useLocation, useSearchParams } from "react-router-dom";
|
2026-05-27 11:33:48 +08:00
|
|
|
import { postJSON } from "../../api";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { useI18n } from "../../i18n";
|
|
|
|
|
import type { PostScope } from "../../types/post";
|
2026-05-29 11:50:27 +08:00
|
|
|
import { Reveal } from "../../motion";
|
|
|
|
|
import { Skeleton } from "../Skeleton";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { FilterChips } from "./FilterChips";
|
|
|
|
|
import { MessageBubble } from "./MessageBubble";
|
|
|
|
|
import { useGroupedByDay } from "./hooks/useGroupedByDay";
|
|
|
|
|
import { usePostStream } from "./hooks/usePostStream";
|
|
|
|
|
|
|
|
|
|
export type MessageStreamProps = {
|
|
|
|
|
scope: PostScope;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function MessageStream({ scope }: MessageStreamProps) {
|
|
|
|
|
const { t, lang } = useI18n();
|
|
|
|
|
const [sp, setSp] = useSearchParams();
|
2026-05-29 11:50:27 +08:00
|
|
|
const { hash } = useLocation();
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
const type = sp.get("type") || "all";
|
2026-05-27 11:33:48 +08:00
|
|
|
const q = (sp.get("q") || "").trim();
|
2026-05-28 16:37:00 +08:00
|
|
|
const sort = sp.get("sort") || "";
|
2026-05-25 05:25:57 +08:00
|
|
|
|
2026-05-27 11:33:48 +08:00
|
|
|
const params = useMemo(
|
2026-05-28 16:49:30 +08:00
|
|
|
() => ({ scope, type, q, sort, lang }),
|
|
|
|
|
[scope, type, q, sort, lang],
|
2026-05-27 11:33:48 +08:00
|
|
|
);
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
const { items, isLoading, error, hasMore, loadMore, reset } =
|
|
|
|
|
usePostStream(params);
|
|
|
|
|
const groups = useGroupedByDay(items, lang);
|
2026-05-26 10:03:12 +08:00
|
|
|
const retryLabel = lang === "zh-CN" ? "重试" : "Retry";
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
const sentinelRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const hasMoreRef = useRef(hasMore);
|
|
|
|
|
const isLoadingRef = useRef(isLoading);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
hasMoreRef.current = hasMore;
|
|
|
|
|
}, [hasMore]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
isLoadingRef.current = isLoading;
|
|
|
|
|
}, [isLoading]);
|
|
|
|
|
|
2026-05-27 11:33:48 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (q) postJSON("/api/search-log", { query: q }).catch(() => {});
|
|
|
|
|
}, [q]);
|
|
|
|
|
|
2026-05-25 05:25:57 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const el = sentinelRef.current;
|
|
|
|
|
if (!el) return;
|
|
|
|
|
const io = new IntersectionObserver(
|
|
|
|
|
(entries) => {
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
if (
|
|
|
|
|
entry.isIntersecting &&
|
|
|
|
|
hasMoreRef.current &&
|
|
|
|
|
!isLoadingRef.current
|
|
|
|
|
) {
|
|
|
|
|
loadMore();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-05-29 19:04:31 +08:00
|
|
|
// Prefetch the next page well before the sentinel is visible so content
|
|
|
|
|
// is already in place as the user scrolls — no blank gap at the bottom.
|
|
|
|
|
{ rootMargin: "1000px" },
|
2026-05-25 05:25:57 +08:00
|
|
|
);
|
|
|
|
|
io.observe(el);
|
|
|
|
|
return () => io.disconnect();
|
|
|
|
|
}, [loadMore]);
|
|
|
|
|
|
2026-05-29 11:50:27 +08:00
|
|
|
// When arriving with a `#post-<id>` hash (e.g. from a recommended card),
|
|
|
|
|
// scroll to that bubble — loading more pages until it shows up — then give
|
|
|
|
|
// it a brief highlight so the user can see where they landed.
|
2026-05-29 12:49:22 +08:00
|
|
|
const targetPostId = hash.startsWith("#post-")
|
|
|
|
|
? hash.slice("#post-".length)
|
|
|
|
|
: "";
|
2026-05-29 11:50:27 +08:00
|
|
|
const handledTargetRef = useRef<string>("");
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
handledTargetRef.current = "";
|
|
|
|
|
}, [targetPostId]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!targetPostId || handledTargetRef.current === targetPostId) return;
|
|
|
|
|
|
|
|
|
|
const el = document.getElementById(`post-${targetPostId}`);
|
|
|
|
|
if (el) {
|
|
|
|
|
handledTargetRef.current = targetPostId;
|
|
|
|
|
const frame = window.requestAnimationFrame(() => {
|
|
|
|
|
el.scrollIntoView({ block: "start", behavior: "smooth" });
|
|
|
|
|
el.classList.add("ark-bubble-highlight");
|
|
|
|
|
window.setTimeout(
|
|
|
|
|
() => el.classList.remove("ark-bubble-highlight"),
|
|
|
|
|
2000,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
return () => window.cancelAnimationFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not loaded yet — keep paging until it appears or the stream is exhausted.
|
|
|
|
|
if (hasMore && !isLoading) loadMore();
|
|
|
|
|
}, [targetPostId, items, hasMore, isLoading, loadMore]);
|
|
|
|
|
|
2026-05-25 05:25:57 +08:00
|
|
|
const updateParam = (key: string, value: string) => {
|
|
|
|
|
const n = new URLSearchParams(sp);
|
|
|
|
|
if (!value || value === "all") n.delete(key);
|
|
|
|
|
else n.set(key, value);
|
|
|
|
|
setSp(n, { replace: true });
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-29 11:50:27 +08:00
|
|
|
const isInitialLoad = isLoading && items.length === 0;
|
|
|
|
|
|
2026-05-25 05:25:57 +08:00
|
|
|
return (
|
2026-05-28 15:11:13 +08:00
|
|
|
<div className="mx-auto max-w-full md:max-w-[820px] lg:max-w-[1080px] xl:max-w-[1180px]">
|
2026-05-26 12:07:13 +08:00
|
|
|
<FilterChips type={type} onTypeChange={(v) => updateParam("type", v)} />
|
2026-05-25 05:25:57 +08:00
|
|
|
|
2026-05-28 15:11:13 +08:00
|
|
|
<div className="flex flex-col gap-3 px-4 pt-4 md:px-0 md:pt-2">
|
2026-05-29 11:50:27 +08:00
|
|
|
{isInitialLoad ? (
|
|
|
|
|
<>
|
|
|
|
|
{Array.from({ length: 10 }).map((_, i) => (
|
|
|
|
|
<div
|
|
|
|
|
key={i}
|
|
|
|
|
className="mx-auto w-full max-w-[358px] md:max-w-[680px] lg:max-w-[900px] xl:max-w-[1120px]"
|
|
|
|
|
>
|
|
|
|
|
<Skeleton
|
|
|
|
|
className={`rounded-2xl ${
|
|
|
|
|
i % 3 === 0 ? "h-[220px]" : "h-[80px]"
|
|
|
|
|
}`}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{groups.map((group) => (
|
|
|
|
|
<div key={group.dayKey} className="flex flex-col gap-3">
|
|
|
|
|
{group.items.map((post, index) => (
|
|
|
|
|
<Reveal key={post.id} delay={Math.min(index, 8) * 0.05}>
|
|
|
|
|
<MessageBubble post={post} />
|
|
|
|
|
</Reveal>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2026-05-25 05:25:57 +08:00
|
|
|
))}
|
2026-05-29 11:50:27 +08:00
|
|
|
|
|
|
|
|
{!isLoading && !error && items.length === 0 ? (
|
|
|
|
|
<p className="py-10 text-center text-sm text-neutral-400">
|
|
|
|
|
{t("noResults")}
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
{error ? (
|
|
|
|
|
<div className="my-4 flex items-center justify-between gap-3 rounded-xl border border-red-900 bg-red-950/40 px-4 py-3 text-sm text-red-200">
|
|
|
|
|
<span className="break-all">{error}</span>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => reset()}
|
|
|
|
|
className="shrink-0 rounded-full border border-red-700 px-3 py-1 text-xs text-red-100 hover:border-red-500"
|
|
|
|
|
>
|
|
|
|
|
{retryLabel}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<div className="py-4 text-center text-xs text-neutral-500">…</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
<div ref={sentinelRef} aria-hidden className="h-1" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|