Skip to content

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

2.1 KB
Raw Blame
import { useEffect } from "react";
import type { ServerSetupGateProps } from "@editor-core/components/startup/ServerSetupGate";
import { useAppConfig } from "@editor/contexts/AppConfigContext";
import { ServerSetupWizard } from "@editor/components/serverSetup/ServerSetupWizard";
import { SetupSignInScreen } from "@editor/components/serverSetup/SetupScreen";
import {
  clearSetupSignIn,
  isSetupSignInPending,
} from "@editor/components/serverSetup/setupSignInHandoff";

/** Pending for an admin of a server with login on that hasn't finished setup;
 *  never without login, nor for an older server that sends no flag. */
export function useServerSetupPending(): boolean {
  const { config } = useAppConfig();
  return (
    config?.enableLogin === true &&
    config.isAdmin === true &&
    config.setupCompleted === false
  );
}

/** Keeps the automatic first sign-in's card up while startup checks load. */
export function ServerSetupLoading() {
  return isSetupSignInPending() ? <SetupSignInScreen /> : null;
}

export function ServerSetupGate({
  children,
  passwordChange,
  onPasswordChanged,
}: ServerSetupGateProps) {
  const pending = useServerSetupPending();
  const { config, loading, refetch } = useAppConfig();
  // The default login is an admin, so a config without isAdmin is still the
  // one from before sign-in.
  const signedInConfig = !loading && config?.isAdmin === true;
  // Straight after the automatic first sign-in, hold its card until the
  // signed-in config arrives rather than flash the editor, then let go.
  const holdingSignIn = !pending && !signedInConfig && isSetupSignInPending();

  useEffect(() => {
    if (signedInConfig && !pending) clearSetupSignIn();
  }, [signedInConfig, pending]);

  // Up until the server reports setup complete, never on a local flag: showing
  // the app early would let the default password through.
  if (holdingSignIn) return <SetupSignInScreen />;
  if (!pending) return <>{children}</>;
  return (
    <ServerSetupWizard
      passwordChange={passwordChange}
      onPasswordChanged={onPasswordChanged}
      onDone={() => void refetch()}
    />
  );
}