@@ -733,6 +733,73 @@ export default function EspacesPage() {
733733 return ( ) => window . removeEventListener ( 'ocl:queue-flushed' , h ) ;
734734 } , [ ] ) ;
735735
736+ // ── Undo / redo (Ctrl+Z / Ctrl+Y) ────────────────────────────────────────────
737+ // A bounded stack of reversible actions. Delete = trash (undo restores); move / rename capture
738+ // the previous location / name. Zero-Knowledge deletes are permanent, so they are NOT tracked.
739+ type UndoAction =
740+ | { type : 'trash' ; items : { kind : 'file' | 'folder' ; id : string } [ ] }
741+ | { type : 'move' ; moves : { kind : 'file' | 'folder' ; id : string ; from : string | null ; to : string | null } [ ] }
742+ | { type : 'rename' ; kind : 'file' | 'folder' ; id : string ; from : string ; to : string } ;
743+ const undoRef = useRef < UndoAction [ ] > ( [ ] ) ;
744+ const redoRef = useRef < UndoAction [ ] > ( [ ] ) ;
745+ function pushUndo ( a : UndoAction ) {
746+ undoRef . current . push ( a ) ;
747+ if ( undoRef . current . length > 30 ) undoRef . current . shift ( ) ;
748+ redoRef . current = [ ] ;
749+ }
750+ // Clear history when switching spaces so an undo can't act across contexts.
751+ useEffect ( ( ) => {
752+ undoRef . current = [ ] ;
753+ redoRef . current = [ ] ;
754+ } , [ activeSpaceId ] ) ;
755+
756+ async function applyAction ( a : UndoAction , dir : 'undo' | 'redo' ) {
757+ if ( a . type === 'trash' ) {
758+ for ( const it of a . items ) {
759+ const base = it . kind === 'folder' ? 'folders' : 'files' ;
760+ if ( dir === 'undo' ) await api . post ( `/trash/${ base } /${ it . id } /restore` ) ;
761+ else await api . del ( `/${ base } /${ it . id } ` ) ;
762+ }
763+ } else if ( a . type === 'move' ) {
764+ for ( const m of a . moves ) {
765+ const target = dir === 'undo' ? m . from : m . to ;
766+ if ( m . kind === 'folder' ) await api . patch ( `/folders/${ m . id } ` , { parentId : target } ) ;
767+ else await api . patch ( `/files/${ m . id } ` , { folderId : target } ) ;
768+ }
769+ } else {
770+ const name = dir === 'undo' ? a . from : a . to ;
771+ if ( a . kind === 'folder' ) await api . patch ( `/folders/${ a . id } ` , { name } ) ;
772+ else await api . patch ( `/files/${ a . id } ` , { name } ) ;
773+ }
774+ }
775+
776+ async function undo ( ) {
777+ const a = undoRef . current . pop ( ) ;
778+ if ( ! a ) return ;
779+ try {
780+ await applyAction ( a , 'undo' ) ;
781+ redoRef . current . push ( a ) ;
782+ await Promise . all ( [ loadFolders ( ) , reloadCurrent ( ) ] ) ;
783+ toast ( t ( 'drive.undone' ) , 'info' ) ;
784+ } catch ( err ) {
785+ undoRef . current . push ( a ) ;
786+ toast ( err instanceof ApiError ? err . message : t ( 'common.opFailed' ) , 'error' ) ;
787+ }
788+ }
789+ async function redo ( ) {
790+ const a = redoRef . current . pop ( ) ;
791+ if ( ! a ) return ;
792+ try {
793+ await applyAction ( a , 'redo' ) ;
794+ undoRef . current . push ( a ) ;
795+ await Promise . all ( [ loadFolders ( ) , reloadCurrent ( ) ] ) ;
796+ toast ( t ( 'drive.redone' ) , 'info' ) ;
797+ } catch ( err ) {
798+ redoRef . current . push ( a ) ;
799+ toast ( err instanceof ApiError ? err . message : t ( 'common.opFailed' ) , 'error' ) ;
800+ }
801+ }
802+
736803 // ── Selection ───────────────────────────────────────────────────────────────
737804 function toggleKey ( key : string ) {
738805 setSelected ( ( prev ) => {
@@ -863,12 +930,15 @@ export default function EspacesPage() {
863930 async function deleteFolder ( id : string ) {
864931 if ( ! ( await confirm ( { title : t ( 'drive.deleteFolderTitle' ) , message : t ( 'drive.deleteFolderMsg' ) , confirmLabel : t ( 'drive.deleteToTrash' ) } ) ) ) return ;
865932 await api . del ( `/folders/${ id } ` ) ;
933+ if ( ! isZk ) pushUndo ( { type : 'trash' , items : [ { kind : 'folder' , id } ] } ) ;
866934 await Promise . all ( [ loadFolders ( ) , refresh ( ) ] ) ;
867935 toast ( t ( 'drive.movedToTrash' ) , 'success' ) ;
868936 }
869937 async function deleteFile ( id : string ) {
870938 if ( ! ( await confirm ( { title : t ( 'drive.deleteFileTitle' ) , confirmLabel : t ( 'drive.deleteToTrash' ) } ) ) ) return ;
871939 await api . del ( isZk ? `/zk/files/${ id } ` : `/files/${ id } ` ) ;
940+ // ZK deletes are permanent (no Trash) → not undoable.
941+ if ( ! isZk ) pushUndo ( { type : 'trash' , items : [ { kind : 'file' , id } ] } ) ;
872942 await reloadCurrent ( ) ;
873943 toast ( t ( 'drive.movedToTrash' ) , 'success' ) ;
874944 }
@@ -897,12 +967,14 @@ export default function EspacesPage() {
897967 const name = await prompt ( { title : t ( 'drive.renameTitle' ) , label : t ( 'drive.renameLabel' ) , defaultValue : f . name } ) ;
898968 if ( ! name || name === f . name ) return ;
899969 await api . patch ( `/files/${ f . id } ` , { name } ) ;
970+ if ( ! isZk ) pushUndo ( { type : 'rename' , kind : 'file' , id : f . id , from : f . name , to : name } ) ;
900971 await reloadCurrent ( ) ;
901972 }
902973 async function renameFolder ( f : PublicFolder ) {
903974 const name = await prompt ( { title : t ( 'drive.renameFolderTitle' ) , label : t ( 'drive.renameLabel' ) , defaultValue : f . name } ) ;
904975 if ( ! name || name === f . name ) return ;
905976 await api . patch ( `/folders/${ f . id } ` , { name } ) ;
977+ if ( ! isZk ) pushUndo ( { type : 'rename' , kind : 'folder' , id : f . id , from : f . name , to : name } ) ;
906978 await loadFolders ( ) ;
907979 }
908980 function renameByKey ( key : string ) {
@@ -923,6 +995,7 @@ export default function EspacesPage() {
923995 if ( dest === null ) return ;
924996 const target = dest === '__root__' ? null : dest ;
925997 await api . patch ( `/${ kind } s/${ id } ` , { folderId : target , parentId : target } ) ;
998+ if ( ! isZk ) pushUndo ( { type : 'move' , moves : [ { kind, id, from : currentFolderId , to : target } ] } ) ;
926999 await Promise . all ( [ loadFolders ( ) , reloadCurrent ( ) ] ) ;
9271000 toast ( t ( 'drive.moved' ) , 'success' ) ;
9281001 }
@@ -974,6 +1047,13 @@ export default function EspacesPage() {
9741047 else if ( it . kind === 'file' ) await api . del ( `/files/${ it . file . id } ` ) ;
9751048 else await api . del ( `/zk/files/${ it . zk . id } ` ) ;
9761049 }
1050+ // Only non-ZK deletes go to the Trash and can be undone.
1051+ const undoable : { kind : 'file' | 'folder' ; id : string } [ ] = [ ] ;
1052+ for ( const it of items ) {
1053+ if ( it . kind === 'folder' ) undoable . push ( { kind : 'folder' , id : it . folder . id } ) ;
1054+ else if ( it . kind === 'file' ) undoable . push ( { kind : 'file' , id : it . file . id } ) ;
1055+ }
1056+ if ( undoable . length > 0 ) pushUndo ( { type : 'trash' , items : undoable } ) ;
9771057 setSelected ( new Set ( ) ) ;
9781058 await Promise . all ( [ loadFolders ( ) , reloadCurrent ( ) ] ) ;
9791059 toast ( t ( 'drive.bulkDeleted' , { n : items . length } ) , 'success' ) ;
@@ -1017,6 +1097,12 @@ export default function EspacesPage() {
10171097 if ( it . kind === 'folder' ) await api . patch ( `/folders/${ it . folder . id } ` , { parentId : target } ) ;
10181098 else if ( it . kind === 'file' ) await api . patch ( `/files/${ it . file . id } ` , { folderId : target } ) ;
10191099 }
1100+ const moves : { kind : 'file' | 'folder' ; id : string ; from : string | null ; to : string | null } [ ] = [ ] ;
1101+ for ( const it of items ) {
1102+ if ( it . kind === 'folder' ) moves . push ( { kind : 'folder' , id : it . folder . id , from : currentFolderId , to : target } ) ;
1103+ else if ( it . kind === 'file' ) moves . push ( { kind : 'file' , id : it . file . id , from : currentFolderId , to : target } ) ;
1104+ }
1105+ if ( moves . length > 0 ) pushUndo ( { type : 'move' , moves } ) ;
10201106 setSelected ( new Set ( ) ) ;
10211107 await Promise . all ( [ loadFolders ( ) , reloadCurrent ( ) ] ) ;
10221108 toast ( t ( 'drive.bulkMoved' , { n : items . length } ) , 'success' ) ;
@@ -1325,6 +1411,17 @@ export default function EspacesPage() {
13251411 }
13261412 return ;
13271413 }
1414+ if ( mod && e . key . toLowerCase ( ) === 'z' ) {
1415+ e . preventDefault ( ) ;
1416+ if ( e . shiftKey ) void redo ( ) ;
1417+ else void undo ( ) ;
1418+ return ;
1419+ }
1420+ if ( mod && e . key . toLowerCase ( ) === 'y' ) {
1421+ e . preventDefault ( ) ;
1422+ void redo ( ) ;
1423+ return ;
1424+ }
13281425 if ( mod && e . key . toLowerCase ( ) === 'd' ) {
13291426 e . preventDefault ( ) ;
13301427 void duplicateSelection ( ) ;
0 commit comments