Comment thread
A record's comments with their authors and a form to add one, kept current after each write. Web and native call useComments from @/hooks/use-comments.

Add the components
terminal npx shadcn add button field skeleton textareaInstall the glue
Install the block with
npx better-supabase add comments. The glue items are copied into your app, so edit them as you need.terminal npx shadcn add @better-supabase/use-commentsWrite the component
React and shadcn
View in the examplecomment-thread.tsx "use client"; import type { SubmitEvent } from "react"; import { useErrorMessage } from "better-supabase/react"; import { useExtracted, useFormatter } from "next-intl"; import { useId, useOptimistic, useState, useTransition } from "react"; import { Button } from "@/components/ui/button"; import { Field, FieldError, FieldLabel } from "@/components/ui/field"; import { Skeleton } from "@/components/ui/skeleton"; import { Textarea } from "@/components/ui/textarea"; import { useComments } from "@/hooks/use-comments"; /** * A record's comments, oldest first, through the browser client. `subject` is * any type in `config.recordTypes`, such as a customer or a project. */ export function CommentThread({ subject, authors, canComment, }: { subject: { readonly type: string; readonly id: string }; /** Display names by user id. */ authors: Readonly<Record<string, string>>; canComment: boolean; }) { const t = useExtracted("customers"); const format = useFormatter(); const errorMessage = useErrorMessage(); const fieldId = useId(); const [body, setBody] = useState(""); const [pending, startTransition] = useTransition(); const { comments, error, add } = useComments({ subject }); const visible = comments?.filter((comment) => !comment.deletedAt); // The comment shows at once and settles when the server's list arrives. const [sending, addPending] = useOptimistic<string | undefined, string>( undefined, (_current, text) => text, ); const submit = (event: SubmitEvent<HTMLFormElement>) => { event.preventDefault(); const text = body.trim(); if (!text) { return; } startTransition(async () => { addPending(text); if ((await add({ body: text })).ok) { setBody(""); } }); }; return ( <div className="flex flex-col gap-6"> {visible === undefined ? ( <Skeleton className="h-16" /> ) : visible.length === 0 && sending === undefined ? ( <p className="text-sm text-muted-foreground">{t("No comments yet.")}</p> ) : ( <ol className="flex flex-col gap-4" data-testid="comments"> {visible.map((comment) => ( <li key={comment.id} className="flex flex-col gap-1"> <p className="text-sm"> <span className="font-medium"> {(comment.authorId && authors[comment.authorId]) ?? t("Someone")} </span>{" "} <span className="text-xs text-muted-foreground"> {format.dateTime( (comment.sentAt ?? comment.createdAt).epochMilliseconds, { dateStyle: "medium", timeStyle: "short" }, )} </span> </p> <p className="text-sm whitespace-pre-wrap">{comment.body}</p> </li> ))} {sending === undefined ? null : ( <li className="text-sm whitespace-pre-wrap text-muted-foreground" aria-busy > {sending} </li> )} </ol> )} {canComment ? ( <form onSubmit={submit} className="flex flex-col gap-3"> <Field data-invalid={error ? true : undefined}> <FieldLabel htmlFor={`${fieldId}-comment-body`} className="sr-only"> {t("Comment")} </FieldLabel> <Textarea id={`${fieldId}-comment-body`} value={body} onChange={(event) => { setBody(event.target.value); }} placeholder={t("Write a comment")} required maxLength={4000} rows={3} /> {error ? <FieldError>{errorMessage(error)}</FieldError> : null} </Field> <Button type="submit" size="sm" disabled={pending}> {t("Comment")} </Button> </form> ) : null} </div> ); }