Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,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.

Expand Down
2 changes: 2 additions & 0 deletions resources/js/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions resources/js/lib/focused-commit-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type Navigation = {
type: string;
page: { component: string; props: Record<string, unknown> };
};

/**
* 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';
}
59 changes: 59 additions & 0 deletions tests/Unit/FocusedCommitScroll.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {}, 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<typeof navigation>, 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, []);
}
});