Start at bottom of thread

merged…
osebo50d ago

When I visit a thread, I should be taken to the most recent reply at the bottom

3 repliesoldestnewest
Osebotfable-5-150d ago

Plan: on the thread page, scroll to the newest reply when the page opens.

  • Add a small client component (components/scroll-to-latest.tsx) that runs once on mount and scrolls the newest reply's post card into view, offset so the sticky compose dock doesn't cover it.
  • Mark the newest reply's <article> with an id (post-<id>) in app/threads/[id]/page.tsx so the component can target it; in ?order=newest mode the latest reply is already at the top, so the scroll is skipped there.
  • Respect URL hashes (#post-… links keep working) and don't scroll when there are no replies.
  • Also give every post an id="post-<id>" so deep links work.
Osebotfable-5-150d agothreadide@155d8ba

Change is committed and pushed (155d8ba on thread/b04ef836); tsc --noEmit is clean. Now taking a phone-size screenshot of a static mockup running the same scroll arithmetic so you can see where the page lands before I open the PR.

Osebotfable-5-150d agothreadide@155d8ba

Done — PR #59: https://github.com/osebo/threadide/pull/59

What changed

  • components/scroll-to-latest.tsx (new, client): on first paint, scrolls the page so the newest reply sits just above the docked composer. If the reply is taller than the viewport, it aligns to the reply's top so you read it from the start.
  • app/threads/[id]/page.tsx renders it after the replies — only when order=oldest and there is at least one reply. With ?order=newest the latest reply is already at the top, so nothing happens.
  • Every post <article> now has id="post-<postId>" (components/post.tsx) + .post { scroll-margin-top: 12px }, so #post-… deep links work. When the URL has a hash, the auto-scroll steps aside and the browser lands on the linked post.
  • The effect keys on the thread id only, so the router.refresh() ThreadLive fires when a new post/status lands does not yank you back to the bottom while you're reading.

Not included (say if you want it): following new replies as they arrive (auto-scroll when you're already near the bottom). Right now a new post appears without moving your viewport.

Screenshot is a 390px static mockup running the same scroll arithmetic (the app can't run in the sandbox); left = old landing position, right = new. tsc --noEmit is clean.

390px mockup: before (lands on pinned task) vs after (lands on newest reply, composer in view)
390px mockup: before (lands on pinned task) vs after (lands on newest reply, composer in view)
Diff · 4 files
diff --git a/threadide/app/globals.css b/threadide/app/globals.css
@@ -377,6 +377,8 @@
 .repo-row:last-of-type { border-bottom: 0; }
+/* Deep links (#post-…) and the open-at-latest scroll leave a little air above the target card. */
+.post { scroll-margin-top: 12px; }
 .replies-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; margin: 24px 0 4px; text-transform: uppercase; letter-spacing: .05em; }
diff --git a/threadide/app/threads/[id]/page.tsx b/threadide/app/threads/[id]/page.tsx
@@ -8,6 +8,7 @@
 import { OrderToggle } from "@/components/order-toggle";
+import { ScrollToLatest } from "@/components/scroll-to-latest";
@@ -75,6 +76,9 @@
   const [first, ...replies] = thread.posts;
+  // Oldest-first puts the newest reply at the bottom; open the page there. Newest-first already
+  // has it at the top, and a thread with no replies has nothing to jump to.
+  const latest = order === "oldest" && replies.length > 0 ? replies[replies.length - 1] : null;
@@ -119,6 +123,7 @@
       {(order === "oldest" ? replies : [...replies].reverse()).map((p) => (
         <PostView key={p.id} post={p} refs={refMap} threadId={thread.id} canAskClaude={canAskClaude} />
       ))}
+      {latest && <ScrollToLatest threadId={thread.id} postId={latest.id} />}
diff --git a/threadide/components/post.tsx b/threadide/components/post.tsx
@@ -41,7 +41,7 @@
-    <article className={`post ${post.author}${pinned ? " pinned" : ""}`}>
+    <article id={`post-${post.id}`} className={`post ${post.author}${pinned ? " pinned" : ""}`}>
diff --git a/threadide/components/scroll-to-latest.tsx b/threadide/components/scroll-to-latest.tsx
new file mode 100644
@@ -0,0 +1,32 @@
+"use client";
+import { useEffect } from "react";
+
+/**
+ * Opens the thread at its most recent reply instead of the top: on first paint, scrolls so the
+ * newest reply sits just above the docked composer. Runs ONCE per thread visit (not on the
+ * router.refresh() ThreadLive triggers when new posts land), and steps aside when the URL carries
+ * a #post-… hash so deep links keep landing where they point.
+ *
+ * A reply taller than the viewport is aligned to its START so it reads from the beginning;
+ * anything shorter is aligned to its END so the whole post plus the composer are in view.
+ */
+export function ScrollToLatest({ threadId, postId }: { threadId: string; postId: string }) {
+  useEffect(() => {
+    if (window.location.hash) return;
+    const el = document.getElementById(`post-${postId}`);
+    if (!el) return;
+    const dock = document.querySelector<HTMLElement>(".compose-dock");
+    const dockHeight = dock?.getBoundingClientRect().height ?? 0;
+    const rect = el.getBoundingClientRect();
+    const visible = window.innerHeight - dockHeight;
+    const gap = 12;
+    const top =
+      rect.height + gap * 2 <= visible
+        ? window.scrollY + rect.bottom - visible + gap // fits: bottom of the post just above the dock
+        : window.scrollY + rect.top - gap; // too tall: start reading at its top
+    window.scrollTo({ top: Math.max(0, top), behavior: "instant" });
+    // Only re-run when the visitor opens a different thread, never on data refreshes.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [threadId]);
+  return null;
+}