Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): preserve optimistic media comments via clientKey

Richie McIlroyb890e6c90b11
diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx
index 3f9277643..d114fd7d7 100644
--- a/apps/web/app/s/[videoId]/Share.tsx
+++ b/apps/web/app/s/[videoId]/Share.tsx
@@ -60,6 +60,12 @@ export type CommentType = typeof commentsSchema.$inferSelect & {
authorName?: string | null;
authorImage?: ImageUpload.ImageUrl | null;
sending?: boolean;
+ /**
+ * The optimistic entry's temp id, carried onto the saved comment so lists
+ * can key the pair identically — the settle then updates the mounted card
+ * in place instead of remounting it (which would restart media playback).
+ */
+ clientKey?: string;
};
const SESSION_STORAGE_KEY = "cap_tb_session_id";
@@ -554,6 +560,20 @@ export const Share = ({
}, 100);
}, []);
+ // If the settle ever commits the saved comment while its optimistic twin is
+ // still applied, both would render under the same clientKey; keep only the
+ // settled one. Almost always a no-op passthrough.
+ const visibleComments = useMemo(() => {
+ const settledKeys = new Set<string>();
+ for (const comment of optimisticComments)
+ if (!comment.sending && comment.clientKey)
+ settledKeys.add(comment.clientKey);
+ if (settledKeys.size === 0) return optimisticComments;
+ return optimisticComments.filter(
+ (comment) => !(comment.sending && settledKeys.has(comment.id)),
+ );
+ }, [optimisticComments]);
+
const reduceMotion = useReducedMotion() ?? false;
const [selectedView, setSelectedView] = useState<ShareView>(initialView);
// Screenshots have no timeline to show, and a comments-disabled video has
@@ -994,7 +1014,7 @@ export const Share = ({
>
<Suspense fallback={<TimelineSkeleton />}>
<TimelineView
- comments={optimisticComments}
+ comments={visibleComments}
videoId={data.id}
chapters={aiData?.chapters ?? undefined}
transcriptionStatus={transcriptionStatus}
@@ -1108,7 +1128,7 @@ export const Share = ({
videoSettings={videoSettings}
commentsData={commentsData}
setCommentsData={setCommentsData}
- optimisticComments={optimisticComments}
+ optimisticComments={visibleComments}
setOptimisticComments={setOptimisticComments}
handleCommentSuccess={handleCommentSuccess}
views={views}
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
index 8739153ec..8edf569e7 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
@@ -69,11 +69,18 @@ const CommentComponent: React.FC<{
return (
<div
id={`comment-${comment.id}`}
- key={`comment-${comment.id}`}
+ // No key here: the list already keys this component by clientKey, and
+ // an id-derived key would force a remount at the optimistic→saved swap
+ // (the id changes) — restarting any playing media in the card.
className={clsx(
`space-y-3`,
level > 0 ? "ml-8 border-l-2 border-gray-100 pl-4" : "",
- comment.sending ? "opacity-20" : "opacity-100",
+ // A media comment uploads for a while and carries its own progress
+ // ring — ghosting it out for the duration would read as broken. Text
+ // keeps the classic dim for its sub-second round trip.
+ comment.sending && !isMediaComment(comment)
+ ? "opacity-20"
+ : "opacity-100",
)}
>
<div className="flex items-start space-x-2.5">
@@ -190,7 +197,7 @@ const CommentComponent: React.FC<{
<div className="mt-3 space-y-3">
{nestedReplies.map((reply) => (
<CommentComponent
- key={reply.id}
+ key={reply.clientKey ?? reply.id}
comment={reply}
replies={replies}
onReply={onReply}
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comments.tsx b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comments.tsx
index b2f546ac3..54752906a 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comments.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comments.tsx
@@ -231,7 +231,9 @@ export const Comments = Object.assign(
<div className="p-4 space-y-6">
{rootComments.map((comment) => (
<CommentComponent
- key={comment.id}
+ // clientKey pins a just-uploaded comment to its optimistic
+ // card's mount, so the settle can't restart its playback.
+ key={comment.clientKey ?? comment.id}
comment={comment}
replies={optimisticComments}
onReply={(id) => {
diff --git a/apps/web/app/s/[videoId]/_components/timeline/timeline-layout.ts b/apps/web/app/s/[videoId]/_components/timeline/timeline-layout.ts
index 11dd5403a..fc61468d9 100644
--- a/apps/web/app/s/[videoId]/_components/timeline/timeline-layout.ts
+++ b/apps/web/app/s/[videoId]/_components/timeline/timeline-layout.ts
@@ -13,10 +13,19 @@ const CULL_MARGIN = 0.02;
/** The only fields the layout cares about. */
export interface LayoutComment {
id: string;
+ /** Stable key carried from a comment's optimistic entry onto its saved row. */
+ clientKey?: string;
timestamp: number | null;
createdAt: Date | string | number;
}
+/**
+ * Node identity: the clientKey when present, so a just-uploaded comment keeps
+ * the node (and any open hover card) its optimistic twin was mounted under.
+ */
+const keyOf = (comment: LayoutComment): string =>
+ comment.clientKey ?? comment.id;
+
export type BranchLane = 0 | 1;
export type BranchNode<C extends LayoutComment = LayoutComment> =
@@ -84,7 +93,7 @@ export function layoutBranches<C extends LayoutComment>(
const push = (comment: C & { timestamp: number }, lane: BranchLane) => {
nodes.push({
kind: "single",
- id: comment.id,
+ id: keyOf(comment),
t: comment.timestamp,
lane,
comment,
@@ -131,7 +140,7 @@ export function layoutBranches<C extends LayoutComment>(
// Full: start a new stack in the same lane and measure future gaps from it.
nodes.push({
kind: "cluster",
- id: comment.id,
+ id: keyOf(comment),
t,
lane: last.lane,
comments: [comment],