Short-Video App with React Native and Supabase · Course book
Chapter 12: Comments
Comments turn watching into conversation. A good comment section opens quickly over the video, lists comments newest or most relevant first with each author's picture and name, lets the viewer type a reply without the keyboard hiding what they are typing, and shows their comment immediately after sending. This chapter builds that section for the short-video app: the comments table and the query that loads a video's comments with their authors, the list and its rendering, the input bar and keyboard handling, inserting a comment and refreshing the list, relative times such as "3h ago" worked out by hand, and the safety features any public comment section needs.
Loading a video's comments
Each comment is a row in the Comment table with its text, the id of the author, the id of the video and the id of the video's owner, plus a creation time. Loading the comments for one video is a filtered, ordered query that also fetches each author's username and picture in the same request:
const { data, error } = await supabase
.from("Comment")
.select("*, User(username, profile_photo)")
.eq("video_id", videoId)
.order("created_at", { ascending: false });
The eq filter selects only this video's comments, which is fast as long as the video_id column has an index; without one, every comment in the table would be scanned to find them. Ordering newest first suits a lively feed; some apps instead put the most-liked comments first, which needs a like count per comment. Wrapping the query in a TanStack Query hook keyed by the video id, such as ["comments", videoId], caches each video's comments separately, so reopening the same video's comments is instant.
The screen receives the video id, and the video owner's id, as route parameters from the overlay. If the video no longer exists, or the query fails, the screen shows a short message rather than an empty list, so users know what happened.
Rendering the list
Comments are displayed in a FlatList, which renders only the rows near the visible area, so a video with thousands of comments stays smooth. Each row shows the author's circular picture, their username in bold, the comment text, and the time since it was posted in a muted colour. A row component, kept small and memoised, receives a single comment and draws it; the list provides each comment's id as its key.
The comments screen usually appears as a bottom sheet covering most of the screen, with the video still visible above it, and a header such as "318 comments". An empty list shows a friendly prompt, "No comments yet. Start the conversation", which is more inviting than a blank space. While the first query runs, a few grey placeholder rows, sometimes called skeletons, show the shape of what is coming and make the wait feel shorter than a spinner does. Tapping an author's picture or name should open their profile, using the same dynamic route as the overlay, so that conversations lead naturally to discovering new creators. If comments are numerous, loading them in pages of, say, 30, and fetching more when the user reaches the end of the list keeps the first load quick.
The same list and row components can later serve the chat screen, which shows messages in a very similar format. Writing them as general "message list" components, with the data and a few display options as props, avoids building the same thing twice, a design choice that pays off in the chapter on chat.
The input bar and the keyboard
At the bottom of the sheet sits an input bar: the viewer's own picture, a text input with placeholder text such as "Add a comment", and a send button that is disabled while the input is empty. The input should allow several lines and grow as the user types, up to a maximum height after which it scrolls, and a character limit stops essay-length comments.
On phones, the on-screen keyboard rises from the bottom and covers whatever is there, including, by default, the very input being typed into. React Native's KeyboardAvoidingView solves this by adjusting its content when the keyboard appears. On iOS the usual behaviour is "padding", which adds space equal to the keyboard's height at the bottom, pushing the input bar up; on Android, where the system can resize the window itself, "height" or no extra behaviour often works better. An offset may be needed to account for headers above the view.
Dismissing the keyboard should feel natural too. Tapping the list or dragging it down should close the keyboard, which FlatList supports with its keyboardDismissMode property, and sending a comment can dismiss it automatically. These small behaviours are what separate a comment box that feels native from one that fights the user.
Posting a comment
When the user taps send, the app trims the text, checks that it is not empty, and inserts a row:
await supabase.from("Comment").insert({
text: text.trim(),
user_id: user.id,
video_id: videoId,
video_user_id: videoUserId,
});
The video_user_id records whose video it is, which the activity feed later uses to show "someone commented on your video". After a successful insert, the input is cleared and the comments query is invalidated, so it refetches and the new comment appears at the top. For a faster feel, an optimistic update can add the comment to the list immediately, marked as sending, and replace it with the stored version once the insert succeeds, or remove it with an error message if it fails.
Security is enforced by the database, not the app. A row level security policy on the Comment table should allow signed-in users to read comments, allow inserts only when user_id equals the requester's own id, and allow deletes only by the comment's author, or perhaps the video's owner. That way a modified app cannot post comments in someone else's name.
Public comments also need safety features, and app stores require them for apps with user-generated content. At a minimum, users should be able to report a comment, which records it in a reports table for review, and to block another user, after which that person's comments are hidden from them. Video owners commonly get the power to delete comments on their own videos. A simple filter that holds back comments containing words from a blocked list, or that limits how many comments one account can post per minute, deters the most obvious abuse. These features are easy to add to the schema early, and awkward to bolt on after an app becomes popular.
Comment counts shown in the overlay must stay consistent with the list. The simplest approach counts the rows each time, which a database can do quickly with an index; a busier app might keep a running count on the video row, updated by a database trigger whenever a comment is added or removed, so the feed can show it without counting.
Relative times
Comments show how long ago they were posted rather than exact timestamps. The rule is simple: compute the difference between now and the creation time, then choose the largest sensible unit.
| Age of the comment | Shown as | Calculation |
|---|---|---|
| Under 60 seconds | just now | Difference below 60 s |
| Under 60 minutes | 25m | Seconds / 60, rounded down |
| Under 24 hours | 3h | Seconds / 3,600, rounded down |
| Under 7 days | 2d | Seconds / 86,400, rounded down |
| Older | 14 Nov | A short date instead |
Relative times go stale while the screen is open. Either recompute them when the list refreshes, or rerender the list every minute while it is visible. JavaScript's Intl.RelativeTimeFormat can produce longer, localised phrases such as "3 hours ago" in the user's language, if the design calls for full words rather than abbreviations. Long-pressing a time could reveal the exact date and time for anyone who wants it.
Practice tasks
- It is 18:20. Give the relative time for comments created at 18:19:30, 17:55, 09:10 the same day, and 18:20 four days earlier.
- Write the policy rules that allow reading all comments, inserting only as yourself and deleting only your own comments.
- Explain why the comments query needs an index on
video_id. - Describe how an optimistic update for a new comment works, including what happens if the insert fails.
- Which
KeyboardAvoidingViewbehaviour is usually used on iOS, and why might Android differ? - How could the same list component serve both comments and chat messages?
This course book is available to read online only at tech-academy.chrysolites.org.