This commit is contained in:
1 parent
e3a713d2df
commit
e842177d9b
163 files changed
+7390
-1240
No files matched your search
@@ -3,6 +3,7 @@ import { z } from "zod/v4";
|
||||
|
||||
import type { RouterOutputs } from "@homarr/api";
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { buildDockerServiceUrlCandidates } from "@homarr/definitions";
|
||||
import { useZodForm } from "@homarr/form";
|
||||
import { createModal } from "@homarr/modals";
|
||||
import { showErrorNotification, showSuccessNotification } from "@homarr/notifications";
|
||||
@@ -10,6 +11,7 @@ import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
interface AddDockerAppToHomarrProps {
|
||||
selectedContainers: RouterOutputs["docker"]["getContainers"]["containers"];
|
||||
initialUrls?: string[];
|
||||
}
|
||||
|
||||
export const AddDockerAppToHomarrModal = createModal<AddDockerAppToHomarrProps>(({ actions, innerProps }) => {
|
||||
@@ -20,12 +22,17 @@ export const AddDockerAppToHomarrModal = createModal<AddDockerAppToHomarrProps>(
|
||||
}),
|
||||
{
|
||||
initialValues: {
|
||||
containerUrls: innerProps.selectedContainers.map((container) => {
|
||||
if (Array.isArray(container.ports) && container.ports[0]) {
|
||||
return `http://${container.ports[0].IP}:${container.ports[0].PublicPort}`;
|
||||
}
|
||||
containerUrls: innerProps.selectedContainers.map((container, index) => {
|
||||
const initialUrl = innerProps.initialUrls?.[index];
|
||||
if (initialUrl) return initialUrl;
|
||||
|
||||
return null;
|
||||
const candidates = buildDockerServiceUrlCandidates({
|
||||
containerName: container.name,
|
||||
endpointHost: container.host,
|
||||
ports: container.ports,
|
||||
}).filter(({ url }) => url.length > 0);
|
||||
|
||||
return candidates.find(({ scopes }) => scopes.includes("browser"))?.url ?? candidates[0]?.url ?? null;
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDockerRemovalConfirmation } from "./confirm-docker-container-removal";
|
||||
|
||||
describe("createDockerRemovalConfirmation", () => {
|
||||
test("does not remove containers before explicit confirmation", () => {
|
||||
const remove = vi.fn();
|
||||
const t = vi.fn((key: string, values?: Record<string, string>) => `${key}:${JSON.stringify(values)}`);
|
||||
|
||||
const confirmation = createDockerRemovalConfirmation([{ name: "sonarr" }, { name: "radarr" }], t as never, remove);
|
||||
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
expect(confirmation.title).toContain('"count":"2"');
|
||||
expect(confirmation.children).toContain('"names":"sonarr, radarr"');
|
||||
|
||||
confirmation.onConfirm();
|
||||
expect(remove).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
|
||||
import { useConfirmModal } from "@homarr/modals";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
interface DockerContainerName {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export const createDockerRemovalConfirmation = (
|
||||
containers: DockerContainerName[],
|
||||
t: ReturnType<typeof useScopedI18n<"docker.action.remove">>,
|
||||
onConfirm: () => void | Promise<void>,
|
||||
) => ({
|
||||
title: t("confirmation.title", { count: String(containers.length) }),
|
||||
children: t("confirmation.message", {
|
||||
count: String(containers.length),
|
||||
names: containers.map(({ name }) => name).join(", "),
|
||||
}),
|
||||
confirmProps: { children: t("label"), color: "red.9" },
|
||||
onConfirm,
|
||||
});
|
||||
|
||||
export const useDockerContainerRemovalConfirmation = () => {
|
||||
const t = useScopedI18n("docker.action.remove");
|
||||
const { openConfirmModal } = useConfirmModal();
|
||||
|
||||
return useCallback(
|
||||
(containers: DockerContainerName[], onConfirm: () => void | Promise<void>) => {
|
||||
openConfirmModal(createDockerRemovalConfirmation(containers, t, onConfirm));
|
||||
},
|
||||
[openConfirmModal, t],
|
||||
);
|
||||
};
|
||||
@@ -1 +1,5 @@
|
||||
export { AddDockerAppToHomarrModal as AddDockerAppToHomarr } from "./add-docker-app-to-homarr";
|
||||
export {
|
||||
createDockerRemovalConfirmation,
|
||||
useDockerContainerRemovalConfirmation,
|
||||
} from "./confirm-docker-container-removal";
|
||||
Reference in new issue
Block a user