feat!: release v2 (#6545)
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386
-139176
No files matched your search
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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],
|
||||
);
|
||||
@@ -85,7 +89,13 @@ export const ConfirmModal = createModal<Omit<ConfirmModalProps, "title">>(({ act
|
||||
{cancelProps?.children ?? translateIfNecessary(t, cancelLabel)}
|
||||
</Button>
|
||||
|
||||
<Button data-autofocus {...confirmProps} onClick={handleConfirm} color="red.9" loading={loading}>
|
||||
<Button
|
||||
data-autofocus
|
||||
{...confirmProps}
|
||||
onClick={handleConfirm}
|
||||
color={confirmProps?.color ?? "red.9"}
|
||||
loading={loading}
|
||||
>
|
||||
{confirmProps?.children ?? translateIfNecessary(t, confirmLabel)}
|
||||
</Button>
|
||||
</Group>
|
||||
|
||||
@@ -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,18 @@ 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,
|
||||
});
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
const canClose = modal.reference.onCloseRequest ? modal.reference.onCloseRequest() : true;
|
||||
if (canClose) {
|
||||
handleCloseModal();
|
||||
}
|
||||
}, [handleCloseModal, modal.reference]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
@@ -113,10 +126,11 @@ const ActiveModal = ({ modal, state, handleCloseModal }: ActiveModalProps) => {
|
||||
},
|
||||
}}
|
||||
trapFocus={modal.id === state.current?.id}
|
||||
classNames={presentationClassNames}
|
||||
{...otherModalProps}
|
||||
title={translateIfNecessary(t, modal.props.defaultTitle)}
|
||||
opened={opened}
|
||||
onClose={handleCloseModal}
|
||||
onClose={handleClose}
|
||||
>
|
||||
{modal.reference.content}
|
||||
</Modal>
|
||||
@@ -125,6 +139,7 @@ const ActiveModal = ({ modal, state, handleCloseModal }: ActiveModalProps) => {
|
||||
|
||||
interface OpenModalOptions {
|
||||
keepMounted?: boolean;
|
||||
onClose?: () => void;
|
||||
title?: stringOrTranslation;
|
||||
}
|
||||
|
||||
@@ -136,7 +151,6 @@ export const useModalAction = <TModal extends ModalDefinition>(modal: TModal) =>
|
||||
return {
|
||||
openModal: (innerProps: inferInnerProps<TModal>, options: OpenModalOptions | void) => {
|
||||
// void actually is undefined
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
context.openModalInner({ modal, innerProps, options: options ?? {} });
|
||||
},
|
||||
};
|
||||
@@ -146,6 +160,7 @@ export const useConfirmModal = () => {
|
||||
const { openModal } = useModalAction(ConfirmModal);
|
||||
|
||||
return {
|
||||
openConfirmModal: (props: ConfirmModalProps) => openModal(props, { title: props.title }),
|
||||
openConfirmModal: (props: ConfirmModalProps, options?: Pick<OpenModalOptions, "onClose">) =>
|
||||
openModal(props, { title: props.title, onClose: options?.onClose }),
|
||||
};
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
interface ModalFormFooterProps {
|
||||
onCancel: () => void;
|
||||
form?: string;
|
||||
submitLabel?: string;
|
||||
cancelLabel?: string;
|
||||
loading?: boolean;
|
||||
@@ -18,24 +19,38 @@ const ActionButtons = ({
|
||||
submitLabel,
|
||||
cancelLabel,
|
||||
loading,
|
||||
}: Pick<ModalFormFooterProps, "onCancel" | "submitLabel" | "cancelLabel" | "loading">) => {
|
||||
const t = useI18n();
|
||||
form,
|
||||
}: Pick<ModalFormFooterProps, "onCancel" | "submitLabel" | "cancelLabel" | "loading" | "form">) => {
|
||||
const t = useI18n("common.action");
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button onClick={onCancel} variant="subtle" color="gray">
|
||||
{cancelLabel ?? t("common.action.cancel")}
|
||||
{cancelLabel ?? t("cancel")}
|
||||
</Button>
|
||||
<Button type="submit" loading={loading}>
|
||||
{submitLabel ?? t("common.action.saveChanges")}
|
||||
<Button type="submit" form={form} loading={loading}>
|
||||
{submitLabel ?? t("saveChanges")}
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export const ModalFormFooter = ({ onCancel, submitLabel, cancelLabel, loading, leftSection }: ModalFormFooterProps) => {
|
||||
export const ModalFormFooter = ({
|
||||
onCancel,
|
||||
form,
|
||||
submitLabel,
|
||||
cancelLabel,
|
||||
loading,
|
||||
leftSection,
|
||||
}: ModalFormFooterProps) => {
|
||||
const actionButtons = (
|
||||
<ActionButtons onCancel={onCancel} submitLabel={submitLabel} cancelLabel={cancelLabel} loading={loading} />
|
||||
<ActionButtons
|
||||
onCancel={onCancel}
|
||||
form={form}
|
||||
submitLabel={submitLabel}
|
||||
cancelLabel={cancelLabel}
|
||||
loading={loading}
|
||||
/>
|
||||
);
|
||||
|
||||
if (leftSection) {
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -94,6 +94,8 @@ const getModal = <TModal extends ModalDefinition>(modal: ModalState<TModal>) =>
|
||||
const ModalContent = modal.modal.component;
|
||||
|
||||
const { innerProps, ...rest } = modal.props;
|
||||
let closeInterceptor: (() => boolean) | null = null;
|
||||
|
||||
const FullModal = () => {
|
||||
const context = useContext(ModalContext);
|
||||
|
||||
@@ -106,6 +108,9 @@ const getModal = <TModal extends ModalDefinition>(modal: ModalState<TModal>) =>
|
||||
innerProps={innerProps}
|
||||
actions={{
|
||||
closeModal: () => context.closeModal(modal.id),
|
||||
setCloseInterceptor: (interceptor: (() => boolean) | null) => {
|
||||
closeInterceptor = interceptor;
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
@@ -114,5 +119,11 @@ const getModal = <TModal extends ModalDefinition>(modal: ModalState<TModal>) =>
|
||||
return {
|
||||
modalProps: rest,
|
||||
content: <FullModal />,
|
||||
onCloseRequest: () => {
|
||||
if (closeInterceptor) {
|
||||
return closeInterceptor();
|
||||
}
|
||||
return true;
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -3,10 +3,14 @@ import type { ModalProps } from "@mantine/core";
|
||||
|
||||
import type { stringOrTranslation } from "@homarr/translation";
|
||||
|
||||
export type ModalComponent<TInnerProps> = (props: {
|
||||
actions: { closeModal: () => void };
|
||||
innerProps: TInnerProps;
|
||||
}) => ReactNode;
|
||||
import type { ModalPresentation } from "./modal-presentation";
|
||||
|
||||
export interface ModalActions {
|
||||
closeModal: () => void;
|
||||
setCloseInterceptor?: (interceptor: (() => boolean) | null) => void;
|
||||
}
|
||||
|
||||
export type ModalComponent<TInnerProps> = (props: { actions: ModalActions; innerProps: TInnerProps }) => ReactNode;
|
||||
|
||||
export type CreateModalOptions = Pick<
|
||||
ModalOptions<unknown>,
|
||||
@@ -23,6 +27,7 @@ export type CreateModalOptions = Pick<
|
||||
| "closeOnEscape"
|
||||
> & {
|
||||
defaultTitle: stringOrTranslation;
|
||||
presentation?: ModalPresentation;
|
||||
};
|
||||
|
||||
export interface ModalDefinition {
|
||||
@@ -34,6 +39,7 @@ export interface ModalDefinition {
|
||||
type ModalOptions<TInnerProps> = Partial<Omit<ModalProps, "opened">> & {
|
||||
innerProps: TInnerProps;
|
||||
defaultTitle?: stringOrTranslation;
|
||||
presentation?: ModalPresentation;
|
||||
};
|
||||
|
||||
export interface ModalState<TModal extends ModalDefinition = ModalDefinition> {
|
||||
|
||||
Reference in new issue
Block a user