Open History to see the last commit
1"use client";
2import { useEffect, useRef, useState } from "react";
3import { News } from "../../news-client";
4import { Discussion } from "../../item/comments-client";
5import type { Story } from "@/lib/types";
6import type { Comment } from "@/lib/comments";
7import type { HnEvent } from "@/lib/events";
8const initialComments: Comment[] = [
9 {
10 story_id: 1000,
11 item_id: 501,
12 parent_id: 1000,
13 path: "000001",
14 depth: 0,
15 author: "alice",
16 created: 1791806400,
17 text: 'A comment with <i>formatting</i> & a <a href="https://neon.com">link</a>.',
18 deleted: false,
19 dead: false,
20 committed_at: "2026-10-12T08:00:00.000Z",
21 },
22 {
23 story_id: 1000,
24 item_id: 502,
25 parent_id: 501,
26 path: "000001.000001",
27 depth: 1,
28 author: "bob",
29 created: 1791806500,
30 text: "A nested reply.<p>With a second paragraph.</p>",
31 deleted: false,
32 dead: false,
33 committed_at: "2026-10-12T08:00:00.000Z",
34 },
35 {
36 story_id: 1000,
37 item_id: 503,
38 parent_id: 1000,
39 path: "000002",
40 depth: 0,
41 author: "carol",
42 created: 1791806600,
43 text: "Another top-level comment.",
44 deleted: false,
45 dead: false,
46 committed_at: "2026-10-12T08:00:00.000Z",
47 },
48];
49export function UpdateFixture({
50 initialStories,
51 initialNow,
52}: {
53 initialStories: Story[];
54 initialNow: number;
55}) {
56 const [view, setView] = useState("stories");
57 const [stories, setStories] = useState(initialStories);
58 const [comments, setComments] = useState(initialComments);
59 const [events, setEvents] = useState<HnEvent[]>(() => [
60 ...initialStories.map((story, index) => ({
61 event_id: index + 1,
62 item_id: story.item_id,
63 story_id: story.item_id,
64 kind: "story-updated",
65 label: story.title,
66 details: '{"score":{"from":"1","to":"2"}}',
67 occurred_at: initialNow - 60000 + index * 1500,
68 })),
69 {
70 event_id: 100,
71 item_id: 99999,
72 story_id: 99999,
73 kind: "new-left",
74 label: "An item on another page",
75 details: '{"rank":150}',
76 occurred_at: initialNow - 8000,
77 },
78 {
79 event_id: 101,
80 item_id: 501,
81 story_id: 1000,
82 kind: "comment-updated",
83 label: "Comment by alice",
84 details: '{"text":{"from":"old","to":"new"}}',
85 occurred_at: initialNow - 5000,
86 },
87 ]);
88 const sequence = useRef(1000);
89 const previous = useRef<{ stories: Story[]; comments: Comment[] }>({
90 stories,
91 comments,
92 });
93 useEffect(() => {
94 const added: HnEvent[] = [];
95 for (const type of ["stories", "comments"] as const) {
96 const old = new Map(
97 previous.current[type].map((row) => [row.item_id, row]),
98 );
99 const current = type === "stories" ? stories : comments;
100 for (const row of current) {
101 const prior = old.get(row.item_id);
102 const delta: Record<string, unknown> = {};
103 if (prior)
104 for (const field of Object.keys(row)) {
105 const before = (prior as unknown as Record<string, unknown>)[field];
106 const after = (row as unknown as Record<string, unknown>)[field];
107 if (field !== "committed_at" && before !== after)
108 delta[field] = { from: before, to: after };
109 }
110 if (!prior || Object.keys(delta).length)
111 added.push({
112 event_id: sequence.current++,
113 item_id: row.item_id,
114 story_id: "story_id" in row ? row.story_id : row.item_id,
115 kind: `${type === "stories" ? "story" : "comment"}-${prior ? "updated" : "added"}`,
116 label: "title" in row ? row.title : `Comment by ${row.author}`,
117 details: JSON.stringify(delta),
118 occurred_at: Date.now(),
119 });
120 old.delete(row.item_id);
121 }
122 for (const row of old.values())
123 added.push({
124 event_id: sequence.current++,
125 item_id: row.item_id,
126 story_id: "story_id" in row ? row.story_id : row.item_id,
127 kind: `${type === "stories" ? "story" : "comment"}-removed`,
128 label: "title" in row ? row.title : `Comment by ${row.author}`,
129 details: "{}",
130 occurred_at: Date.now(),
131 });
132 }
133 previous.current = { stories, comments };
134 if (added.length) setEvents((rows) => [...rows, ...added]);
135 }, [stories, comments]);
136 return (
137 <>
138 <div
139 style={{ padding: 12, display: "flex", gap: 8, flexWrap: "wrap" }}
140 aria-label="Development update fixtures"
141 >
142 <button
143 onClick={() => setView(view === "stories" ? "comments" : "stories")}
144 >
145 {view === "stories" ? "Test comments" : "Test stories"}
146 </button>
147 <button
148 onClick={() =>
149 setStories((rows) =>
150 rows.map((row, i) =>
151 i === 0
152 ? {
153 ...row,
154 score: row.score + 1,
155 committed_at: new Date().toISOString(),
156 }
157 : row,
158 ),
159 )
160 }
161 >
162 Change score
163 </button>
164 <button
165 onClick={() =>
166 setStories((rows) =>
167 rows.map((row, i) =>
168 i === 0
169 ? {
170 ...row,
171 comments: row.comments + 1,
172 committed_at: new Date().toISOString(),
173 }
174 : row,
175 ),
176 )
177 }
178 >
179 Change count
180 </button>
181 <button
182 onClick={() => {
183 if (view === "stories")
184 setStories((rows) => [
185 {
186 ...initialStories[0],
187 item_id: 9000 + rows.length,
188 rank: 1,
189 title:
190 "A newly arriving story with enough text to wrap onto a second line on a narrow screen",
191 committed_at: new Date().toISOString(),
192 },
193 ...rows.map((row) => ({ ...row, rank: row.rank + 1 })),
194 ]);
195 else
196 setComments((rows) => [
197 ...rows.map((row) => ({
198 ...row,
199 path: row.path.replace(/^[0-9]{6}/, (prefix) =>
200 String(Number(prefix) + 1).padStart(6, "0"),
201 ),
202 })),
203 {
204 ...initialComments[0],
205 item_id: 9000 + rows.length,
206 path: "000001",
207 text: "A new comment that expands into the thread.<p>The new row has two paragraphs.</p>",
208 committed_at: new Date().toISOString(),
209 },
210 ]);
211 }}
212 >
213 Insert row
214 </button>
215 <button
216 onClick={() => {
217 if (view === "stories")
218 setStories((rows) =>
219 rows.map((row) => ({
220 ...row,
221 rank: rows.length - row.rank + 1,
222 })),
223 );
224 else
225 setComments((rows) =>
226 rows.map((row) => ({
227 ...row,
228 path: row.path.startsWith("000001")
229 ? row.path.replace(/^000001/, "000002")
230 : row.path.startsWith("000002")
231 ? row.path.replace(/^000002/, "000001")
232 : row.path,
233 })),
234 );
235 }}
236 >
237 Move rows
238 </button>
239 <button
240 onClick={() =>
241 setComments((rows) =>
242 rows.map((row) =>
243 row.item_id === 501
244 ? {
245 ...row,
246 text: `${row.text}<p>An edited paragraph.</p>`,
247 committed_at: new Date().toISOString(),
248 }
249 : row,
250 ),
251 )
252 }
253 >
254 Edit comment
255 </button>
256 <button
257 onClick={() =>
258 setComments((rows) =>
259 rows.map((row) =>
260 row.item_id === 502
261 ? {
262 ...row,
263 text: `${row.text}<p>Edited reply.</p>`,
264 committed_at: new Date().toISOString(),
265 }
266 : row,
267 ),
268 )
269 }
270 >
271 Edit reply
272 </button>
273 <button
274 onClick={() => {
275 setStories(initialStories);
276 setComments(initialComments);
277 }}
278 >
279 Reset fixture
280 </button>
281 <button
282 onClick={() =>
283 setEvents((rows) => [
284 ...rows,
285 {
286 event_id: sequence.current++,
287 item_id: 99999,
288 story_id: 99999,
289 kind: "comment-removed",
290 label: "Offscreen comment",
291 details: "{}",
292 occurred_at: Date.now(),
293 },
294 ])
295 }
296 >
297 Offscreen event
298 </button>
299 <button
300 onClick={() =>
301 view === "stories"
302 ? setStories((rows) => rows.slice(1))
303 : setComments((rows) => rows.filter((row) => row.item_id !== 503))
304 }
305 >
306 Remove row
307 </button>
308 </div>
309 {view === "stories" ? (
310 <News
311 feed="top"
312 page={1}
313 query={null}
314 initialRows={initialStories}
315 rows={stories}
316 initialNow={initialNow}
317 connection="fixture"
318 fixtureEvents={events}
319 />
320 ) : (
321 <Discussion
322 initialStories={initialStories.slice(0, 1)}
323 stories={stories.filter((row) => row.item_id === 1000)}
324 initialComments={initialComments}
325 comments={comments}
326 queries={null}
327 initialNow={initialNow}
328 connection="fixture"
329 fixtureEvents={events}
330 />
331 )}
332 </>
333 );
334}