@@ -13,6 +13,7 @@ import {
1313import { Action , AsyncAction } from 'app/overmind' ;
1414import { utcToZonedTime } from 'date-fns-tz' ;
1515
16+ import { captureException } from '@codesandbox/common/lib/utils/analytics/sentry' ;
1617import { OPTIMISTIC_COMMENT_ID } from './state' ;
1718
1819export const selectCommentsFilter : Action < CommentsFilterOption > = (
@@ -212,7 +213,7 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
212213 }
213214
214215 const id = OPTIMISTIC_COMMENT_ID ;
215- const sandboxId = state . editor . currentSandbox . id ;
216+ const sandbox = state . editor . currentSandbox ;
216217 const now = utcToZonedTime ( new Date ( ) . toISOString ( ) , 'Etc/UTC' ) ;
217218 let codeReference : CodeReference | null = null ;
218219 const selection = state . live . currentSelection ;
@@ -228,6 +229,7 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
228229 )
229230 : '' ,
230231 path : state . editor . currentModule . path ,
232+ sandboxVersion : sandbox . version ,
231233 } ;
232234 }
233235
@@ -258,11 +260,11 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
258260 } ;
259261 const comments = state . comments . comments ;
260262
261- if ( ! comments [ sandboxId ] ) {
262- comments [ sandboxId ] = { } ;
263+ if ( ! comments [ sandbox . id ] ) {
264+ comments [ sandbox . id ] = { } ;
263265 }
264266
265- comments [ sandboxId ] [ id ] = optimisticComment ;
267+ comments [ sandbox . id ] [ id ] = optimisticComment ;
266268 // placeholder value until we know the correct values
267269 const {
268270 left,
@@ -299,17 +301,17 @@ export const addComment: AsyncAction<{
299301 }
300302
301303 const id = OPTIMISTIC_COMMENT_ID ;
302- const sandbox = state . editor . currentSandbox ;
304+ const sandboxId = state . editor . currentSandbox . id ;
303305 const now = utcToZonedTime ( new Date ( ) . toISOString ( ) , 'Etc/UTC' ) ;
304306 const comments = state . comments . comments ;
305307
306- if ( ! comments [ sandbox . id ] ) {
307- comments [ sandbox . id ] = { } ;
308+ if ( ! comments [ sandboxId ] ) {
309+ comments [ sandboxId ] = { } ;
308310 }
309311
310312 let optimisticComment : CommentFragment ;
311- if ( state . comments . comments [ sandbox . id ] [ id ] ) {
312- optimisticComment = state . comments . comments [ sandbox . id ] [ id ] ;
313+ if ( state . comments . comments [ sandboxId ] [ id ] ) {
314+ optimisticComment = state . comments . comments [ sandboxId ] [ id ] ;
313315 } else {
314316 optimisticComment = {
315317 parentComment : parentCommentId ? { id : parentCommentId } : null ,
@@ -327,20 +329,25 @@ export const addComment: AsyncAction<{
327329 references : [ ] ,
328330 comments : [ ] ,
329331 } ;
330- comments [ sandbox . id ] [ id ] = optimisticComment ;
332+ comments [ sandboxId ] [ id ] = optimisticComment ;
331333 }
332334
333335 if ( optimisticComment . parentComment ) {
334- comments [ sandbox . id ] [ optimisticComment . parentComment . id ] . comments . push ( {
336+ comments [ sandboxId ] [ optimisticComment . parentComment . id ] . comments . push ( {
335337 id,
336338 } ) ;
337339 }
338340 state . comments . selectedCommentsFilter = CommentsFilterOption . OPEN ;
339341
340- try {
342+ // The server might be ahead on sandbox version, so we need to try to save
343+ // several times
344+ let tryCount = 0 ;
345+
346+ async function saveComment ( ) {
347+ tryCount ++ ;
341348 const response = await effects . gql . mutations . createComment ( {
342349 parentCommentId : parentCommentId || null ,
343- sandboxId : sandbox . id ,
350+ sandboxId,
344351 content,
345352 codeReference : optimisticComment . references . length
346353 ? optimisticComment . references [ 0 ] . metadata
@@ -349,15 +356,15 @@ export const addComment: AsyncAction<{
349356
350357 const comment = response . createComment ;
351358
352- delete comments [ sandbox . id ] [ id ] ;
353- comments [ sandbox . id ] [ comment . id ] = comment ;
359+ delete comments [ sandboxId ] [ id ] ;
360+ comments [ sandboxId ] [ comment . id ] = comment ;
354361
355362 if ( parentCommentId ) {
356- comments [ sandbox . id ] [ parentCommentId ] . comments . push ( {
363+ comments [ sandboxId ] [ parentCommentId ] . comments . push ( {
357364 id : comment . id ,
358365 } ) ;
359- comments [ sandbox . id ] [ parentCommentId ] . comments . splice (
360- comments [ sandbox . id ] [ parentCommentId ] . comments . findIndex (
366+ comments [ sandboxId ] [ parentCommentId ] . comments . splice (
367+ comments [ sandboxId ] [ parentCommentId ] . comments . findIndex (
361368 childComment => childComment . id === id
362369 ) ,
363370 1
@@ -366,13 +373,21 @@ export const addComment: AsyncAction<{
366373
367374 if ( state . comments . currentCommentId === OPTIMISTIC_COMMENT_ID ) {
368375 state . comments . currentCommentId = comment . id ;
369- delete comments [ sandbox . id ] [ OPTIMISTIC_COMMENT_ID ] ;
376+ delete comments [ sandboxId ] [ OPTIMISTIC_COMMENT_ID ] ;
370377 }
378+ }
379+ try {
380+ await saveComment ( ) ;
371381 } catch ( error ) {
372- effects . notificationToast . error (
373- 'Unable to create your comment, please try again'
374- ) ;
375- delete comments [ sandbox . id ] [ id ] ;
382+ if ( error . response ?. data ?. error === 'old_version' && tryCount < 3 ) {
383+ await saveComment ( ) ;
384+ } else {
385+ captureException ( error ) ;
386+ effects . notificationToast . error (
387+ 'Unable to create your comment, please try again'
388+ ) ;
389+ delete comments [ sandboxId ] [ id ] ;
390+ }
376391 }
377392} ;
378393
0 commit comments