feat: port release-v2 experience improvements from #6582 (audited subset) (#6609)

This commit is contained in:
Thomas authored and GitHub committed 2026-08-18 09:40:02 +02:00
1 parent e3a713d2df
commit e842177d9b
163 files changed
+7390 -1240

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
// @vitest-environment jsdom
import { act } from "react";
import { createRoot } from "react-dom/client";
import type { Root } from "react-dom/client";
import { MantineProvider } from "@mantine/core";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { ConfirmModal } from "./confirm-modal";
const ConfirmModalComponent = ConfirmModal.component;
vi.mock("@homarr/translation/client", () => ({
useI18n: () => (key: string) => key,
}));
let root: Root;
let host: HTMLDivElement;
beforeEach(() => {
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
window.matchMedia = vi.fn().mockImplementation(() => ({
matches: false,
media: "",
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}));
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
});
afterEach(async () => {
await act(() => root.unmount());
host.remove();
});
describe("ConfirmModal", () => {
test("clears loading and stays open when confirmation rejects", async () => {
const closeModal = vi.fn();
let rejectConfirmation!: (error: Error) => void;
const pendingConfirmation = new Promise<void>((_resolve, reject) => {
rejectConfirmation = reject;
});
const onConfirm = vi.fn(() => pendingConfirmation);
await act(async () =>
root.render(
<MantineProvider>
<ConfirmModalComponent actions={{ closeModal }} innerProps={{ children: "Remove containers?", onConfirm }} />
</MantineProvider>,
),
);
const confirmButton = host.querySelectorAll("button").item(1);
await act(async () => confirmButton.click());
expect(confirmButton.disabled).toBe(true);
await act(async () => {
rejectConfirmation(new Error("Removal failed"));
await Promise.resolve();
});
expect(confirmButton.disabled).toBe(false);
expect(closeModal).not.toHaveBeenCalled();
});
test("waits for a rejecting confirmation button callback", async () => {
const closeModal = vi.fn();
const onConfirm = vi.fn();
let rejectClick!: (error: Error) => void;
const onClick = vi.fn(
() =>
new Promise<void>((_resolve, reject) => {
rejectClick = reject;
}),
);
await act(async () =>
root.render(
<MantineProvider>
<ConfirmModalComponent
actions={{ closeModal }}
innerProps={{ children: "Remove containers?", confirmProps: { onClick }, onConfirm }}
/>
</MantineProvider>,
),
);
const confirmButton = host.querySelectorAll("button").item(1);
await act(async () => confirmButton.click());
expect(confirmButton.disabled).toBe(true);
expect(onConfirm).not.toHaveBeenCalled();
await act(async () => {
rejectClick(new Error("Callback failed"));
await Promise.resolve();
});
expect(confirmButton.disabled).toBe(false);
expect(onConfirm).not.toHaveBeenCalled();
expect(closeModal).not.toHaveBeenCalled();
});
});
+16 -12
View File
@@ -36,8 +36,8 @@ export const ConfirmModal = createModal<Omit<ConfirmModalProps, "title">>(({ act
const closeOnConfirm = innerProps.closeOnConfirm ?? true;
const closeOnCancel = innerProps.closeOnCancel ?? true;
const cancelLabel = labels?.cancel ?? ((t: TranslationFunction) => t("common.action.cancel"));
const confirmLabel = labels?.confirm ?? ((t: TranslationFunction) => t("common.action.confirm"));
const cancelLabel = labels?.cancel ?? ((translate: TranslationFunction) => translate("common.action.cancel"));
const confirmLabel = labels?.confirm ?? ((translate: TranslationFunction) => translate("common.action.confirm"));
const handleCancel = useCallback(
async (event: React.MouseEvent<HTMLButtonElement>) => {
@@ -59,19 +59,23 @@ export const ConfirmModal = createModal<Omit<ConfirmModalProps, "title">>(({ act
const handleConfirm = useCallback(
async (event: React.MouseEvent<HTMLButtonElement>) => {
setLoading(true);
try {
if (typeof confirmProps?.onClick === "function") {
await confirmProps.onClick(event);
}
if (typeof confirmProps?.onClick === "function") {
confirmProps.onClick(event);
}
if (typeof onConfirm === "function") {
await onConfirm();
}
if (typeof onConfirm === "function") {
await onConfirm();
if (closeOnConfirm) {
actions.closeModal();
}
} catch {
// Keep the modal open. Callers own action-specific error feedback.
} finally {
setLoading(false);
}
if (closeOnConfirm) {
actions.closeModal();
}
setLoading(false);
},
[confirmProps, onConfirm, closeOnConfirm, actions],
);
+8 -1
View File
@@ -14,6 +14,8 @@ import { ConfirmModal } from "./confirm-modal";
import type { ModalsState, ModalStateWithReference } from "./reducer";
import { modalReducer } from "./reducer";
import type { inferInnerProps, ModalDefinition } from "./type";
import presentationClasses from "./modal-presentation.module.css";
import { getModalPresentationClassNames } from "./modal-presentation";
interface ModalContextProps {
openModalInner: <TModal extends ModalDefinition>(props: {
@@ -94,7 +96,11 @@ const ActiveModal = ({ modal, state, handleCloseModal }: ActiveModalProps) => {
setTimeout(() => setOpened(true), 0);
}, []);
const { defaultTitle: _ignored, ...otherModalProps } = modal.reference.modalProps;
const { defaultTitle: _ignored, presentation, ...otherModalProps } = modal.reference.modalProps;
const presentationClassNames = getModalPresentationClassNames(presentation, {
inner: presentationClasses.inspectorInner,
content: presentationClasses.inspectorContent,
});
return (
<Modal
@@ -113,6 +119,7 @@ const ActiveModal = ({ modal, state, handleCloseModal }: ActiveModalProps) => {
},
}}
trapFocus={modal.id === state.current?.id}
classNames={presentationClassNames}
{...otherModalProps}
title={translateIfNecessary(t, modal.props.defaultTitle)}
opened={opened}
@@ -0,0 +1,30 @@
.inspectorInner {
align-items: flex-end;
justify-content: center;
padding: 0;
}
.inspectorContent {
--modal-content-flex: 0 0 100%;
--modal-content-max-height: min(88dvh, 52rem);
width: 100%;
max-width: 100%;
border-radius: var(--mantine-radius-lg) var(--mantine-radius-lg) 0 0;
}
@media (min-width: 48em) {
.inspectorInner {
align-items: stretch;
justify-content: flex-end;
}
.inspectorContent {
--modal-content-flex: 0 0 min(34rem, 46vw);
--modal-content-height: 100dvh;
--modal-content-max-height: 100dvh;
width: min(34rem, 46vw);
border-radius: 0;
}
}
@@ -0,0 +1,5 @@
declare const classes: {
readonly inspectorInner: string;
readonly inspectorContent: string;
};
export default classes;
@@ -0,0 +1,19 @@
import { describe, expect, test } from "vitest";
import { getModalPresentationClassNames } from "./modal-presentation";
describe("getModalPresentationClassNames", () => {
const inspectorClassNames = { inner: "inspector-inner", content: "inspector-content" };
test("leaves the default modal presentation unchanged", () => {
expect(getModalPresentationClassNames("default", inspectorClassNames)).toBeUndefined();
expect(getModalPresentationClassNames(undefined, inspectorClassNames)).toBeUndefined();
});
test("selects the dock and sheet selectors for inspector modals", () => {
expect(getModalPresentationClassNames("inspector", inspectorClassNames)).toEqual({
inner: "inspector-inner",
content: "inspector-content",
});
});
});
+18
View File
@@ -0,0 +1,18 @@
export type ModalPresentation = "default" | "inspector";
interface InspectorClassNames {
inner: string;
content: string;
}
export const getModalPresentationClassNames = (
presentation: ModalPresentation | undefined,
inspectorClassNames: InspectorClassNames,
) => {
if (presentation !== "inspector") return undefined;
return {
inner: inspectorClassNames.inner,
content: inspectorClassNames.content,
};
};
+4
View File
@@ -3,6 +3,8 @@ import type { ModalProps } from "@mantine/core";
import type { stringOrTranslation } from "@homarr/translation";
import type { ModalPresentation } from "./modal-presentation";
export type ModalComponent<TInnerProps> = (props: {
actions: { closeModal: () => void };
innerProps: TInnerProps;
@@ -23,6 +25,7 @@ export type CreateModalOptions = Pick<
| "closeOnEscape"
> & {
defaultTitle: stringOrTranslation;
presentation?: ModalPresentation;
};
export interface ModalDefinition {
@@ -34,6 +37,7 @@ export interface ModalDefinition {
type ModalOptions<TInnerProps> = Partial<Omit<ModalProps, "opened">> & {
innerProps: TInnerProps;
defaultTitle?: stringOrTranslation;
presentation?: ModalPresentation;
};
export interface ModalState<TModal extends ModalDefinition = ModalDefinition> {