Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

18.9 KB
Raw Blame
import {
  useEffect,
  useRef,
  useState,
  type ComponentProps,
  type ComponentType,
} from "react";
import { useQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { SegmentedControl } from "@editor/ui/SegmentedControl";
import styles from "@editor/components/shared/setupCard/SetupCard.module.css";
import {
  SettingRow,
  SetupNote,
  initialOpenSection,
  nextOpenSection,
  sectionsConfiguredOnServer,
  sectionForField,
  type ExpandableSection,
  type SetupStepProps,
} from "@editor/components/serverSetup/SetupFields";
import { SetupStepFrame } from "@editor/components/serverSetup/SetupStepFrame";
import {
  SetupScreen,
  SetupSignInScreen,
} from "@editor/components/serverSetup/SetupScreen";
import {
  clearSetupSignIn,
  isSetupSignInPending,
} from "@editor/components/serverSetup/setupSignInHandoff";
import {
  PasswordChangeError,
  SignInAgainError,
  UsernameChangeError,
  useSetupFinish,
} from "@editor/components/serverSetup/useSetupFinish";
import { useAuth } from "@editor/auth/UseSession";
import { DEFAULT_ADMIN_CREDENTIALS } from "@editor/auth/defaultCredentials";
import {
  checkIssuer,
  fetchServerSettings,
} from "@editor/components/serverSetup/serverSetupApi";
import {
  draftFromSettings,
  draftToSave,
  issuersToCheck,
  prefillAddresses,
  stepsConfiguredOnServer,
  stepsFor,
  validateStep,
  type ServerSetupDraft,
  type SetupMode,
  type SetupStepId,
  type StepErrors,
} from "@editor/components/serverSetup/serverSetupDraft";
import { AddressesStep } from "@editor/components/serverSetup/steps/AddressesStep";
import { LanguageStep } from "@editor/components/serverSetup/steps/LanguageStep";
import { SignInStep } from "@editor/components/serverSetup/steps/SignInStep";
import { EmailStep } from "@editor/components/serverSetup/steps/EmailStep";
import { FilesStep } from "@editor/components/serverSetup/steps/FilesStep";
import { AiStep } from "@editor/components/serverSetup/steps/AiStep";
import { AnalyticsStep } from "@editor/components/serverSetup/steps/AnalyticsStep";
import { PasswordStep } from "@editor/components/serverSetup/steps/PasswordStep";
import { QuickSummaryStep } from "@editor/components/serverSetup/steps/QuickSummaryStep";
import { useStepCopy } from "@editor/components/serverSetup/useStepCopy";
import { setupAddressPrefill } from "@editor/components/serverSetup/setupAddressPrefill";

const STEP_COMPONENTS: Record<SetupStepId, ComponentType<SetupStepProps>> = {
  addresses: AddressesStep,
  language: LanguageStep,
  signIn: SignInStep,
  email: EmailStep,
  files: FilesStep,
  ai: AiStep,
  analytics: AnalyticsStep,
  password: PasswordStep,
  summary: QuickSummaryStep,
};

/** Where each server password error belongs on the Password step. */
function passwordErrors(error: PasswordChangeError): StepErrors {
  switch (error.code) {
    case "incorrectPassword":
      return { currentPassword: "passwordIncorrect" };
    case "passwordUnchanged":
      return { newPassword: "passwordUnchanged" };
    case "passwordMismatch":
      return { confirmPassword: "passwordMismatch" };
    default:
      return { newPassword: "passwordRejected" };
  }
}

function usernameErrors(error: UsernameChangeError): StepErrors {
  return {
    adminUsername:
      error.code === "usernameExists" ? "usernameTaken" : "username",
  };
}

/** The account card's progress; "updating" holds the card while the details save. */
type AccountState = "editing" | "updating" | "failed" | "signInAgain";

type FrameProps = ComponentProps<typeof SetupStepFrame>;

type Phase =
  | { kind: "password" }
  | { kind: "start" }
  | { kind: "step"; index: number }
  | { kind: "finish" };

interface ServerSetupWizardProps {
  onDone: () => void;
  passwordChange?: { usingDefaultCredentials: boolean };
  onPasswordChanged?: () => void;
}

function ServerSetupFlow({
  settings,
  onDone,
  passwordChange,
  onPasswordChanged,
}: ServerSetupWizardProps & { settings: Record<string, unknown> }) {
  // Setup's own card is up, so the sign-in card has done its job.
  useEffect(() => clearSetupSignIn(), []);
  const { t } = useTranslation();
  const stepCopy = useStepCopy();
  const [initial] = useState(() => draftFromSettings(settings));
  // Fixed when the wizard opens, so a step switched on here stays on screen.
  const [configuredSteps] = useState(() => stepsConfiguredOnServer(initial));
  const usingDefaultCredentials =
    passwordChange?.usingDefaultCredentials === true;
  const [draft, setDraft] = useState<ServerSetupDraft>(() => ({
    ...prefillAddresses(initial, setupAddressPrefill()),
    currentPassword: usingDefaultCredentials
      ? DEFAULT_ADMIN_CREDENTIALS.password
      : "",
  }));
  const [mode, setMode] = useState<SetupMode>("quick");
  // Owed account details come first, so the default login stops working
  // before the admin spends any time on settings.
  const [phase, setPhase] = useState<Phase>(
    passwordChange ? { kind: "password" } : { kind: "start" },
  );
  const [accountState, setAccountState] = useState<AccountState>("editing");
  const { signOut } = useAuth();
  const [errors, setErrors] = useState<StepErrors>({});
  const [openSection, setOpenSectionState] = useState<ExpandableSection | null>(
    null,
  );
  const [configured, setConfigured] = useState(() =>
    sectionsConfiguredOnServer(initial),
  );
  // A section reads Configured from the first time it is shut after being open.
  const setOpenSection = (next: ExpandableSection | null) => {
    if (openSection && openSection !== next) {
      setConfigured((current) => new Set(current).add(openSection));
    }
    setOpenSectionState(next);
  };
  const steps = stepsFor(mode, configuredSteps);
  const {
    state: finishState,
    finish,
    saveAccount,
    complete,
    reset,
  } = useSetupFinish({ onDone, onPasswordChanged });

  // Custom's AI card starts with AI on, so its fields are there to fill
  // in; only the first visit applies it, so switching AI off sticks.
  const aiDefaulted = useRef(false);
  // True while that default is in the draft, so a switch to Quick start can undo it.
  const aiAutoSet = useRef(false);

  const goTo = (next: Phase) => {
    setErrors({});
    setPhase(next);
    if (next.kind !== "step") return;
    const step = steps[next.index];
    let target = draft;
    if (step === "ai" && mode === "custom" && !aiDefaulted.current) {
      aiDefaulted.current = true;
      if (draft.ai === "off") {
        target = { ...draft, ai: "custom" };
        setDraft(target);
        aiAutoSet.current = true;
      }
    }
    setOpenSection(initialOpenSection(step, target, configuredSteps));
  };

  /** Errors `step` has in `section`'s fields. */
  const sectionErrors = (
    step: SetupStepId,
    section: ExpandableSection,
  ): StepErrors =>
    Object.fromEntries(
      Object.entries(validateStep(step, draft, mode)).filter(
        ([field]) =>
          sectionForField(field as keyof ServerSetupDraft) === section,
      ),
    );

  const saveSection = (section: ExpandableSection) => {
    if (phase.kind !== "step") return;
    const step = steps[phase.index];
    const found = sectionErrors(step, section);
    if (Object.keys(found).length > 0) {
      showErrors(found);
      return;
    }
    setErrors({});
    setOpenSection(nextOpenSection(step, draft, section, configuredSteps));
  };

  const stepProps: SetupStepProps = {
    draft,
    initial,
    update: (patch) => setDraft((current) => ({ ...current, ...patch })),
    errors,
    mode,
    usingDefaultCredentials,
    openSection,
    setOpenSection,
    configured,
    configuredSteps,
    saveSection,
  };

  /** Shows `stepErrors`, opening the section the first is in and focusing it. */
  const showErrors = (stepErrors: StepErrors) => {
    setErrors(stepErrors);
    const [first] = Object.keys(stepErrors) as (keyof ServerSetupDraft)[];
    const section = first ? sectionForField(first) : null;
    if (section) setOpenSection(section);
    if (first) {
      // After the render that opens its section and marks the field invalid.
      requestAnimationFrame(() =>
        document.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus(),
      );
    }
  };

  const [checkingIssuers, setCheckingIssuers] = useState(false);

  /** Errors for issuers this step would save that the server cannot reach. */
  const unreachableIssuers = async (step: SetupStepId): Promise<StepErrors> => {
    const fields = issuersToCheck(step, draft, initial);
    if (fields.length === 0) return {};
    setCheckingIssuers(true);
    try {
      const reachable = await Promise.all(
        fields.map((field) => checkIssuer(draft[field]).catch(() => false)),
      );
      return Object.fromEntries(
        fields
          .filter((_, i) => !reachable[i])
          .map((field) => [field, "issuerUnreachable"]),
      );
    } finally {
      setCheckingIssuers(false);
    }
  };

  const startFinish = (finalDraft: ServerSetupDraft = draft) => {
    goTo({ kind: "finish" });
    finish(initial, draftToSave(finalDraft, initial, mode));
  };

  const submitAccount = () => {
    if (accountState === "updating") return;
    const stepErrors = validateStep("password", draft, mode);
    showErrors(stepErrors);
    if (Object.keys(stepErrors).length > 0) return;
    setAccountState("updating");
    saveAccount(draft)
      .then(() => {
        // Nothing needs the passwords after this, so they leave memory now.
        setDraft((current) => ({
          ...current,
          currentPassword: "",
          newPassword: "",
          confirmPassword: "",
        }));
        setAccountState("editing");
        goTo({ kind: "start" });
      })
      .catch((error: unknown) => {
        if (error instanceof SignInAgainError) {
          setAccountState("signInAgain");
          return;
        }
        if (error instanceof PasswordChangeError) {
          setAccountState("editing");
          showErrors(passwordErrors(error));
        } else if (error instanceof UsernameChangeError) {
          setAccountState("editing");
          showErrors(usernameErrors(error));
        } else {
          setAccountState("failed");
        }
      });
  };

  const accountFrame = (): FrameProps => {
    switch (accountState) {
      case "updating":
        return {
          title: t(
            "serverSetup.account.updatingTitle",
            "Updating your details",
          ),
          subtitle: t("serverSetup.account.updatingSubtitle", "One moment."),
          primaryLabel: t("serverSetup.account.updating", "Updating"),
          onPrimary: () => {},
          primaryLoading: true,
        };
      case "signInAgain":
        return {
          title: t(
            "serverSetup.account.signInAgainTitle",
            "Your details are saved",
          ),
          subtitle: t(
            "serverSetup.account.signInAgainSubtitle",
            "Sign in again with your new username and password to carry on.",
          ),
          primaryLabel: t("serverSetup.account.signIn", "Sign in"),
          onPrimary: () => void signOut(),
        };
      default:
        return {
          ...stepCopy("password"),
          primaryLabel: t("serverSetup.account.save", "Save and continue"),
          onPrimary: submitAccount,
          children: (
            <>
              <PasswordStep {...stepProps} />
              {accountState === "failed" && (
                <SetupNote tone="warning">
                  <p className={styles.noteText}>
                    {t(
                      "serverSetup.account.failed",
                      "Couldn't save your details. Check the server is reachable and try again.",
                    )}
                  </p>
                </SetupNote>
              )}
            </>
          ),
        };
    }
  };

  const finishOrContinue = (isLast: boolean) =>
    isLast
      ? t("serverSetup.finishSetup", "Finish setup")
      : t("serverSetup.continue", "Continue");

  const analyticsLabel = (accept: boolean, isLast: boolean) => {
    if (accept) {
      return isLast
        ? t("serverSetup.analytics.acceptFinish", "Accept and finish")
        : t("serverSetup.analytics.accept", "Accept and continue");
    }
    return isLast
      ? t("serverSetup.analytics.declineFinish", "Decline and finish")
      : t("serverSetup.analytics.decline", "Decline and continue");
  };

  const startFrame = (): FrameProps => {
    const setupLabel = t("serverSetup.start.setupLabel", "Setup");
    return {
      title: t("serverSetup.start.title", "Your server is up"),
      subtitle: t(
        "serverSetup.start.subtitle",
        "Set it up for your team in a few steps.",
      ),
      primaryLabel: finishOrContinue(steps.length === 0),
      onPrimary: () =>
        steps.length === 0 ? startFinish() : goTo({ kind: "step", index: 0 }),
      children: (
        <SettingRow label={setupLabel}>
          {() => (
            <SegmentedControl
              fullWidth
              ariaLabel={setupLabel}
              value={mode}
              onChange={(next) => {
                setMode(next);
                if (next === "quick" && aiAutoSet.current) {
                  aiAutoSet.current = false;
                  aiDefaulted.current = false;
                  setDraft((current) => ({ ...current, ai: initial.ai }));
                }
              }}
              options={[
                {
                  value: "quick",
                  label: t("serverSetup.start.quick", "Quick start"),
                },
                {
                  value: "custom",
                  label: t("serverSetup.start.custom", "Custom"),
                },
              ]}
            />
          )}
        </SettingRow>
      ),
    };
  };

  const answerAnalytics = (accept: boolean, index: number) => {
    const answered = { ...draft, analytics: accept };
    setDraft(answered);
    if (index === steps.length - 1) startFinish(answered);
    else goTo({ kind: "step", index: index + 1 });
  };

  const stepFrame = (index: number): FrameProps => {
    const step = steps[index];
    const Step = STEP_COMPONENTS[step];
    const isLast = index === steps.length - 1;
    const card = {
      ...stepCopy(step),
      progress: { current: index + 1, total: steps.length },
      onBack: () =>
        goTo(
          index === 0 ? { kind: "start" } : { kind: "step", index: index - 1 },
        ),
      children: <Step {...stepProps} />,
    };
    if (step === "analytics") {
      return {
        ...card,
        primaryLabel: analyticsLabel(true, isLast),
        onPrimary: () => answerAnalytics(true, index),
        secondaryLabel: analyticsLabel(false, isLast),
        onSecondary: () => answerAnalytics(false, index),
      };
    }
    return {
      ...card,
      primaryLabel: finishOrContinue(isLast),
      primaryLoading: checkingIssuers,
      onPrimary: () => {
        if (checkingIssuers) return;
        const stepErrors = validateStep(step, draft, mode);
        showErrors(stepErrors);
        if (Object.keys(stepErrors).length > 0) return;
        const advance = () =>
          isLast ? startFinish() : goTo({ kind: "step", index: index + 1 });
        if (issuersToCheck(step, draft, initial).length === 0) {
          advance();
          return;
        }
        void unreachableIssuers(step).then((issuerErrors) => {
          if (Object.keys(issuerErrors).length > 0) showErrors(issuerErrors);
          else advance();
        });
      },
    };
  };

  const finishFrame = (): FrameProps => {
    switch (finishState) {
      case "restarting":
        return {
          title: t(
            "serverSetup.finish.restartingTitle",
            "Restarting your server",
          ),
          subtitle: t(
            "serverSetup.finish.restartingSubtitle",
            "This page reloads when it's back, usually within a minute.",
          ),
        };
      case "manualRestart":
      case "restartFailed":
        return {
          title: t("serverSetup.finish.manualTitle", "Settings saved"),
          subtitle:
            finishState === "manualRestart"
              ? t(
                  "serverSetup.finish.manualSubtitle",
                  "This server can't restart itself. Restart its container or service to apply them.",
                )
              : t(
                  "serverSetup.finish.restartFailedSubtitle",
                  "The server didn't restart. Restart its container or service to apply them.",
                ),
          primaryLabel: t("serverSetup.continue", "Continue"),
          onPrimary: complete,
        };
      case "error":
        return {
          title: t(
            "serverSetup.finish.errorTitle",
            "Couldn't save your settings",
          ),
          subtitle: t(
            "serverSetup.finish.errorSubtitle",
            "Nothing was restarted.",
          ),
          onBack: () => {
            reset();
            goTo({ kind: "step", index: steps.length - 1 });
          },
          primaryLabel: t("serverSetup.finish.retry", "Try again"),
          onPrimary: startFinish,
          children: (
            <SetupNote tone="warning">
              <p className={styles.noteText}>
                {t(
                  "serverSetup.finish.errorNote",
                  "Check the server is reachable and that you're still signed in as an admin.",
                )}
              </p>
            </SetupNote>
          ),
        };
      default:
        return {
          title: t("serverSetup.finish.savingTitle", "Saving your settings"),
          subtitle: t("serverSetup.finish.savingSubtitle", "One moment."),
          primaryLabel: t("serverSetup.finish.saving", "Saving"),
          onPrimary: () => {},
          primaryLoading: true,
        };
    }
  };

  const frame =
    phase.kind === "password"
      ? accountFrame()
      : phase.kind === "start"
        ? startFrame()
        : phase.kind === "finish"
          ? finishFrame()
          : stepFrame(phase.index);

  // One frame for every phase, so its height animates between them.
  return (
    <SetupScreen>
      <SetupStepFrame {...frame} />
    </SetupScreen>
  );
}

/** First-run settings for a new self-hosted server, shown once to its first admin. */
export function ServerSetupWizard({
  onDone,
  passwordChange,
  onPasswordChanged,
}: ServerSetupWizardProps) {
  const { t } = useTranslation();
  const settings = useQuery({
    queryKey: ["serverSetup", "settings"],
    queryFn: fetchServerSettings,
    staleTime: Infinity,
    retry: false,
  });

  if (settings.isPending) {
    return isSetupSignInPending() ? <SetupSignInScreen /> : null;
  }
  if (settings.isError) {
    return (
      <SetupScreen>
        <SetupStepFrame
          title={t("serverSetup.loadErrorTitle", "Couldn't load your settings")}
          subtitle={t(
            "serverSetup.loadErrorSubtitle",
            "Setup needs the server's current settings to start.",
          )}
          primaryLabel={t("serverSetup.finish.retry", "Try again")}
          onPrimary={() => void settings.refetch()}
        />
      </SetupScreen>
    );
  }
  return (
    <ServerSetupFlow
      settings={settings.data}
      onDone={onDone}
      passwordChange={passwordChange}
      onPasswordChanged={onPasswordChanged}
    />
  );
}