feat(settings): confirmations in the shared dialog, token colours on About, scope picker in the new look

This commit is contained in:
Maurice
2026-10-01 16:19:41 +02:00
parent 56dfd56bab
commit cf3818cb66
17 changed files with 337 additions and 191 deletions
@@ -52,6 +52,13 @@ function EditFormHarness() {
return <AdminUserModals admin={admin} t={t} />;
}
/** Answers the confirm dialog: its button repeats the label and sits last in the document. */
function confirmPasskeyReset() {
expect(screen.getByText('Remove all passkeys for alice?')).toBeInTheDocument();
const buttons = screen.getAllByRole('button', { name: /reset passkeys/i });
fireEvent.click(buttons[buttons.length - 1]);
}
const editing = {
editingUser: alice,
editForm: { username: 'alice', email: 'alice@example.com', role: 'user', password: '' },
@@ -183,18 +190,31 @@ describe('AdminUserModals', () => {
expect(admin.handleSaveUser).toHaveBeenCalledTimes(1);
});
it('FE-ADMMOD-012: resetting passkeys is skipped when the confirm is declined', () => {
vi.spyOn(window, 'confirm').mockReturnValue(false);
it('FE-ADMMOD-012: resetting passkeys is skipped when the confirm is cancelled', async () => {
let calls = 0;
server.use(
http.delete('/api/admin/users/:id/passkeys', () => {
calls += 1;
return HttpResponse.json({ deleted: 1 });
})
);
const admin = renderModals(editing);
fireEvent.click(screen.getByRole('button', { name: /reset passkeys/i }));
expect(screen.getByText('Remove all passkeys for alice?')).toBeInTheDocument();
expect(window.confirm).toHaveBeenCalledWith('Remove all passkeys for alice?');
// The edit dialog has its own Cancel; the question's Cancel is the last one.
const cancels = screen.getAllByRole('button', { name: /^cancel$/i });
fireEvent.click(cancels[cancels.length - 1]);
expect(screen.queryByText('Remove all passkeys for alice?')).not.toBeInTheDocument();
await new Promise((r) => setTimeout(r, 20));
expect(calls).toBe(0);
expect(admin.toast.success).not.toHaveBeenCalled();
expect(admin.setEditingUser).not.toHaveBeenCalled();
});
it('FE-ADMMOD-013: a confirmed passkey reset reports how many were removed', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(true);
let deletedFor: string | undefined;
server.use(
http.delete('/api/admin/users/:id/passkeys', ({ params }) => {
@@ -205,17 +225,18 @@ describe('AdminUserModals', () => {
const admin = renderModals(editing);
fireEvent.click(screen.getByRole('button', { name: /reset passkeys/i }));
confirmPasskeyReset();
await waitFor(() => expect(admin.toast.success).toHaveBeenCalledWith('Removed 3 passkey(s)'));
expect(deletedFor).toBe('2');
});
it('FE-ADMMOD-014: a failing passkey reset toasts the generic error', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(true);
server.use(http.delete('/api/admin/users/:id/passkeys', () => HttpResponse.json({}, { status: 500 })));
const admin = renderModals(editing);
fireEvent.click(screen.getByRole('button', { name: /reset passkeys/i }));
confirmPasskeyReset();
await waitFor(() => expect(admin.toast.error).toHaveBeenCalledWith('Error'));
});
+23 -9
View File
@@ -13,6 +13,7 @@ import {
import React, { useId } from 'react';
import { adminApi } from '../../api/client';
import CustomSelect from '../../components/shared/CustomSelect';
import ConfirmDialog from '../../components/shared/ConfirmDialog';
import { Tooltip } from '../../components/shared/Tooltip';
import {
DialogButton,
@@ -115,6 +116,8 @@ export default function AdminUserModals({ admin, t }: AdminUserModalsProps): Rea
} = admin;
const [showCreatePw, setShowCreatePw] = React.useState(false);
const [showEditPw, setShowEditPw] = React.useState(false);
// The user whose passkeys wait for the admin's answer in the confirm dialog.
const [passkeyResetUser, setPasskeyResetUser] = React.useState<typeof editingUser>(null);
const uid = useId();
const createId = `${uid}-create`;
const editId = `${uid}-edit`;
@@ -280,15 +283,8 @@ export default function AdminUserModals({ admin, t }: AdminUserModalsProps): Rea
</p>
<button
type="button"
onClick={async () => {
if (!editingUser) return;
if (!confirm(t('admin.passkey.resetConfirm', { name: editingUser.username }))) return;
try {
const r = await adminApi.resetUserPasskeys(editingUser.id);
toast.success(t('admin.passkey.resetDone', { count: r.deleted ?? 0 }));
} catch {
toast.error(t('common.error'));
}
onClick={() => {
if (editingUser) setPasskeyResetUser(editingUser);
}}
className={`${SETTINGS_BUTTON_DANGER} flex-none`}
style={fs(12.5, 'body')}
@@ -445,6 +441,24 @@ docker run -d --name trek \\
</p>
</div>
</DialogShell>
<ConfirmDialog
isOpen={passkeyResetUser !== null}
onClose={() => setPasskeyResetUser(null)}
onConfirm={async () => {
if (!passkeyResetUser) return;
try {
const r = await adminApi.resetUserPasskeys(passkeyResetUser.id);
toast.success(t('admin.passkey.resetDone', { count: r.deleted ?? 0 }));
} catch {
toast.error(t('common.error'));
}
}}
title={t('admin.passkey.reset')}
message={passkeyResetUser ? t('admin.passkey.resetConfirm', { name: passkeyResetUser.username }) : ''}
confirmLabel={t('admin.passkey.reset')}
danger
/>
</>
);
}
+16 -2
View File
@@ -125,13 +125,27 @@ describe('AdminUsersTab', () => {
expect(admin.handleEditUser).toHaveBeenCalledWith(alice);
});
it('FE-ADMUSR-009: delete button forwards the row user to handleDeleteUser', () => {
it('FE-ADMUSR-009: delete asks first and forwards the row user to handleDeleteUser on confirm', () => {
const admin = renderTab({ users: [me, alice], currentUser: me });
const deleteButtons = screen.getAllByLabelText('Delete user');
fireEvent.click(deleteButtons[1]);
expect(admin.handleDeleteUser).toHaveBeenCalledWith(alice);
expect(admin.handleDeleteUser).not.toHaveBeenCalled();
expect(screen.getByText(`Delete user "${alice.username}"? All trips will be permanently deleted.`)).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Delete' }));
expect(admin.handleDeleteUser).toHaveBeenCalledWith(alice, { confirmed: true });
});
it('FE-ADMUSR-009b: cancelling the delete question deletes nothing', () => {
const admin = renderTab({ users: [me, alice], currentUser: me });
fireEvent.click(screen.getAllByLabelText('Delete user')[1]);
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
expect(screen.queryByText(`Delete user "${alice.username}"? All trips will be permanently deleted.`)).not.toBeInTheDocument();
expect(admin.handleDeleteUser).not.toHaveBeenCalled();
});
it('FE-ADMUSR-010: the delete button of the current user is disabled', () => {
+15 -2
View File
@@ -1,8 +1,9 @@
import React, { useId, type ReactNode } from 'react'
import React, { useId, useState, type ReactNode } from 'react'
import CustomSelect from '../../components/shared/CustomSelect'
import { Shield, Trash2, Edit2, UserPlus, Link2, Copy, Plus, Users } from 'lucide-react'
import PermissionsPanel from '../../components/Admin/PermissionsPanel'
import { Tooltip } from '../../components/shared/Tooltip'
import ConfirmDialog from '../../components/shared/ConfirmDialog'
import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../../components/shared/DialogShell'
import { EditorField, Segmented } from '../../components/shared/dialogParts'
import { SettingsCard, SettingRows, SettingsHint, StatusPill, SETTINGS_BUTTON, SETTINGS_BUTTON_PRIMARY, SETTINGS_ICON_BUTTON } from '../../components/Settings/settingsKit'
@@ -42,6 +43,8 @@ export default function AdminUsersTab({ admin, t, locale }: AdminUsersTabProps):
handleEditUser, handleDeleteUser,
} = admin
const inviteLabelId = useId()
// The row whose delete waits for the admin's answer in the confirm dialog.
const [userToDelete, setUserToDelete] = useState<(typeof users)[number] | null>(null)
return (
<>
@@ -117,7 +120,7 @@ export default function AdminUsersTab({ admin, t, locale }: AdminUsersTabProps):
<RowIconButton label={t('admin.editUser')} onClick={() => handleEditUser(u)}>
<Edit2 size={14} strokeWidth={2} />
</RowIconButton>
<RowIconButton label={t('admin.deleteUserTitle')} onClick={() => handleDeleteUser(u)} disabled={isMe} danger>
<RowIconButton label={t('admin.deleteUserTitle')} onClick={() => setUserToDelete(u)} disabled={isMe} danger>
<Trash2 size={14} strokeWidth={2} />
</RowIconButton>
</div>
@@ -253,6 +256,16 @@ export default function AdminUsersTab({ admin, t, locale }: AdminUsersTabProps):
</EditorField>
)}
</DialogShell>
<ConfirmDialog
isOpen={userToDelete !== null}
onClose={() => setUserToDelete(null)}
onConfirm={() => { if (userToDelete) void handleDeleteUser(userToDelete, { confirmed: true }) }}
title={t('admin.deleteUserTitle')}
message={userToDelete ? t('admin.deleteUser', { name: userToDelete.username }) : ''}
confirmLabel={t('common.delete')}
danger
/>
</>
)
}
+13
View File
@@ -800,6 +800,19 @@ describe('useAdmin', () => {
expect(toastCalls).toContainEqual({ type: 'success', message: 'User deleted' });
});
it('FE-ADMHOOK-042b: a delete the caller already confirmed skips the browser confirm', async () => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(false);
server.use(http.delete('/api/admin/users/:id', () => HttpResponse.json({ success: true })));
const { result } = await mountAdmin();
await act(async () => {
await result.current.handleDeleteUser(result.current.users[1], { confirmed: true });
});
expect(confirmSpy).not.toHaveBeenCalled();
expect(result.current.users.map(u => u.username)).toEqual(['admin']);
});
it('FE-ADMHOOK-043: a failing delete surfaces the server error', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(true);
server.use(http.delete('/api/admin/users/:id', () => HttpResponse.json({ error: 'busy' }, { status: 500 })));
+4 -2
View File
@@ -492,12 +492,14 @@ export function useAdmin() {
}
}
const handleDeleteUser = async (user) => {
// `confirmed` means the caller already asked in its own ConfirmDialog (the
// desktop users tab); without it the browser's confirm asks, as on mobile.
const handleDeleteUser = async (user, opts: { confirmed?: boolean } = {}) => {
if (user.id === currentUser?.id) {
toast.error(t('admin.toast.cannotDeleteSelf'))
return
}
if (!confirm(t('admin.deleteUser', { name: user.username }))) return
if (!opts.confirmed && !confirm(t('admin.deleteUser', { name: user.username }))) return
try {
await adminApi.deleteUser(user.id)
setUsers(prev => prev.filter(u => u.id !== user.id))