Love the direction. If two people edit the same note offline, does mergeNotes() keep both edits or is it last write wins?
Comment thread
A discussion under a design doc. Each comment has an initials avatar, the author’s name, an Author Badge for the doc’s writer, a timestamp, a body with inline Code, and reaction buttons. Replies nest one level on a rail, long reply chains collapse behind “Show 3 more replies”, and a Textarea composer appends your comment.
Discussion
8 comments on RFC 014: Offline sync for field notes- Lena Fischer
- Amara OseiAuthor
Both. Each edit is stored as an operation and replayed in
lamportorder, so nothing is dropped. The note is flagged when two edits touch the same line. - Diego Ramos
That matches the field pilot: conflicts showed up on fewer than 1% of notes.
- Diego Ramos
The storage estimate assumes photos are resized to
1600px. Coastal crews shoot at full resolution; can we budget for four times that?- Amara OseiAuthor
Good catch. I’ll rerun the numbers with
maxPhotoEdge = 4032and update the table.
Source · recipes/comment-thread.tsx
'use client'
import { Badge, BodySmall, Button, Code, Heading, Label, Stack, Text, Textarea } from '@blocks/design-system'
import { type FormEvent, type KeyboardEvent, useId, useState } from 'react'
import styles from './comment-thread.module.css'
interface Person {
name: string
initials: string
isAuthor?: boolean
}
interface Reaction {
emoji: string
label: string
count: number
mine: boolean
}
interface Comment {
id: string
author: Person
time: { iso: string; label: string }
body: string
reactions: Reaction[]
replies?: Comment[]
}
const amara: Person = { name: 'Amara Osei', initials: 'AO', isAuthor: true }
const lena: Person = { name: 'Lena Fischer', initials: 'LF' }
const diego: Person = { name: 'Diego Ramos', initials: 'DR' }
const wen: Person = { name: 'Wen Li', initials: 'WL' }
const you: Person = { name: 'Priya Raman', initials: 'PR' }
const thumbsUp = (count: number, mine = false): Reaction => ({ emoji: '👍', label: 'Thumbs up', count, mine })
const eyes = (count: number): Reaction => ({ emoji: '👀', label: 'Eyes', count, mine: false })
const tada = (count: number): Reaction => ({ emoji: '🎉', label: 'Party popper', count, mine: false })
const initialComments: Comment[] = [
{
id: 'c1',
author: lena,
time: { iso: '2026-11-30T10:14', label: 'Nov 30, 10:14' },
body: 'Love the direction. If two people edit the same note offline, does `mergeNotes()` keep both edits or is it last write wins?',
reactions: [thumbsUp(4), eyes(2)],
replies: [
{
id: 'c1-1',
author: amara,
time: { iso: '2026-11-30T10:32', label: 'Nov 30, 10:32' },
body: 'Both. Each edit is stored as an operation and replayed in `lamport` order, so nothing is dropped. The note is flagged when two edits touch the same line.',
reactions: [tada(3)],
},
{
id: 'c1-2',
author: diego,
time: { iso: '2026-11-30T11:05', label: 'Nov 30, 11:05' },
body: 'That matches the field pilot: conflicts showed up on fewer than 1% of notes.',
reactions: [],
},
{
id: 'c1-3',
author: wen,
time: { iso: '2026-11-30T13:47', label: 'Nov 30, 13:47' },
body: 'Could the sync badge show the conflict flag too? Crews rarely open the note list mid-shift.',
reactions: [thumbsUp(2)],
},
{
id: 'c1-4',
author: lena,
time: { iso: '2026-11-30T14:02', label: 'Nov 30, 14:02' },
body: 'Agree with Wen, a count on the badge would be enough.',
reactions: [],
},
{
id: 'c1-5',
author: amara,
time: { iso: '2026-11-30T16:20', label: 'Nov 30, 16:20' },
body: 'Added to the open questions, with a sketch of `SyncBadge` showing a count.',
reactions: [thumbsUp(1)],
},
],
},
{
id: 'c2',
author: diego,
time: { iso: '2026-12-01T09:02', label: 'Dec 1, 09:02' },
body: 'The storage estimate assumes photos are resized to `1600px`. Coastal crews shoot at full resolution; can we budget for four times that?',
reactions: [thumbsUp(2, true)],
replies: [
{
id: 'c2-1',
author: amara,
time: { iso: '2026-12-01T09:40', label: 'Dec 1, 09:40' },
body: 'Good catch. I’ll rerun the numbers with `maxPhotoEdge = 4032` and update the table.',
reactions: [],
},
],
},
]
// Replies beyond this many start collapsed.
const visibleReplies = 2
// Toggles one reaction anywhere in the thread.
function toggleReaction(comments: Comment[], id: string, emoji: string): Comment[] {
return comments.map((comment) => ({
...comment,
reactions:
comment.id === id
? comment.reactions.map((reaction) =>
reaction.emoji === emoji
? { ...reaction, mine: !reaction.mine, count: reaction.count + (reaction.mine ? -1 : 1) }
: reaction,
)
: comment.reactions,
replies: comment.replies && toggleReaction(comment.replies, id, emoji),
}))
}
// A discussion under a design doc: reactions are toggle buttons, long reply
// chains collapse, and the composer appends a comment.
export default function CommentThread() {
const [comments, setComments] = useState(initialComments)
const [draft, setDraft] = useState('')
const id = useId()
const total = comments.reduce((sum, comment) => sum + 1 + (comment.replies?.length ?? 0), 0)
function post(event?: FormEvent<HTMLFormElement>) {
event?.preventDefault()
const body = draft.trim()
if (!body) return
const now = new Date()
setComments((current) => [
...current,
{ id: `c${now.getTime()}`, author: you, time: { iso: now.toISOString(), label: 'Just now' }, body, reactions: [] },
])
setDraft('')
}
function submitOnShortcut(event: KeyboardEvent<HTMLTextAreaElement>) {
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) post()
}
const react = (commentId: string, emoji: string) => setComments((current) => toggleReaction(current, commentId, emoji))
return (
<Stack as="section" gap="lg" aria-labelledby={`${id}-title`} dangerouslySetClassName={styles.root}>
<Stack gap="xxs">
<Heading as="h2" id={`${id}-title`} fontSize="lg">
Discussion
</Heading>
<Text fontSize="sm" color="text-muted">
{total} comments on RFC 014: Offline sync for field notes
</Text>
</Stack>
<ol className={styles.thread}>
{comments.map((comment) => (
<li key={comment.id}>
<CommentView comment={comment} onReact={react} />
{comment.replies && comment.replies.length > 0 && <Replies parent={comment} onReact={react} />}
</li>
))}
</ol>
<Stack as="form" gap="sm" onSubmit={post} aria-label="Add a comment" dangerouslySetClassName={styles.composer}>
<Label htmlFor={`${id}-draft`}>Add a comment</Label>
<Textarea
id={`${id}-draft`}
rows={3}
value={draft}
placeholder="Reply to the discussion. Wrap code in `backticks`."
onChange={(event) => setDraft(event.target.value)}
onKeyDown={submitOnShortcut}
aria-describedby={`${id}-hint`}
/>
<Stack direction="row" justify="space-between" align="center" gap="sm" wrap="wrap">
<BodySmall id={`${id}-hint`} margin="none" color="text-muted">
Press ⌘ Enter or Ctrl Enter to post.
</BodySmall>
<Button type="submit" variant="primary" disabled={!draft.trim()}>
Comment
</Button>
</Stack>
</Stack>
</Stack>
)
}
function Replies({ parent, onReact }: { parent: Comment; onReact: (id: string, emoji: string) => void }) {
const [expanded, setExpanded] = useState(false)
const listId = useId()
const replies = parent.replies ?? []
const hidden = replies.length - visibleReplies
return (
<div className={styles.replies}>
<ol id={listId} aria-label={`Replies to ${parent.author.name}`} className={styles.thread}>
{(expanded ? replies : replies.slice(0, visibleReplies)).map((reply) => (
<li key={reply.id}>
<CommentView comment={reply} onReact={onReact} />
</li>
))}
</ol>
{hidden > 0 && (
<Button
type="button"
variant="link"
size="sm"
aria-expanded={expanded}
aria-controls={listId}
onClick={() => setExpanded(!expanded)}
>
{expanded ? 'Show fewer replies' : `Show ${hidden} more ${hidden === 1 ? 'reply' : 'replies'}`}
</Button>
)}
</div>
)
}
function CommentView({ comment, onReact }: { comment: Comment; onReact: (id: string, emoji: string) => void }) {
const { author } = comment
return (
<article aria-label={`${author.name}, ${comment.time.label}`} className={styles.comment}>
<span aria-hidden className={author.isAuthor ? styles.avatarAuthor : styles.avatar}>
{author.initials}
</span>
<Stack gap="xs" dangerouslySetClassName={styles.content}>
<Stack direction="row" gap="sm" align="center" wrap="wrap">
<Text fontSize="sm" fontWeight="semibold">
{author.name}
</Text>
{author.isAuthor && <Badge tone="accent">Author</Badge>}
<Text fontSize="xs" color="text-muted">
<time dateTime={comment.time.iso}>{comment.time.label}</time>
</Text>
</Stack>
<BodySmall margin="none">
<InlineMarkdown text={comment.body} />
</BodySmall>
{comment.reactions.length > 0 && (
<Stack direction="row" gap="xs" wrap="wrap">
{comment.reactions.map((reaction) => (
<button
key={reaction.emoji}
type="button"
aria-pressed={reaction.mine}
aria-label={`${reaction.label}, ${reaction.count}`}
onClick={() => onReact(comment.id, reaction.emoji)}
className={styles.reaction}
>
<span aria-hidden>{reaction.emoji}</span>
{reaction.count}
</button>
))}
</Stack>
)}
</Stack>
</article>
)
}
// Just enough Markdown for comments: text between backticks becomes <Code>.
function InlineMarkdown({ text }: { text: string }) {
// Odd parts sit between a pair of backticks.
// biome-ignore lint/suspicious/noArrayIndexKey: the parts of one string never reorder
return text.split('`').map((part, index) => (index % 2 === 1 ? <Code key={index}>{part}</Code> : part))
}How it works
- Reactions are toggle buttons with
aria-pressed, named by the reaction and its count (“Thumbs up, 4”) because the emoji alone reads differently across screen readers. Pressing one updates the count wherever the comment sits in the thread. - Replies are a nested ordered list labelled for the comment they answer. The collapse button sets
aria-expandedandaria-controlson that list, and says how many replies it hides. - Comment bodies get just enough Markdown: text between backticks renders as
Code, for seeded comments and new ones alike. - The Comment button is disabled until the draft has text, and ⌘ Enter or Ctrl Enter posts from the
Textarea; the hint is linked to the field witharia-describedby. The new comment joins the end of the thread and the composer clears. - Each comment is an
articlenamed by its author and time, so a screen reader’s article navigation steps through the thread.
App-owned CSS
.root {
--avatar: var(--ds-control-height-md);
}
.thread {
display: grid;
gap: var(--ds-space-lg);
margin: 0;
padding: 0;
list-style: none;
}
/* Avatar in a narrow column, everything else beside it. */
.comment {
display: grid;
grid-template-columns: var(--avatar) minmax(0, 1fr);
gap: var(--ds-space-md);
}
.avatar,
.avatarAuthor {
display: grid;
place-items: center;
width: var(--avatar);
height: var(--avatar);
font-size: var(--ds-font-size-xs);
font-weight: var(--ds-font-weight-semibold);
color: var(--ds-color-text-muted);
background-color: var(--ds-color-surface);
border-radius: var(--ds-border-radius-circle);
}
.avatarAuthor {
color: var(--ds-color-accent-text);
background-color: var(--ds-color-accent-subtle);
}
.content {
min-width: 0;
overflow-wrap: anywhere;
}
/* Replies sit one level in, on a rail under the parent's avatar. */
.replies {
display: grid;
gap: var(--ds-space-sm);
justify-items: start;
margin-top: var(--ds-space-lg);
margin-left: calc(var(--avatar) / 2 - var(--ds-border-width-sm));
padding-left: calc(var(--avatar) / 2 + var(--ds-space-md) - var(--ds-border-width-sm));
border-left: var(--ds-border-width-md) solid var(--ds-color-border-subtle);
}
.reaction {
display: inline-flex;
align-items: center;
gap: var(--ds-space-sm);
min-height: var(--ds-control-height-sm);
padding: 0 var(--ds-space-md);
font: inherit;
font-size: var(--ds-font-size-xs);
font-weight: var(--ds-font-weight-medium);
font-variant-numeric: tabular-nums;
color: var(--ds-color-text-muted);
background-color: var(--ds-color-bg);
border: var(--ds-border-width-sm) solid var(--ds-color-border);
border-radius: var(--ds-border-radius-pill);
cursor: pointer;
&:hover {
background-color: var(--ds-color-surface-hover);
}
&[aria-pressed="true"] {
color: var(--ds-color-accent-text);
background-color: var(--ds-color-accent-subtle);
border-color: var(--ds-color-accent);
}
&:focus-visible {
outline: var(--ds-border-width-md) solid var(--ds-color-focus-ring);
outline-offset: var(--ds-space-xxs);
}
}
.composer {
padding-top: var(--ds-space-lg);
border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
}