@jant/core 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{app--uxnaIRV.js → app-BzH8jmh3.js} +1199 -262
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-BT6kpcuG.js → client-S8oryJ0m.js} +1 -1
- package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-Ds9G0IgR.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +1 -1
- package/src/__tests__/helpers/cursor-walk.ts +37 -7
- package/src/app.tsx +4 -0
- package/src/lib/__tests__/thread-fold.test.ts +43 -0
- package/src/lib/api-posts.ts +110 -2
- package/src/lib/api-public-posts.ts +263 -0
- package/src/lib/api-threads.ts +207 -0
- package/src/lib/thread-fold.ts +83 -41
- package/src/lib/thread-query.ts +143 -0
- package/src/middleware/cors.ts +8 -1
- package/src/middleware/deprecation.ts +48 -0
- package/src/routes/api/__tests__/mcp.test.ts +89 -0
- package/src/routes/api/__tests__/posts.test.ts +78 -0
- package/src/routes/api/__tests__/public-access.test.ts +5 -0
- package/src/routes/api/__tests__/threads.test.ts +115 -0
- package/src/routes/api/posts.ts +15 -104
- package/src/routes/api/public/__tests__/threads.test.ts +363 -0
- package/src/routes/api/public/archive.ts +82 -81
- package/src/routes/api/public/posts.ts +97 -298
- package/src/routes/api/public/threads.ts +141 -0
- package/src/routes/api/threads.ts +128 -0
- package/src/services/__tests__/thread.test.ts +495 -0
- package/src/services/index.ts +4 -0
- package/src/services/mcp.ts +208 -53
- package/src/services/post.ts +222 -43
- package/src/services/thread.ts +392 -0
- package/src/types/constants.ts +25 -0
- package/src/ui/feed/ThreadPreview.tsx +9 -14
- package/src/ui/feed/__tests__/thread-preview.test.ts +18 -22
- package/src/ui/feed/thread-preview-state.ts +0 -35
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thread Service
|
|
3
|
+
*
|
|
4
|
+
* Threads as a resource of their own: a list of Thread roots in one of the
|
|
5
|
+
* orders the site shows, each with its Post count and, on request, the fold the
|
|
6
|
+
* homepage draws; one Thread by any of its Posts; and a Thread's Posts in Thread
|
|
7
|
+
* order.
|
|
8
|
+
*
|
|
9
|
+
* Every read goes through `PostService`. What lives here is the choice of read:
|
|
10
|
+
* which order a request names, when a collection's own order and pins apply,
|
|
11
|
+
* and what a reader may see as opposed to the author.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
resolveCollectionSortOrder,
|
|
16
|
+
supportsCollectionRatingSort,
|
|
17
|
+
} from "../lib/collection-sort.js";
|
|
18
|
+
import { ValidationError } from "../lib/errors.js";
|
|
19
|
+
import {
|
|
20
|
+
toPostFilters,
|
|
21
|
+
type PostFilterSelection,
|
|
22
|
+
} from "../lib/filter-dimensions.js";
|
|
23
|
+
import { foldReplyWindows, type ThreadFold } from "../lib/thread-fold.js";
|
|
24
|
+
import type {
|
|
25
|
+
CollectionSortOrder,
|
|
26
|
+
Post,
|
|
27
|
+
Status,
|
|
28
|
+
ThreadSort,
|
|
29
|
+
} from "../types.js";
|
|
30
|
+
import type { CollectionService } from "./collection.js";
|
|
31
|
+
import type {
|
|
32
|
+
PostFilters,
|
|
33
|
+
PostListPageOptions,
|
|
34
|
+
PostListPage,
|
|
35
|
+
PostService,
|
|
36
|
+
ThreadRootPageOptions,
|
|
37
|
+
} from "./post.js";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Who is asking. A reader sees what the site shows anyone: published Threads
|
|
41
|
+
* that aren't private. The author sees every Thread.
|
|
42
|
+
*/
|
|
43
|
+
export type ThreadAudience = "reader" | "author";
|
|
44
|
+
|
|
45
|
+
/** What a Thread list is asked for. */
|
|
46
|
+
export interface ThreadListQuery {
|
|
47
|
+
audience: ThreadAudience;
|
|
48
|
+
/** The author's status filter; a reader always sees `published`. */
|
|
49
|
+
status?: Status;
|
|
50
|
+
/** Filter dimensions, with collection slugs already resolved to IDs. */
|
|
51
|
+
selection: PostFilterSelection;
|
|
52
|
+
/**
|
|
53
|
+
* A reader's list leaves out Threads hidden from Latest, as the homepage
|
|
54
|
+
* does, unless this is set. A visibility in `selection` is the whole
|
|
55
|
+
* visibility filter and overrides both.
|
|
56
|
+
*/
|
|
57
|
+
includeHidden?: boolean;
|
|
58
|
+
/** One content language, as a canonical BCP 47 tag. */
|
|
59
|
+
lang?: string;
|
|
60
|
+
/** Defaults to `activity`, or a named collection's own order. */
|
|
61
|
+
sort?: ThreadSort;
|
|
62
|
+
/** Attach the homepage fold to each Thread. */
|
|
63
|
+
fold?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One Thread, as a list or a lookup returns it. */
|
|
67
|
+
export interface ThreadSummary {
|
|
68
|
+
root: Post;
|
|
69
|
+
/** Published Posts in the Thread, the root included. */
|
|
70
|
+
postCount: number;
|
|
71
|
+
/** Present when asked for; null when the Thread has no published reply. */
|
|
72
|
+
fold?: ThreadFold<Post> | null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One page of Threads, and the opaque cursor that continues it. */
|
|
76
|
+
export interface ThreadListPage {
|
|
77
|
+
threads: ThreadSummary[];
|
|
78
|
+
nextCursor: string | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** How a Thread is named: by a Post ID, or by a Post's slug. */
|
|
82
|
+
export type ThreadRef = { id: string } | { slug: string };
|
|
83
|
+
|
|
84
|
+
export interface ThreadService {
|
|
85
|
+
/**
|
|
86
|
+
* One page of Thread roots, in the order `query.sort` names.
|
|
87
|
+
*
|
|
88
|
+
* Without a `sort`, a single named collection lists in its own configured
|
|
89
|
+
* order and an aggregate of several in activity order; `activity`,
|
|
90
|
+
* `oldest`, and `rating` on a named collection also use the collection's
|
|
91
|
+
* pins, as its page does. `published` and `updated` treat a collection as a
|
|
92
|
+
* plain filter, as the archive does.
|
|
93
|
+
*
|
|
94
|
+
* @param query - What to list, for whom, and in which order
|
|
95
|
+
* @param page - The previous page's `nextCursor`, and the page size
|
|
96
|
+
* @returns The page's Threads and the cursor after them
|
|
97
|
+
* @throws {ValidationError} When the cursor can't be resumed, or when
|
|
98
|
+
* `visibility` is `featured` and `sort` names anything but `published`
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const page = await threads.listThreads(
|
|
102
|
+
* { audience: "reader", selection: {}, fold: true },
|
|
103
|
+
* { limit: 20 },
|
|
104
|
+
* );
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
listThreads(
|
|
108
|
+
query: ThreadListQuery,
|
|
109
|
+
page: PostListPageOptions,
|
|
110
|
+
): Promise<ThreadListPage>;
|
|
111
|
+
/**
|
|
112
|
+
* The root of the Thread a Post belongs to, when `audience` may see it.
|
|
113
|
+
*
|
|
114
|
+
* A reader is answered only for a published Post in a published Thread that
|
|
115
|
+
* isn't private; a Thread hidden from Latest is still found.
|
|
116
|
+
*
|
|
117
|
+
* @param ref - Any Post in the Thread, by ID or by slug
|
|
118
|
+
* @param audience - Who is asking
|
|
119
|
+
* @returns The Thread's root, or null when there is none to show
|
|
120
|
+
* @example
|
|
121
|
+
* const root = await threads.findRoot({ slug: "a-reply" }, "reader");
|
|
122
|
+
*/
|
|
123
|
+
findRoot(ref: ThreadRef, audience: ThreadAudience): Promise<Post | null>;
|
|
124
|
+
/**
|
|
125
|
+
* Post counts, and the fold on request, for Thread roots already in hand.
|
|
126
|
+
*
|
|
127
|
+
* @param roots - Thread roots, in the order to return them
|
|
128
|
+
* @param options - `fold` attaches the homepage fold
|
|
129
|
+
* @returns One summary per root, in the same order
|
|
130
|
+
* @example
|
|
131
|
+
* const [summary] = await threads.summarize([root], { fold: true });
|
|
132
|
+
*/
|
|
133
|
+
summarize(
|
|
134
|
+
roots: Post[],
|
|
135
|
+
options: { fold?: boolean },
|
|
136
|
+
): Promise<ThreadSummary[]>;
|
|
137
|
+
/**
|
|
138
|
+
* One page of a Thread's Posts, in Thread order. A reader gets published
|
|
139
|
+
* Posts only; the author's `status` defaults to `published`.
|
|
140
|
+
*
|
|
141
|
+
* @param rootId - The Thread root's ID, as {@link findRoot} returned it
|
|
142
|
+
* @param query - Who is asking, and the author's status filter
|
|
143
|
+
* @param page - The previous page's `nextCursor` or a Post ID, and the size
|
|
144
|
+
* @returns The page's Posts and the cursor after them
|
|
145
|
+
* @example
|
|
146
|
+
* await threads.listPosts(root.id, { audience: "reader" }, { limit: 100 });
|
|
147
|
+
*/
|
|
148
|
+
listPosts(
|
|
149
|
+
rootId: string,
|
|
150
|
+
query: { audience: ThreadAudience; status?: Status },
|
|
151
|
+
page: PostListPageOptions,
|
|
152
|
+
): Promise<PostListPage>;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const FEATURED_SORT_MESSAGE =
|
|
156
|
+
"Featured posts are listed newest-published first. Leave sort out, or pass sort=published.";
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* How a plain Thread list orders each `ThreadSort`, as `PostFilters` fields.
|
|
160
|
+
* Only `activity`, the homepage's order, puts pinned Threads first.
|
|
161
|
+
*/
|
|
162
|
+
function toListOrder(
|
|
163
|
+
sort: ThreadSort,
|
|
164
|
+
): Pick<PostFilters, "sortBy" | "sortOrder" | "ignorePinnedSort"> {
|
|
165
|
+
switch (sort) {
|
|
166
|
+
case "activity":
|
|
167
|
+
return { sortBy: "activity", sortOrder: "newest" };
|
|
168
|
+
case "published":
|
|
169
|
+
return {
|
|
170
|
+
sortBy: "published",
|
|
171
|
+
sortOrder: "newest",
|
|
172
|
+
ignorePinnedSort: true,
|
|
173
|
+
};
|
|
174
|
+
case "updated":
|
|
175
|
+
return {
|
|
176
|
+
sortBy: "thread_updated",
|
|
177
|
+
sortOrder: "newest",
|
|
178
|
+
ignorePinnedSort: true,
|
|
179
|
+
};
|
|
180
|
+
case "oldest":
|
|
181
|
+
return {
|
|
182
|
+
sortBy: "published",
|
|
183
|
+
sortOrder: "oldest",
|
|
184
|
+
ignorePinnedSort: true,
|
|
185
|
+
};
|
|
186
|
+
case "rating":
|
|
187
|
+
return {
|
|
188
|
+
sortBy: "activity",
|
|
189
|
+
sortOrder: "rating_desc",
|
|
190
|
+
ignorePinnedSort: true,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The collection order a `ThreadSort` names, or null for the two that read a
|
|
197
|
+
* collection as a plain filter.
|
|
198
|
+
*/
|
|
199
|
+
function toCollectionOrder(sort: ThreadSort): CollectionSortOrder | null {
|
|
200
|
+
switch (sort) {
|
|
201
|
+
case "activity":
|
|
202
|
+
return "newest";
|
|
203
|
+
case "oldest":
|
|
204
|
+
return "oldest";
|
|
205
|
+
case "rating":
|
|
206
|
+
return "rating_desc";
|
|
207
|
+
case "published":
|
|
208
|
+
case "updated":
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Create the Thread service.
|
|
215
|
+
*
|
|
216
|
+
* @param deps - The Post and collection services it reads through
|
|
217
|
+
* @returns The Thread service
|
|
218
|
+
* @example
|
|
219
|
+
* const threads = createThreadService({ posts, collections });
|
|
220
|
+
*/
|
|
221
|
+
export function createThreadService(deps: {
|
|
222
|
+
posts: PostService;
|
|
223
|
+
collections: CollectionService;
|
|
224
|
+
}): ThreadService {
|
|
225
|
+
const { posts, collections } = deps;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The order a collection's page would use: its configured order when it is
|
|
229
|
+
* the only one named, activity for several. A rating order with fewer than
|
|
230
|
+
* two rated Threads falls back, as the page does — for the default only; a
|
|
231
|
+
* caller who asks for `rating` gets it.
|
|
232
|
+
*/
|
|
233
|
+
async function resolveDefaultCollectionOrder(
|
|
234
|
+
collectionIds: readonly string[],
|
|
235
|
+
options: ThreadRootPageOptions,
|
|
236
|
+
): Promise<CollectionSortOrder> {
|
|
237
|
+
const [onlyId, ...others] = collectionIds;
|
|
238
|
+
if (!onlyId || others.length > 0) return "newest";
|
|
239
|
+
|
|
240
|
+
const collection = await collections.getById(onlyId);
|
|
241
|
+
if (!collection) return "newest";
|
|
242
|
+
|
|
243
|
+
const ratedThreadCount =
|
|
244
|
+
await posts.countCollectionThreadRootsUpToForCollections(
|
|
245
|
+
[onlyId],
|
|
246
|
+
{ ...options, hasRating: true },
|
|
247
|
+
2,
|
|
248
|
+
);
|
|
249
|
+
return resolveCollectionSortOrder(
|
|
250
|
+
undefined,
|
|
251
|
+
collection.sortOrder,
|
|
252
|
+
supportsCollectionRatingSort(ratedThreadCount),
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const service: ThreadService = {
|
|
257
|
+
async listThreads(query, page) {
|
|
258
|
+
const reader = query.audience === "reader";
|
|
259
|
+
const { selection } = query;
|
|
260
|
+
const visibility = selection.visibility;
|
|
261
|
+
if (reader && visibility === "private") {
|
|
262
|
+
throw new ValidationError("Private posts aren't listed here.");
|
|
263
|
+
}
|
|
264
|
+
if (
|
|
265
|
+
visibility === "featured" &&
|
|
266
|
+
query.sort !== undefined &&
|
|
267
|
+
query.sort !== "published"
|
|
268
|
+
) {
|
|
269
|
+
throw new ValidationError(FEATURED_SORT_MESSAGE);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const base = {
|
|
273
|
+
status: reader ? ("published" as const) : (query.status ?? "published"),
|
|
274
|
+
excludePrivate: reader,
|
|
275
|
+
excludeLatestHidden:
|
|
276
|
+
reader && visibility === undefined && !query.includeHidden,
|
|
277
|
+
lang: query.lang,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const collectionIds = selection.collection;
|
|
281
|
+
if (collectionIds && visibility !== "featured") {
|
|
282
|
+
// Membership is the collection query's own; the other dimensions
|
|
283
|
+
// narrow which roots it may return.
|
|
284
|
+
const { collection: _named, ...rootSelection } = selection;
|
|
285
|
+
const options: ThreadRootPageOptions = {
|
|
286
|
+
...base,
|
|
287
|
+
rootFilters: toPostFilters(rootSelection, { yearAxis: "published" }),
|
|
288
|
+
};
|
|
289
|
+
const collectionOrder =
|
|
290
|
+
query.sort === undefined
|
|
291
|
+
? await resolveDefaultCollectionOrder(collectionIds, options)
|
|
292
|
+
: toCollectionOrder(query.sort);
|
|
293
|
+
|
|
294
|
+
if (collectionOrder) {
|
|
295
|
+
const result = await posts.listCollectionThreadRootPage(
|
|
296
|
+
[...collectionIds],
|
|
297
|
+
{ ...options, sortOrder: collectionOrder },
|
|
298
|
+
page,
|
|
299
|
+
);
|
|
300
|
+
return {
|
|
301
|
+
threads: await service.summarize(result.posts, query),
|
|
302
|
+
nextCursor: result.nextCursor,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const result = await posts.listPage(
|
|
308
|
+
{
|
|
309
|
+
...base,
|
|
310
|
+
excludeReplies: true,
|
|
311
|
+
// Membership never depends on presentation: the year reads the
|
|
312
|
+
// publication date whatever the order.
|
|
313
|
+
...toPostFilters(selection, { yearAxis: "published" }),
|
|
314
|
+
...(visibility === "featured"
|
|
315
|
+
? toListOrder("published")
|
|
316
|
+
: toListOrder(query.sort ?? "activity")),
|
|
317
|
+
},
|
|
318
|
+
page,
|
|
319
|
+
);
|
|
320
|
+
return {
|
|
321
|
+
threads: await service.summarize(result.posts, query),
|
|
322
|
+
nextCursor: result.nextCursor,
|
|
323
|
+
};
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
async findRoot(ref, audience) {
|
|
327
|
+
const post =
|
|
328
|
+
"id" in ref
|
|
329
|
+
? await posts.getById(ref.id)
|
|
330
|
+
: await posts.getBySlug(ref.slug);
|
|
331
|
+
if (!post) return null;
|
|
332
|
+
if (audience === "reader" && post.status !== "published") return null;
|
|
333
|
+
|
|
334
|
+
const root =
|
|
335
|
+
post.threadId === post.id ? post : await posts.getById(post.threadId);
|
|
336
|
+
if (!root) return null;
|
|
337
|
+
if (
|
|
338
|
+
audience === "reader" &&
|
|
339
|
+
(root.status !== "published" || root.visibility === "private")
|
|
340
|
+
) {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
return root;
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
async summarize(roots, options) {
|
|
347
|
+
if (roots.length === 0) return [];
|
|
348
|
+
const rootIds = roots.map((root) => root.id);
|
|
349
|
+
const [counts, contexts] = await Promise.all([
|
|
350
|
+
posts.countThreadPosts(rootIds),
|
|
351
|
+
options.fold
|
|
352
|
+
? posts.getThreadTimelineContext(rootIds)
|
|
353
|
+
: Promise.resolve(null),
|
|
354
|
+
]);
|
|
355
|
+
|
|
356
|
+
return roots.map((root) => {
|
|
357
|
+
const summary: ThreadSummary = {
|
|
358
|
+
root,
|
|
359
|
+
postCount: counts.get(root.id) ?? 0,
|
|
360
|
+
};
|
|
361
|
+
if (contexts) {
|
|
362
|
+
const context = contexts.get(root.id);
|
|
363
|
+
summary.fold = context
|
|
364
|
+
? foldReplyWindows({
|
|
365
|
+
leadingReplies: context.leadingReplies,
|
|
366
|
+
trailingReplies: context.trailingReplies,
|
|
367
|
+
latestReply: context.latestReply,
|
|
368
|
+
nextReply: context.firstHiddenReply,
|
|
369
|
+
totalReplyCount: context.totalReplyCount,
|
|
370
|
+
})
|
|
371
|
+
: null;
|
|
372
|
+
}
|
|
373
|
+
return summary;
|
|
374
|
+
});
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
async listPosts(rootId, query, page) {
|
|
378
|
+
return posts.listThreadPostsPage(
|
|
379
|
+
rootId,
|
|
380
|
+
{
|
|
381
|
+
status:
|
|
382
|
+
query.audience === "reader"
|
|
383
|
+
? "published"
|
|
384
|
+
: (query.status ?? "published"),
|
|
385
|
+
},
|
|
386
|
+
page,
|
|
387
|
+
);
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
return service;
|
|
392
|
+
}
|
package/src/types/constants.ts
CHANGED
|
@@ -103,6 +103,31 @@ export type CollectionSortOrder = SortOrder;
|
|
|
103
103
|
export const SMART_COLLECTION_SORT_ORDERS = COLLECTION_SORT_ORDERS;
|
|
104
104
|
export type SmartCollectionSortOrder = CollectionSortOrder;
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* How the Threads API orders a list of Threads — each value one the site
|
|
108
|
+
* already shows somewhere, named for the axis it reads:
|
|
109
|
+
*
|
|
110
|
+
* - `activity`: the homepage, and a collection's `newest`. A Thread moves up
|
|
111
|
+
* when it gains an announced post.
|
|
112
|
+
* - `published`: the archive's default. Newest root publication first; a reply
|
|
113
|
+
* never moves a Thread, so this is the order to walk a whole site in.
|
|
114
|
+
* - `updated`: the archive's `?sort=updated`. Like `activity`, but a quiet
|
|
115
|
+
* reply counts too.
|
|
116
|
+
* - `oldest`: a collection's `oldest`. Oldest root publication first.
|
|
117
|
+
* - `rating`: a collection's `rating_desc`. Highest rating first, then activity.
|
|
118
|
+
*
|
|
119
|
+
* Not `SORT_ORDERS`: `newest` means activity on a collection and would read as
|
|
120
|
+
* publication here.
|
|
121
|
+
*/
|
|
122
|
+
export const THREAD_SORTS = [
|
|
123
|
+
"activity",
|
|
124
|
+
"published",
|
|
125
|
+
"updated",
|
|
126
|
+
"oldest",
|
|
127
|
+
"rating",
|
|
128
|
+
] as const;
|
|
129
|
+
export type ThreadSort = (typeof THREAD_SORTS)[number];
|
|
130
|
+
|
|
106
131
|
export const NAV_ITEM_TYPES = [
|
|
107
132
|
"link",
|
|
108
133
|
"system",
|
|
@@ -11,10 +11,8 @@ import { useLingui } from "../../i18n/context.js";
|
|
|
11
11
|
import type { ThreadPreviewProps } from "../../types.js";
|
|
12
12
|
import { TimelineItem } from "./TimelineItem.js";
|
|
13
13
|
import { TimelineItemFromPost } from "./TimelineItem.js";
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
threadContextAssumesOverflow,
|
|
17
|
-
} from "./thread-preview-state.js";
|
|
14
|
+
import { foldReplyWindows } from "../../lib/thread-fold.js";
|
|
15
|
+
import { threadContextAssumesOverflow } from "./thread-preview-state.js";
|
|
18
16
|
|
|
19
17
|
const ROOT_CONTEXT_DISPLAY = {
|
|
20
18
|
footer: {
|
|
@@ -40,12 +38,16 @@ export const ThreadPreview: FC<ThreadPreviewProps> = ({
|
|
|
40
38
|
totalReplyCount,
|
|
41
39
|
}) => {
|
|
42
40
|
const { i18n } = useLingui();
|
|
43
|
-
|
|
41
|
+
// The gap's target arrives resolved as `gapHref`, so the fold is asked only
|
|
42
|
+
// what it shows and how much it hides.
|
|
43
|
+
const fold = foldReplyWindows({
|
|
44
44
|
leadingReplies,
|
|
45
45
|
trailingReplies,
|
|
46
46
|
latestReply,
|
|
47
|
+
nextReply: null,
|
|
47
48
|
totalReplyCount,
|
|
48
49
|
});
|
|
50
|
+
const hiddenCount = fold.hiddenCount;
|
|
49
51
|
const assumeOverflow = threadContextAssumesOverflow({
|
|
50
52
|
rootPost,
|
|
51
53
|
totalReplyCount,
|
|
@@ -73,15 +75,8 @@ export const ThreadPreview: FC<ThreadPreviewProps> = ({
|
|
|
73
75
|
"@context: Collapse expanded thread ancestor context in the feed",
|
|
74
76
|
}),
|
|
75
77
|
);
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
replies.filter((reply) => {
|
|
79
|
-
if (visibleReplyIds.has(reply.id)) return false;
|
|
80
|
-
visibleReplyIds.add(reply.id);
|
|
81
|
-
return true;
|
|
82
|
-
});
|
|
83
|
-
const renderedLeadingReplies = dedupeReplies(leadingReplies);
|
|
84
|
-
const renderedTrailingReplies = dedupeReplies(trailingReplies);
|
|
78
|
+
const renderedLeadingReplies = fold.leadingReplies;
|
|
79
|
+
const renderedTrailingReplies = fold.trailingReplies;
|
|
85
80
|
// The gap opens the first post it hides — the rule in `lib/thread-fold.ts`,
|
|
86
81
|
// which the feed follows too. The fallback only covers a gap target that
|
|
87
82
|
// went unpublished between the count and the fetch.
|
|
@@ -7,10 +7,8 @@ import { createI18n } from "../../../i18n/i18n.js";
|
|
|
7
7
|
import type { PostView, TimelineItemView } from "../../../types.js";
|
|
8
8
|
import { CuratedThreadPreview } from "../CuratedThreadPreview.js";
|
|
9
9
|
import { ThreadPreview } from "../ThreadPreview.js";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
threadContextAssumesOverflow,
|
|
13
|
-
} from "../thread-preview-state.js";
|
|
10
|
+
import { foldReplyWindows } from "../../../lib/thread-fold.js";
|
|
11
|
+
import { threadContextAssumesOverflow } from "../thread-preview-state.js";
|
|
14
12
|
|
|
15
13
|
function createPostView(overrides: Partial<PostView> = {}): PostView {
|
|
16
14
|
return {
|
|
@@ -57,7 +55,7 @@ function toggleTag(html: string): string {
|
|
|
57
55
|
);
|
|
58
56
|
}
|
|
59
57
|
|
|
60
|
-
describe("
|
|
58
|
+
describe("thread preview fold", () => {
|
|
61
59
|
it("has no hidden posts for a 2-post thread", () => {
|
|
62
60
|
const latestReply = createPostView({
|
|
63
61
|
id: "post-2",
|
|
@@ -65,16 +63,16 @@ describe("getThreadPreviewState", () => {
|
|
|
65
63
|
slug: "post-2",
|
|
66
64
|
});
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}),
|
|
75
|
-
).toEqual({
|
|
76
|
-
hiddenCount: 0,
|
|
66
|
+
const fold = foldReplyWindows({
|
|
67
|
+
leadingReplies: [latestReply],
|
|
68
|
+
trailingReplies: [],
|
|
69
|
+
latestReply,
|
|
70
|
+
nextReply: null,
|
|
71
|
+
totalReplyCount: 1,
|
|
77
72
|
});
|
|
73
|
+
expect(fold.hiddenCount).toBe(0);
|
|
74
|
+
// The only reply is the hero, so the leading window gives it up.
|
|
75
|
+
expect(fold.leadingReplies).toEqual([]);
|
|
78
76
|
});
|
|
79
77
|
|
|
80
78
|
it("has no hidden posts for a 6-post thread when all slots are visible", () => {
|
|
@@ -105,15 +103,14 @@ describe("getThreadPreviewState", () => {
|
|
|
105
103
|
});
|
|
106
104
|
|
|
107
105
|
expect(
|
|
108
|
-
|
|
106
|
+
foldReplyWindows({
|
|
109
107
|
leadingReplies: [firstReply, secondReply],
|
|
110
108
|
trailingReplies: [antepenultimateReply, penultimateReply],
|
|
111
109
|
latestReply,
|
|
110
|
+
nextReply: antepenultimateReply,
|
|
112
111
|
totalReplyCount: 5,
|
|
113
112
|
}),
|
|
114
|
-
).
|
|
115
|
-
hiddenCount: 0,
|
|
116
|
-
});
|
|
113
|
+
).toMatchObject({ hiddenCount: 0, firstHiddenReply: null });
|
|
117
114
|
});
|
|
118
115
|
|
|
119
116
|
it("counts hidden posts for longer threads after deduping visible slots", () => {
|
|
@@ -144,15 +141,14 @@ describe("getThreadPreviewState", () => {
|
|
|
144
141
|
});
|
|
145
142
|
|
|
146
143
|
expect(
|
|
147
|
-
|
|
144
|
+
foldReplyWindows({
|
|
148
145
|
leadingReplies: [firstReply, secondReply],
|
|
149
146
|
trailingReplies: [antepenultimateReply, penultimateReply],
|
|
150
147
|
latestReply,
|
|
148
|
+
nextReply: createPostView({ id: "post-4" }),
|
|
151
149
|
totalReplyCount: 8,
|
|
152
150
|
}),
|
|
153
|
-
).
|
|
154
|
-
hiddenCount: 3,
|
|
155
|
-
});
|
|
151
|
+
).toMatchObject({ hiddenCount: 3, firstHiddenReply: { id: "post-4" } });
|
|
156
152
|
});
|
|
157
153
|
|
|
158
154
|
it("keeps thread preview items shrinkable within the grid track", () => {
|
|
@@ -1,39 +1,4 @@
|
|
|
1
1
|
import type { PostView } from "../../types.js";
|
|
2
|
-
import { getThreadHiddenCount } from "../../lib/thread-fold.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* What the preview needs to know about its own fold.
|
|
6
|
-
*
|
|
7
|
-
* The count itself lives in `lib/thread-fold.ts`, where the feed reads it too —
|
|
8
|
-
* a card and a feed entry that disagreed about how many posts are hidden would
|
|
9
|
-
* be describing different threads.
|
|
10
|
-
*
|
|
11
|
-
* @param selection - The replies this preview shows, and the thread's total
|
|
12
|
-
* @returns The hidden-post count the gap link announces
|
|
13
|
-
* @example
|
|
14
|
-
* getThreadPreviewState({ leadingReplies, trailingReplies, latestReply,
|
|
15
|
-
* totalReplyCount: 8 }); // { hiddenCount: 3 }
|
|
16
|
-
*/
|
|
17
|
-
export function getThreadPreviewState({
|
|
18
|
-
leadingReplies,
|
|
19
|
-
trailingReplies,
|
|
20
|
-
latestReply,
|
|
21
|
-
totalReplyCount,
|
|
22
|
-
}: {
|
|
23
|
-
leadingReplies: PostView[];
|
|
24
|
-
trailingReplies: PostView[];
|
|
25
|
-
latestReply: PostView;
|
|
26
|
-
totalReplyCount: number;
|
|
27
|
-
}) {
|
|
28
|
-
return {
|
|
29
|
-
hiddenCount: getThreadHiddenCount({
|
|
30
|
-
leadingReplies,
|
|
31
|
-
trailingReplies,
|
|
32
|
-
latestReply,
|
|
33
|
-
totalReplyCount,
|
|
34
|
-
}),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
2
|
|
|
38
3
|
/**
|
|
39
4
|
* A lone root post shorter than this (plain-text code points) comfortably
|