@@ -79,7 +79,8 @@ function createOwnHistory() {
7979 changes : ( ) => changes ,
8080 claimCount : ( ) => claimed . count ,
8181 claimedAfter : ( count : number ) => ( claimed . count > count && claimed . id ? claimed . id : null ) ,
82- unlessUndone : ( id : string | null ) => ( id && ! undoneIds . has ( id ) ? id : null ) ,
82+ targetFor : ( id : string | null , pressedAt : number ) =>
83+ ! id || ! undoneIds . has ( id ) ? id : claimed . count > pressedAt ? false : null ,
8384 noteClaim : ( id : string ) => {
8485 claimed = { count : claimed . count + 1 , id } ;
8586 } ,
@@ -126,6 +127,10 @@ function historyUrl(projectId: string, path = ""): string {
126127 return `/api/projects/${ encodeURIComponent ( projectId ) } /history${ path } ` ;
127128}
128129
130+ async function cantUndo ( paths : string [ ] | undefined ) : Promise < ApplyResult > {
131+ return { ok : false , reason : "content-mismatch" , paths : paths ?? [ ] } ;
132+ }
133+
129134function unionPaths ( ...lists : Array < readonly string [ ] | undefined > ) : string [ ] {
130135 return [ ...new Set ( lists . flatMap ( ( list ) => list ?? [ ] ) ) ] ;
131136}
@@ -261,9 +266,9 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory
261266 const next = direction === "undo" ? view . back : view . forward ;
262267 const stepPaths = candidate ? Object . keys ( own . afterOf ( candidate ) ) : next ?. paths ;
263268 const paths = unionPaths ( stepPaths , heldClaimRef . current ?. paths ) ;
269+ const pressedAt = own . claimCount ( ) ;
264270 own . overtake ( ) ;
265- const run = async ( ) : Promise < ApplyResult > => {
266- const target = own . unlessUndone ( candidate ) ;
271+ const attempt = async ( target : string | null ) : Promise < ApplyResult > => {
267272 const previous = await readAll ( paths , callbacks . readFile ) ;
268273 const posted = await post (
269274 historyUrl ( projectId , target ? "/undo" : "/step" ) ,
@@ -295,6 +300,10 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory
295300 ) ,
296301 } ;
297302 } ;
303+ const run = ( ) => {
304+ const target = own . targetFor ( candidate , pressedAt ) ;
305+ return target === false ? cantUndo ( stepPaths ) : attempt ( target ) ;
306+ } ;
298307 return callbacks . serialize ? callbacks . serialize ( paths , run ) : run ( ) ;
299308 } ,
300309 [ projectId , view , refresh , own ] ,
0 commit comments