From cb05ffdc947ba47013092bc93dd168875da6f47b Mon Sep 17 00:00:00 2001 From: Kjell Kind Date: Tue, 17 Nov 2026 23:44:22 +0100 Subject: [PATCH] fix: open a reply's page on the reply instead of the top of its thread A commit's page lists its ancestors above it, so a reply deep in a thread opened off screen. A fresh visit now scrolls the opened commit under the header with the end of its parent above it. Back, Forward, reloads, anchors and opening a photo keep their position. Co-Authored-By: Claude Opus 5.5 --- KNOWLEDGE_BASE.md | 2 +- resources/js/app.tsx | 2 + resources/js/lib/focused-commit-scroll.ts | 40 +++++++++++++++ tests/Unit/FocusedCommitScroll.test.ts | 59 +++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 resources/js/lib/focused-commit-scroll.ts create mode 100644 tests/Unit/FocusedCommitScroll.test.ts diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index cb89553085..f2bcd086d6 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -308,7 +308,7 @@ Sources: [link rendering](resources/js/components/linkified-text.tsx), [link-car ### Replies and threads -Reply to a visible commit to continue its thread. Replies use the same text limits, image support, moderation, and submission rate limit as commits. Threads expand inline, a few replies at a time: choose 1, 3, 5, 10, 15, 20, or 25 in Settings → Appearance (3 by default). Opening a commit's own page shows its ancestors and replies. Quotes of a commit are listed among its replies and counted with them. Profiles have a separate Replies tab for authored replies. A reply cannot also be a quote, contain a poll, or be scheduled. +Reply to a visible commit to continue its thread. Replies use the same text limits, image support, moderation, and submission rate limit as commits. Threads expand inline, a few replies at a time: choose 1, 3, 5, 10, 15, 20, or 25 in Settings → Appearance (3 by default). Opening a commit's own page shows its ancestors and replies, and scrolls to the commit itself so a deep reply opens on screen with its parent just above it; Back and Forward keep where you were. Quotes of a commit are listed among its replies and counted with them. Profiles have a separate Replies tab for authored replies. A reply cannot also be a quote, contain a poll, or be scheduled. When an author replies to their own commit, their earliest such reply continues it as a thread. The commit's page reads that thread in order, 10 commits at a time with **Show more of this thread**, each marked with its place, such as `3/7`. Elsewhere, a continued commit shows **Show thread**, which opens its page. The thread stops at the first reply you cannot see, and is hidden if you muted the author. diff --git a/resources/js/app.tsx b/resources/js/app.tsx index 21d407f4ed..e1b3152bfc 100644 --- a/resources/js/app.tsx +++ b/resources/js/app.tsx @@ -14,6 +14,7 @@ import { trackCommitPhotoPage } from '@/lib/commit-photo'; import { initializeCommitState } from '@/lib/commit-state'; import { currentPageComponent, trackCurrentPage } from '@/lib/current-page'; import { createEcho } from '@/lib/echo'; +import { scrollToFocusedCommit } from '@/lib/focused-commit-scroll'; import { trackFollowRequests } from '@/lib/follow-requests'; import { startLiteMobileApp } from '@/lib/lite-mobile-app'; import { pageTitle } from '@/lib/page-title'; @@ -34,6 +35,7 @@ trackCommitPhotoPage(); trackVisualViewport(); trackAnalyticsEvents(); trackPrefetchedPages(); +router.on('navigate', (event) => scrollToFocusedCommit(event.detail)); connectRealtime(createEcho()); // What a page shows was read once the visit bringing all of it started, and the first one once the browser asked for // it. A page brought back from history or a prefetch shows what was read back then, and catches up. diff --git a/resources/js/lib/focused-commit-scroll.ts b/resources/js/lib/focused-commit-scroll.ts new file mode 100644 index 0000000000..6eff98f9db --- /dev/null +++ b/resources/js/lib/focused-commit-scroll.ts @@ -0,0 +1,40 @@ +type Navigation = { + type: string; + page: { component: string; props: Record }; +}; + +/** + * A commit's page lists the thread above the commit, so a deep reply would open below the fold. Opening the page + * brings the commit under the header, with the end of its parent still showing above it. Back, Forward, a reload or an + * anchor in the URL keep the position Inertia or the browser gives them, and opening a photo leaves the page behind it + * where it was. + */ +export function scrollToFocusedCommit({ type, page }: Navigation): void { + const { commit, ancestors, photo } = page.props as { + commit: { id: string }; + ancestors?: unknown[]; + photo?: unknown; + }; + + if ( + page.component !== 'commit/show' || + !ancestors?.length || + photo || + window.location.hash || + !(type === 'visit' || (type === 'initial' && isFirstLoad())) + ) { + return; + } + + document + .querySelector(`[data-commit="${CSS.escape(commit.id)}"]`) + ?.scrollIntoView({ block: 'start' }); +} + +function isFirstLoad(): boolean { + const [entry] = performance.getEntriesByType( + 'navigation', + ) as PerformanceNavigationTiming[]; + + return entry?.type !== 'reload' && entry?.type !== 'back_forward'; +} diff --git a/tests/Unit/FocusedCommitScroll.test.ts b/tests/Unit/FocusedCommitScroll.test.ts new file mode 100644 index 0000000000..bcd9ebb530 --- /dev/null +++ b/tests/Unit/FocusedCommitScroll.test.ts @@ -0,0 +1,59 @@ +import assert from 'node:assert/strict'; +import { afterEach, test } from 'node:test'; +import { scrollToFocusedCommit } from '../../resources/js/lib/focused-commit-scroll.ts'; + +const originals = new Map(['window', 'document', 'CSS', 'performance'].map((name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)])); +afterEach(() => { + for (const [name, descriptor] of originals) { + if (descriptor) Object.defineProperty(globalThis, name, descriptor); + else Reflect.deleteProperty(globalThis, name); + } +}); + +function setup({ hash = '', load = 'navigate' } = {}) { + const scrolled: { selector: string; options: ScrollIntoViewOptions }[] = []; + const define = (name: string, value: unknown) => Object.defineProperty(globalThis, name, { configurable: true, value }); + define('window', { location: { hash } }); + define('CSS', { escape: (value: string) => value }); + define('performance', { getEntriesByType: () => [{ type: load }] }); + define('document', { + querySelector: (selector: string) => ({ + scrollIntoView: (options: ScrollIntoViewOptions) => scrolled.push({ selector, options }), + }), + }); + return scrolled; +} + +function navigation(type: string, props: Record = {}, component = 'commit/show') { + return { type, page: { component, props: { commit: { id: 'reply' }, ancestors: [{ id: 'root' }], ...props } } }; +} + +void test('brings the commit below its ancestors into view when its page opens', () => { + for (const type of ['visit', 'initial']) { + const scrolled = setup(); + scrollToFocusedCommit(navigation(type)); + assert.deepEqual(scrolled, [{ selector: '[data-commit="reply"]', options: { block: 'start' } }]); + } +}); + +void test('keeps the restored position after Back, Forward or a reload', () => { + for (const [type, load] of [['history', 'navigate'], ['initial', 'reload'], ['initial', 'back_forward']]) { + const scrolled = setup({ load }); + scrollToFocusedCommit(navigation(type)); + assert.deepEqual(scrolled, []); + } +}); + +void test('leaves the position alone without ancestors, with an anchor or a photo, or on other pages', () => { + const cases: [ReturnType, string][] = [ + [navigation('visit', { ancestors: [] }), ''], + [navigation('visit', { photo: { index: 0 } }), ''], + [navigation('visit'), '#replies'], + [navigation('visit', {}, 'log/show'), ''], + ]; + for (const [event, hash] of cases) { + const scrolled = setup({ hash }); + scrollToFocusedCommit(event); + assert.deepEqual(scrolled, []); + } +});