import { useState } from "react";
import { Form } from "react-router";
import { useTranslation } from "react-i18next";
import type { KwpSetupData, KwpSetupFeedback, KwpSetupVM } from "@hivegrid/view-models";
import { AuthShell } from "~/components/auth/AuthShell";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { ButtonGroup } from "~/ui/components/ButtonGroup";
import { EmptyState } from "~/ui/components/EmptyState";
import { LinkButton } from "~/ui/components/LinkButton";
import { RadioGroup } from "~/ui/components/RadioGroup";
import { TextInput } from "~/ui/components/TextInput";
import { kwpMandantSelectionPath, kwpSetupPath } from "~/utils/integrations/integrationsRoutePaths";
const MAX_CONTEXT_CODE_LENGTH = 128;
export type KwpSetupPageProps = {
readonly vm: KwpSetupVM;
readonly csrf: string;
readonly submitting: boolean;
};
function FeedbackAlert({ feedback }: { readonly feedback: KwpSetupFeedback }) {
const { t } = useTranslation();
if (!feedback) return null;
const key =
feedback === "configuration-error"
? "configurationError"
: feedback === "selection-error"
? "selectionError"
: feedback === "selection-expired"
? "selectionExpired"
: "error";
return (
);
}
function MandantSelectionForm({ data, csrf, submitting }: {
readonly data: Extract;
readonly csrf: string;
readonly submitting: boolean;
}) {
const { t } = useTranslation();
const onlyOption = data.discoveryMode === "available" && data.mandants.length === 1 ? data.mandants[0] : undefined;
const [choice, setChoice] = useState(onlyOption ? `selected:${onlyOption.code}` : "");
const [manualCode, setManualCode] = useState("");
const selectedCode = choice.startsWith("selected:") ? choice.slice("selected:".length) : choice === "manual" ? manualCode.trim() : "";
const selectionKind = choice === "none" ? "none" : "selected";
const canSubmit = choice === "none" || (choice.startsWith("selected:") && selectedCode.length > 0) || (choice === "manual" && selectedCode.length > 0);
const discovered = data.discoveryMode === "available" && data.mandants.length > 0;
return (
);
}
export function KwpSetupPage({ vm, csrf, submitting }: KwpSetupPageProps) {
const { t } = useTranslation();
if (vm.status !== "ready") return null;
const data = vm.data;
if (data.mode === "entry") {
if (!data.authenticated) {
return (
);
}
return (
{data.tenants.length > 0 ? (
{data.tenants.map((tenant) => (
))}
) : (
)}
);
}
if (data.mode === "mandant") {
return (
);
}
return (
);
}