Navigation: "zurück zum Feed" springt zum Rootpost des Threads
Der Link trägt die pid des Rootposts im History-State; der Feed lädt notfalls Seiten nach, bis der Beitrag da ist, und scrollt hin. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4a4c79cdec
commit
20c17cbf81
@@ -24,7 +24,7 @@ export function EntryCard({ entry }: { entry: Entry }) {
|
||||
function DeletedCard({ entry: e }: { entry: Entry }) {
|
||||
const open = useOpenOnClick(e.pid);
|
||||
return (
|
||||
<article className="card" onClick={open}>
|
||||
<article className="card" data-pid={e.pid} onClick={open}>
|
||||
<div className="muted byline"><span>[deleted] · {formatTime(e.created_at)}</span></div>
|
||||
<div className="threadline-wrapper">
|
||||
<div className="threadline"></div>
|
||||
@@ -59,7 +59,7 @@ function LiveCard({ entry: e }: { entry: Entry }) {
|
||||
const profile = `/u/${encodeURIComponent(e.username)}`;
|
||||
|
||||
return (
|
||||
<article className="card" onClick={open}>
|
||||
<article className="card" data-pid={e.pid} onClick={open}>
|
||||
<div className="muted byline">
|
||||
<Link to={profile}>
|
||||
<img className="avatar avatar-sm" src={avatarUrl(e.avatar)} alt="" loading="lazy" />
|
||||
|
||||
@@ -12,11 +12,24 @@ const maxPage = 100;
|
||||
// Feed lädt Seiten per Infinite-Scroll: queryKey identifiziert den Feed im
|
||||
// Cache (zum Invalidieren nach eigenen Beiträgen), urlFor(page) baut den
|
||||
// API-Pfad der Seite. as ist das Container-Element: <section> auf der
|
||||
// Startseite, <div> innerhalb des Profils.
|
||||
export function Feed({ queryKey, urlFor, as: Container = "section" }: {
|
||||
// Startseite, <div> innerhalb des Profils. focus ist ein Beitrag, zu dem
|
||||
// gescrollt wird, notfalls nach dem Nachladen weiterer Seiten; onFocused
|
||||
// meldet, dass das erledigt ist (gefunden oder Feed zu Ende).
|
||||
// scrollBelowHeader scrollt el an den oberen Rand; mobil klebt der Kopf
|
||||
// oben (sticky) und würde den Beitrag sonst verdecken.
|
||||
function scrollBelowHeader(el: Element) {
|
||||
const header = document.querySelector("header");
|
||||
const sticky = header && getComputedStyle(header).position === "sticky";
|
||||
const offset = (sticky ? header.getBoundingClientRect().height : 0) + 8;
|
||||
window.scrollTo({ top: el.getBoundingClientRect().top + window.scrollY - offset });
|
||||
}
|
||||
|
||||
export function Feed({ queryKey, urlFor, as: Container = "section", focus, onFocused }: {
|
||||
queryKey: unknown[];
|
||||
urlFor: (page: number) => string;
|
||||
as?: "section" | "div";
|
||||
focus?: number;
|
||||
onFocused?: () => void;
|
||||
}) {
|
||||
const feed = useInfiniteQuery({
|
||||
queryKey,
|
||||
@@ -48,6 +61,20 @@ export function Feed({ queryKey, urlFor, as: Container = "section" }: {
|
||||
return () => observer.disconnect();
|
||||
}, [canLoad, fetchNextPage]);
|
||||
|
||||
const pageCount = feed.data?.pages.length ?? 0;
|
||||
useEffect(() => {
|
||||
if (!focus || !feed.isSuccess) return;
|
||||
const el = document.querySelector(`#feed [data-pid="${focus}"]`);
|
||||
if (el) {
|
||||
scrollBelowHeader(el);
|
||||
onFocused?.();
|
||||
} else if (canLoad) {
|
||||
fetchNextPage();
|
||||
} else if (!hasNextPage || isError) {
|
||||
onFocused?.();
|
||||
}
|
||||
}, [focus, feed.isSuccess, pageCount, canLoad, hasNextPage, isError, fetchNextPage, onFocused]);
|
||||
|
||||
// Zwischen zwei Seiten kann ein Beitrag durch einen Repost nach vorn
|
||||
// rutschen und auf der nächsten Seite erneut auftauchen -- nur einmal zeigen.
|
||||
const seen = new Set<number>();
|
||||
|
||||
Reference in New Issue
Block a user