Skip to content

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

1.6 KB
Raw Blame
import { useAppConfig } from "@editor/contexts/AppConfigContext";
import { useSelfHostedAuth } from "@editor/hooks/useSelfHostedAuth";
import { useServerExperience } from "@editor/hooks/useServerExperience";
import {
  useChecklistInviteTarget as useCloudInviteTarget,
  type ChecklistInviteTarget,
} from "@editor-cloud/components/onboarding/checklistInviteTarget";

export type { ChecklistInviteTarget } from "@editor-cloud/components/onboarding/checklistInviteTarget";

/** Cloud team leaders invite from Users; self-hosted admins from People, once login
 * is on and they are still the server's only user. Guests are excluded by the Cloud
 * check and are never self-hosted admins. */
export function useChecklistInviteTarget(): ChecklistInviteTarget {
  const cloudTarget = useCloudInviteTarget();
  const { isSelfHosted, isAuthenticated } = useSelfHostedAuth();
  const { config } = useAppConfig();
  const { totalUsers, userCountSource, userCountError } = useServerExperience();
  if (isSelfHosted) {
    const isAdmin = Boolean(
      isAuthenticated && config?.enableLogin && config.isAdmin,
    );
    // A successful fetch sets the source, a failed one the error; a refresh keeps both.
    const countPending = userCountSource === "unknown" && !userCountError;
    // Only the admin count is exact; the WAU fallback estimates browsers, not accounts.
    const hasOtherUsers = userCountSource === "admin" && (totalUsers ?? 0) > 1;
    return {
      loading: isAdmin && countPending,
      target: isAdmin && !hasOtherUsers ? "people" : null,
    };
  }
  return cloudTarget;
}