Skip to content

Commit c993f7c

Browse files
feat(drive): undo/redo for move, delete and rename (Ctrl+Z / Ctrl+Y) (#6)
A bounded per-space history (30 actions) makes the common mistakes reversible from the keyboard: - Delete → Trash: undo restores from the Trash, redo re-trashes. - Move (single or bulk): undo moves items back to their previous folder. - Rename: undo restores the previous name. Ctrl+Z undoes, Ctrl+Y (or Ctrl+Shift+Z) redoes; both are in the shortcuts help. History clears when switching spaces. Zero-Knowledge deletes are permanent (no Trash) so they are intentionally not tracked.
1 parent ef3137a commit c993f7c

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎apps/web/src/app/(app)/drive/page.tsx‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

‎apps/web/src/components/drive/ShortcutsHelp.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export function ShortcutsHelp({ onClose }: { onClose: () => void }) {
3434
{ keys: [m, 'C'], label: t('drive.scCopy') },
3535
{ keys: [m, 'X'], label: t('drive.scCut') },
3636
{ keys: [m, 'V'], label: t('drive.scPaste') },
37+
{ keys: [m, 'Z'], label: t('drive.scUndo') },
38+
{ keys: [m, 'Y'], label: t('drive.scRedo') },
3739
{ keys: [m, 'D'], label: t('drive.scDuplicate') },
3840
{ keys: ['Esc'], label: t('drive.scClear') },
3941
{ keys: ['?'], label: t('drive.scHelp') },

‎apps/web/src/lib/i18n.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ const fr: Dict = {
169169
'drive.spaceRoot': 'Racine de l’espace',
170170
'drive.moveTitle': 'Déplacer vers',
171171
'drive.moved': 'Déplacé',
172+
'drive.undone': 'Action annulée',
173+
'drive.redone': 'Action rétablie',
174+
'drive.scUndo': 'Annuler (déplacer / supprimer / renommer)',
175+
'drive.scRedo': 'Rétablir',
172176
// drive: share
173177
'drive.shareTitle': 'Partager',
174178
'drive.shareMsg': 'Qui peut ouvrir ce lien ?',
@@ -797,6 +801,10 @@ const en: Dict = {
797801
'drive.spaceRoot': 'Space root',
798802
'drive.moveTitle': 'Move to',
799803
'drive.moved': 'Moved',
804+
'drive.undone': 'Undone',
805+
'drive.redone': 'Redone',
806+
'drive.scUndo': 'Undo (move / delete / rename)',
807+
'drive.scRedo': 'Redo',
800808
'drive.shareTitle': 'Share',
801809
'drive.shareMsg': 'Who can open this link?',
802810
'drive.shareEveryone': 'Everyone',

0 commit comments

Comments
 (0)