From 20c17cbf81f69e0f355b7b81725b5ecfcb0c0472 Mon Sep 17 00:00:00 2001 From: irrlicht Date: Sat, 10 Oct 2026 21:32:05 +0200 Subject: [PATCH] =?UTF-8?q?Navigation:=20"zur=C3=BCck=20zum=20Feed"=20spri?= =?UTF-8?q?ngt=20zum=20Rootpost=20des=20Threads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q --- frontend/src/Layout.tsx | 13 ++++++++--- frontend/src/components/EntryCard.tsx | 4 ++-- frontend/src/components/Feed.tsx | 31 +++++++++++++++++++++++++-- frontend/src/pages/EntryPage.tsx | 26 ++++++++++++++-------- frontend/src/pages/FeedPage.tsx | 13 ++++++++++- notes/frontend.md | 4 ++++ 6 files changed, 74 insertions(+), 17 deletions(-) diff --git a/frontend/src/Layout.tsx b/frontend/src/Layout.tsx index 8f52e41..fdd5fb7 100644 --- a/frontend/src/Layout.tsx +++ b/frontend/src/Layout.tsx @@ -1,7 +1,9 @@ import { useEffect, useState } from "react"; -import { Link, Outlet, useLocation } from "react-router"; +import { Link, Outlet, useLocation, useMatch } from "react-router"; +import { useQuery } from "@tanstack/react-query"; import { Menu, Settings } from "lucide-react"; import { Sidebar, TabBar, isReady } from "./Nav"; +import { threadQuery, threadRoot } from "./pages/EntryPage"; // Tage ohne Datenverlust (fester Startpunkt, Kontinuität seit der alten // Version). @@ -49,12 +51,17 @@ function useIsDesktop() { // unten die TabBar mit den fünf wichtigsten Zielen; das Menü blendet dieselbe // Seitenleiste als Schublade ein. Umgeschaltet wird per CSS (app.css), nur // der Menüknopf fragt die Breite ab. Außer der Startseite haben alle Seiten -// den "zurück zum Feed"-Knopf. +// den "zurück zum Feed"-Knopf; aus einem Thread führt er zu dessen Rootpost +// im Feed (FeedPage, focus). export function Layout() { const path = useLocation().pathname; const desktop = useIsDesktop(); const [menuOpen, setMenuOpen] = useState(false); const [collapsed, setCollapsed] = useState(loadCollapsed); + // Dieselbe Abfrage wie die Thread-Seite, kommt also aus dem Cache. + const threadPid = useMatch("/e/:pid")?.params.pid ?? ""; + const thread = useQuery({ ...threadQuery(threadPid), enabled: !!threadPid }).data; + const focus = threadPid && thread ? threadRoot(thread) : undefined; const days = Math.floor((Date.now() - dataLossStart) / 86400000); const close = () => setMenuOpen(false); @@ -105,7 +112,7 @@ export function Layout() { Ohne Datenverlust seit {plural(days, "Tag", "Tagen")}

) : ( -
zurück zum Feed
+
zurück zum Feed
)} diff --git a/frontend/src/components/EntryCard.tsx b/frontend/src/components/EntryCard.tsx index eb1f9dd..32efc07 100644 --- a/frontend/src/components/EntryCard.tsx +++ b/frontend/src/components/EntryCard.tsx @@ -24,7 +24,7 @@ export function EntryCard({ entry }: { entry: Entry }) { function DeletedCard({ entry: e }: { entry: Entry }) { const open = useOpenOnClick(e.pid); return ( -
+
[deleted] · {formatTime(e.created_at)}
@@ -59,7 +59,7 @@ function LiveCard({ entry: e }: { entry: Entry }) { const profile = `/u/${encodeURIComponent(e.username)}`; return ( -
+
diff --git a/frontend/src/components/Feed.tsx b/frontend/src/components/Feed.tsx index 2cf6d0d..bf2c2a6 100644 --- a/frontend/src/components/Feed.tsx +++ b/frontend/src/components/Feed.tsx @@ -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:
auf der -// Startseite,
innerhalb des Profils. -export function Feed({ queryKey, urlFor, as: Container = "section" }: { +// Startseite,
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(); diff --git a/frontend/src/pages/EntryPage.tsx b/frontend/src/pages/EntryPage.tsx index bb164fe..df67c7a 100644 --- a/frontend/src/pages/EntryPage.tsx +++ b/frontend/src/pages/EntryPage.tsx @@ -1,6 +1,6 @@ import { useState, type FormEvent } from "react"; import { useParams } from "react-router"; -import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { queryOptions, useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { api, errMsg } from "../api"; import { useMe } from "../auth"; import { Discussion, EntryCard } from "../components/EntryCard"; @@ -8,6 +8,21 @@ import { PostForm } from "../components/PostForm"; import { previewPoll } from "../previewPoll"; import type { Entry, Thread } from "../types"; +// threadQuery lädt einen Beitrag mit Vorfahren und Antworten. Geteilt mit dem +// Layout, das daraus den Rootpost für "zurück zum Feed" liest. +export const threadQuery = (pid: string) => + queryOptions({ + queryKey: ["thread", pid], + queryFn: () => api.get(`/entry/${pid}/thread`), + refetchInterval: (q) => { + const t = q.state.data; + return previewPoll(t ? [...t.ancestors, t.entry, ...t.replies] : [], q); + }, + }); + +// threadRoot ist der oberste Beitrag des Threads, also der, der im Feed steht. +export const threadRoot = (t: Thread) => (t.ancestors[0] ?? t.entry).pid; + // Beim Wechsel zu einem anderen Beitrag frisch anfangen (offene Bearbeitung, // Meldungen) -- deshalb hängt die Seite per key an der pid. export function EntryPage() { @@ -17,14 +32,7 @@ export function EntryPage() { function ThreadView({ pid }: { pid: string }) { const qc = useQueryClient(); - const thread = useQuery({ - queryKey: ["thread", pid], - queryFn: () => api.get(`/entry/${pid}/thread`), - refetchInterval: (q) => { - const t = q.state.data; - return previewPoll(t ? [...t.ancestors, t.entry, ...t.replies] : [], q); - }, - }); + const thread = useQuery(threadQuery(pid)); const me = useMe().data; // Nach eigenen Änderungen den Thread und alle Feeds (Antwortzahl, Inhalt) diff --git a/frontend/src/pages/FeedPage.tsx b/frontend/src/pages/FeedPage.tsx index c020d9f..02a60c8 100644 --- a/frontend/src/pages/FeedPage.tsx +++ b/frontend/src/pages/FeedPage.tsx @@ -1,4 +1,5 @@ import { useQueryClient } from "@tanstack/react-query"; +import { useLocation, useNavigate } from "react-router"; import { useMe } from "../auth"; import { AuthBox } from "../components/AuthBox"; import { Feed } from "../components/Feed"; @@ -6,12 +7,22 @@ import { PostForm } from "../components/PostForm"; // Solange der Anmeldestatus noch lädt, weder Login noch Formular zeigen -- // sonst blitzt bei Angemeldeten kurz die Login-Box auf. +// focus im History-State (gesetzt von "zurück zum Feed" im Thread) ist der +// Beitrag, zu dem der Feed scrollt. Danach wird er gelöscht, damit Reload +// oder Browser-Zurück nicht erneut springen. export function FeedPage() { const me = useMe(); + const navigate = useNavigate(); + const focus = (useLocation().state as { focus?: number } | null)?.focus; return ( <> {me.isSuccess && (me.data ? : )} - `/entry/feed/${page}`} /> + `/entry/feed/${page}`} + focus={focus} + onFocused={() => navigate(".", { replace: true, state: null })} + /> ); } diff --git a/notes/frontend.md b/notes/frontend.md index 6b762b9..3ab014e 100644 --- a/notes/frontend.md +++ b/notes/frontend.md @@ -68,6 +68,10 @@ noch Platzhalter und deshalb ausgeblendet (`unfinished` in `src/Nav.tsx`); ihre Routen bestehen weiter. Umgeschaltet wird per CSS (`app.css`, Abschnitt Navigation); nur der Menüknopf fragt die Breite ab. +"zurück zum Feed" führt aus einem Thread zum Rootpost im Feed: der Link +trägt dessen pid im History-State (`focus`), der Feed lädt notfalls Seiten +nach, bis der Beitrag (`data-pid`) da ist, scrollt hin und löscht den State. + ## Impressions Das Backend zählt Seitenaufrufe beim Ausliefern der `index.html`. Navigation