feat: Self hosted onboarding (#22102)
* intro work * update wixard form to have content callback to remove preset navigation * more fixes to deployment * fix calling static service * fix save license key text * ensure default steps work as expected * fix conditional for rendering step * skip step * add on next step for free license * refactor wizard form to use nuqs * fix styles * merge base param with step config * fix next stepo text * use deployment Signature token * decrypt signature token * fix: resolve type errors and test failures from wizard form refactor - Fix signatureToken field name to signatureTokenEncrypted in deployment repository - Add missing getSignatureToken method to verifyApiKey test mock - Fix WizardForm import from default to named export in test file - Add missing nextStep prop to Steps component in WizardForm Resolves TypeScript type check errors and unit test failures without changing functionality. Co-Authored-By: [email protected] <[email protected]> * fix: add missing getDeploymentSignatureToken mock in LicenseKeyService test Co-Authored-By: [email protected] <[email protected]> * fix: add nuqs library mock for WizardForm test Co-Authored-By: [email protected] <[email protected]> * fix: add missing nav prop to AdminAppsList component with eslint disable Co-Authored-By: [email protected] <[email protected]> * Apply suggestions from code review Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update apps/web/modules/auth/setup-view.tsx Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix license schema changes * revret schema generation * fix eslint errors * remove required nav type + add use client * fix types * Update packages/ui/components/form/wizard/useWizardState.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix controller issue * add checks for deployment key being null - add more tests * fix tests * add deployment key tests * fix: resolve crypto mock to handle empty encryption keys gracefully - Updated symmetricDecrypt mock to return null instead of throwing 'Invalid key' error when encryption key is empty - All getDeploymentKey tests now pass including the previously failing 'should return null when decryption fails due to missing encryption key' test - Fixes mocking issues in PR 22102 self-hosted onboarding wizard form refactor Co-Authored-By: [email protected] <[email protected]> * fix label * add i18n to error * use enum for steps * add as const * fix test env issues --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
36 files changed
+982
-263
No files matched your search
@@ -1,49 +1,57 @@
|
||||
"use client";
|
||||
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { noop } from "lodash";
|
||||
import { useRouter } from "next/navigation";
|
||||
import type { Dispatch, SetStateAction } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { z } from "zod";
|
||||
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
import { Button } from "../../button";
|
||||
import { Steps } from "../../form/step";
|
||||
import { useWizardState } from "./useWizardState";
|
||||
|
||||
type DefaultStep = {
|
||||
export type WizardStep = {
|
||||
title: string;
|
||||
containerClassname?: string;
|
||||
contentClassname?: string;
|
||||
description: string;
|
||||
content?: ((setIsPending: Dispatch<SetStateAction<boolean>>) => JSX.Element) | JSX.Element;
|
||||
content?:
|
||||
| ((
|
||||
setIsPending: Dispatch<SetStateAction<boolean>>,
|
||||
nav: { onNext: () => void; onPrev: () => void; step: number; maxSteps: number }
|
||||
) => JSX.Element)
|
||||
| JSX.Element;
|
||||
isEnabled?: boolean;
|
||||
isPending?: boolean;
|
||||
customActions?: boolean;
|
||||
};
|
||||
|
||||
function WizardForm<T extends DefaultStep>(props: {
|
||||
href: string;
|
||||
steps: T[];
|
||||
disableNavigation?: boolean;
|
||||
export interface WizardFormProps {
|
||||
steps: WizardStep[];
|
||||
containerClassname?: string;
|
||||
prevLabel?: string;
|
||||
nextLabel?: string;
|
||||
finishLabel?: string;
|
||||
stepLabel?: React.ComponentProps<typeof Steps>["stepLabel"];
|
||||
}) {
|
||||
const searchParams = useCompatSearchParams();
|
||||
const { href, steps, nextLabel = "Next", finishLabel = "Finish", prevLabel = "Back", stepLabel } = props;
|
||||
const router = useRouter();
|
||||
const stepSchema = z.coerce.number().int().min(1).max(steps.length).default(1);
|
||||
const stepResult = stepSchema.safeParse(searchParams?.get("step"));
|
||||
const step = stepResult.success ? stepResult.data : 1;
|
||||
const currentStep = steps[step - 1];
|
||||
const setStep = (newStep: number) => {
|
||||
router.replace(`${href}?step=${newStep || 1}`);
|
||||
};
|
||||
defaultStep?: number;
|
||||
disableNavigation?: boolean;
|
||||
}
|
||||
|
||||
export function WizardForm({
|
||||
steps,
|
||||
containerClassname,
|
||||
prevLabel = "Back",
|
||||
nextLabel = "Next",
|
||||
finishLabel = "Finish",
|
||||
stepLabel,
|
||||
defaultStep = 1,
|
||||
disableNavigation = false,
|
||||
}: WizardFormProps) {
|
||||
const { currentStep, maxSteps, nextStep, prevStep, isFirstStep, isLastStep } = useWizardState(
|
||||
defaultStep,
|
||||
steps.length
|
||||
);
|
||||
const [currentStepisPending, setCurrentStepisPending] = useState(false);
|
||||
const currentStepData = steps[currentStep - 1];
|
||||
|
||||
useEffect(() => {
|
||||
setCurrentStepisPending(false);
|
||||
@@ -51,39 +59,45 @@ function WizardForm<T extends DefaultStep>(props: {
|
||||
|
||||
return (
|
||||
<div className="mx-auto mt-4 print:w-full" data-testid="wizard-form">
|
||||
<div className={classNames("overflow-hidden md:mb-2 md:w-[700px]", props.containerClassname)}>
|
||||
<div className="px-6 py-5 sm:px-14">
|
||||
<div className={classNames("overflow-hidden md:mb-2 md:w-[700px]", containerClassname)}>
|
||||
<div className="px-6 py-5">
|
||||
<h1 className="font-cal text-emphasis text-2xl" data-testid="step-title">
|
||||
{currentStep.title}
|
||||
{currentStepData.title}
|
||||
</h1>
|
||||
<p className="text-subtle text-sm" data-testid="step-description">
|
||||
{currentStep.description}
|
||||
{currentStepData.description}
|
||||
</p>
|
||||
{!props.disableNavigation && (
|
||||
{!disableNavigation && (
|
||||
<Steps
|
||||
maxSteps={steps.length}
|
||||
currentStep={step}
|
||||
nextStep={noop}
|
||||
maxSteps={maxSteps}
|
||||
currentStep={currentStep}
|
||||
nextStep={nextStep}
|
||||
stepLabel={stepLabel}
|
||||
data-testid="wizard-step-component"
|
||||
disableNavigation={disableNavigation}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className={classNames("mb-8 overflow-hidden md:w-[700px]", props.containerClassname)}>
|
||||
<div className={classNames("print:p-none max-w-3xl px-8 py-5 sm:p-6", currentStep.contentClassname)}>
|
||||
{typeof currentStep.content === "function"
|
||||
? currentStep.content(setCurrentStepisPending)
|
||||
: currentStep.content}
|
||||
<div className={classNames("mb-8 overflow-hidden md:w-[700px]", containerClassname)}>
|
||||
<div
|
||||
className={classNames(
|
||||
"bg-default border-subtle max-w-3xl rounded-2xl border px-4 py-3 sm:p-4 ",
|
||||
currentStepData.contentClassname
|
||||
)}>
|
||||
{typeof currentStepData.content === "function"
|
||||
? currentStepData.content(setCurrentStepisPending, {
|
||||
onNext: nextStep,
|
||||
onPrev: prevStep,
|
||||
step: currentStep,
|
||||
maxSteps,
|
||||
})
|
||||
: currentStepData.content}
|
||||
</div>
|
||||
{!props.disableNavigation && (
|
||||
{!disableNavigation && !currentStepData.customActions && (
|
||||
<div className="flex justify-end px-4 py-4 print:hidden sm:px-6">
|
||||
{step > 1 && (
|
||||
<Button
|
||||
color="secondary"
|
||||
onClick={() => {
|
||||
setStep(step - 1);
|
||||
}}>
|
||||
{!isFirstStep && (
|
||||
<Button color="secondary" onClick={prevStep}>
|
||||
{prevLabel}
|
||||
</Button>
|
||||
)}
|
||||
@@ -93,10 +107,10 @@ function WizardForm<T extends DefaultStep>(props: {
|
||||
loading={currentStepisPending}
|
||||
type="submit"
|
||||
color="primary"
|
||||
form={`wizard-step-${step}`}
|
||||
disabled={currentStep.isEnabled === false}
|
||||
form={`wizard-step-${currentStep}`}
|
||||
disabled={currentStepData.isEnabled === false}
|
||||
className="relative ml-2">
|
||||
{step < steps.length ? nextLabel : finishLabel}
|
||||
{isLastStep ? finishLabel : nextLabel}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
@@ -104,5 +118,3 @@ function WizardForm<T extends DefaultStep>(props: {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default WizardForm;
|
||||
@@ -1 +1,4 @@
|
||||
export { default as WizardForm } from "./WizardForm";
|
||||
export { WizardForm } from "./WizardForm";
|
||||
export type { WizardFormProps, WizardStep } from "./WizardForm";
|
||||
export { useWizardState } from "./useWizardState";
|
||||
export type { WizardState } from "./useWizardState";
|
||||
@@ -0,0 +1,54 @@
|
||||
import { createParser, useQueryState } from "nuqs";
|
||||
import { useCallback } from "react";
|
||||
|
||||
export interface WizardState {
|
||||
currentStep: number;
|
||||
maxSteps: number;
|
||||
}
|
||||
|
||||
const createStepParser = (defaultStep: number, maxSteps: number) =>
|
||||
createParser({
|
||||
parse: (value: string) => {
|
||||
const parsed = parseInt(value, 10);
|
||||
if (isNaN(parsed)) return defaultStep;
|
||||
// Ensure step is within bounds
|
||||
if (parsed < 1) return 1;
|
||||
if (parsed > maxSteps) return maxSteps;
|
||||
return parsed;
|
||||
},
|
||||
serialize: (value: number) => value.toString(),
|
||||
});
|
||||
|
||||
export function useWizardState(defaultStep = 1, maxSteps: number) {
|
||||
const stepParser = createStepParser(defaultStep, maxSteps);
|
||||
const [step, setStep] = useQueryState("step", stepParser.withDefault(defaultStep));
|
||||
|
||||
const goToStep = useCallback(
|
||||
(newStep: number) => {
|
||||
setStep(Math.min(Math.max(newStep, 1), maxSteps));
|
||||
},
|
||||
[setStep, maxSteps]
|
||||
);
|
||||
|
||||
const nextStep = useCallback(() => {
|
||||
if (step < maxSteps) {
|
||||
setStep((prev) => prev + 1);
|
||||
}
|
||||
}, [step, maxSteps, setStep]);
|
||||
|
||||
const prevStep = useCallback(() => {
|
||||
if (step > 1) {
|
||||
setStep((prev) => prev - 1);
|
||||
}
|
||||
}, [step, setStep]);
|
||||
|
||||
return {
|
||||
currentStep: step,
|
||||
maxSteps,
|
||||
goToStep,
|
||||
nextStep,
|
||||
prevStep,
|
||||
isFirstStep: step === 1,
|
||||
isLastStep: step === maxSteps,
|
||||
};
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
import { render, waitFor } from "@testing-library/react";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import WizardForm from "./WizardForm";
|
||||
import { WizardForm } from "./WizardForm";
|
||||
|
||||
vi.mock("@calcom/lib/hooks/useCompatSearchParams", () => ({
|
||||
useCompatSearchParams() {
|
||||
@@ -19,6 +19,13 @@ vi.mock("next/navigation", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("nuqs", () => ({
|
||||
useQueryState: vi.fn(() => [currentStepNavigation, vi.fn()]),
|
||||
createParser: vi.fn(() => ({
|
||||
withDefault: vi.fn(() => ({})),
|
||||
})),
|
||||
}));
|
||||
|
||||
const steps = [
|
||||
{
|
||||
title: "Step 1",
|
||||
|
||||
Reference in new issue
Block a user