Skip to content

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

7.9 KB
Raw Blame
import { useTranslation } from "react-i18next";
import { Icon } from "@editor/ui/Icon";
import { type OAuthProvider } from "@editor/auth/spring/oauthTypes";
import { Button as DSButton } from "@editor/ui/Button";
import { GENERIC_PROVIDER_ICON } from "@editor/auth/ui/oauthIcons";
import { ProviderMark } from "@editor/auth/ui/ProviderMark";

// Debug flag to show all providers for UI testing
// Set to true to see all SSO options regardless of backend configuration
export const DEBUG_SHOW_ALL_PROVIDERS = false;

// OAuth provider configuration - maps provider ID to display info
// Known providers get custom icons; unknown providers use generic SSO icon
export const oauthProviderConfig: Record<
  string,
  { label: string; file: string }
> = {
  google: { label: "Google", file: "google.svg" },
  github: { label: "GitHub", file: "github.svg" },
  apple: { label: "Apple", file: "apple.svg" },
  azure: { label: "Microsoft", file: "microsoft.svg" },
  keycloak: { label: "Keycloak", file: "keycloak.svg" },
  cloudron: { label: "Cloudron", file: "cloudron.svg" },
  authentik: { label: "Authentik", file: "authentik.svg" },
  oidc: { label: "OIDC", file: "oidc.svg" },
};

// Icon URLs + GENERIC_PROVIDER_ICON come from the shared oauthIcons resolver.
// Every provider icon is decorative (alt=""): the button it sits in already
// names the provider, so alt text would only repeat that name.

interface OAuthButtonsProps {
  onProviderClick: (provider: OAuthProvider) => void;
  isSubmitting: boolean;
  /**
   * `fullwidth` is the SaaS-login canonical look — rounded pill buttons stacked
   * vertically. `vertical` (default) uses the Spring/Mantine button shape.
   */
  layout?: "vertical" | "grid" | "icons" | "fullwidth";
  enabledProviders?: OAuthProvider[]; // List of full auth paths from backend (e.g., '/oauth2/authorization/google', '/saml2/authenticate/stirling')
  ctaPrefix?: string;
  styleVariant?: "neutral" | "tinted" | "outline" | "light";
  demoMode?: boolean;
  useNewStyle?: boolean;
}

export default function OAuthButtons({
  onProviderClick,
  isSubmitting,
  layout = "vertical",
  enabledProviders = [],
  ctaPrefix,
  styleVariant = "neutral",
  demoMode = false,
  useNewStyle = false,
}: OAuthButtonsProps) {
  const { t } = useTranslation();

  // Debug mode: show all providers for UI testing
  const providersToShow = DEBUG_SHOW_ALL_PROVIDERS
    ? Object.keys(oauthProviderConfig)
    : enabledProviders;

  // Build provider list - extract provider ID from full path for display
  const providers = providersToShow.map((pathOrId) => {
    // Extract provider ID from full path (e.g., '/saml2/authenticate/stirling' -> 'stirling')
    const providerId = pathOrId.split("/").pop() || pathOrId;

    if (providerId in oauthProviderConfig) {
      // Known provider - use predefined icon and label
      return {
        id: pathOrId, // Keep full path for redirect
        providerId, // Store extracted ID for display lookup
        ...oauthProviderConfig[providerId],
      };
    }
    // Unknown provider - use generic icon and capitalize ID for label
    return {
      id: pathOrId, // Keep full path for redirect
      providerId, // Store extracted ID for display lookup
      label: providerId.charAt(0).toUpperCase() + providerId.slice(1),
      file: GENERIC_PROVIDER_ICON,
    };
  });

  // If no providers are enabled, don't render anything
  if (providers.length === 0) {
    return null;
  }

  const isSingleProvider = providers.length === 1;
  const isTinted = styleVariant === "tinted";
  const isOutline = styleVariant === "outline";
  const isLight = styleVariant === "light";
  const accentMap: Record<string, string> = {
    google: "#4285F4",
    github: "#111827",
    apple: "#111827",
    azure: "#0078D4",
    keycloak: "#2C2C2C",
    cloudron: "#3B82F6",
    authentik: "#FA7B17",
    oidc: "#334155",
  };

  if (layout === "icons") {
    return (
      <div className="oauth-container-icons">
        {providers.map((p) => (
          <div
            key={p.id}
            title={`${t("login.signInWith", "Sign in with")} ${p.label}`}
          >
            <DSButton
              onClick={() => onProviderClick(p.id)}
              disabled={isSubmitting}
              className="oauth-button-icon"
              aria-label={`${t("login.signInWith", "Sign in with")} ${p.label}`}
              variant="tertiary"
            >
              <ProviderMark file={p.file} className="oauth-icon-small" />
            </DSButton>
          </div>
        ))}
      </div>
    );
  }
  if (layout === "grid") {
    return (
      <div className="oauth-container-grid">
        {providers.map((p) => (
          <div
            key={p.id}
            title={`${t("login.signInWith", "Sign in with")} ${p.label}`}
          >
            <DSButton
              onClick={() => onProviderClick(p.id)}
              disabled={isSubmitting}
              className="oauth-button-grid"
              aria-label={`${t("login.signInWith", "Sign in with")} ${p.label}`}
              variant="tertiary"
            >
              <ProviderMark file={p.file} className="oauth-icon-medium" />
            </DSButton>
          </div>
        ))}
      </div>
    );
  }
  if (layout === "fullwidth") {
    // Mirrors the SaaS editor login: rounded pill buttons stacked vertically,
    // each with the provider's icon + "Sign in with X" label.
    return (
      <div className="oauth-container-fullwidth">
        {providers.map((p) => (
          <DSButton
            key={p.id}
            variant="secondary"
            onClick={() => onProviderClick(p.id)}
            disabled={isSubmitting}
            className="oauth-button-fullwidth"
            title={p.label}
            aria-label={ctaPrefix ? `${ctaPrefix} ${p.label}` : p.label}
          >
            <span className="oauth-btn-group">
              <ProviderMark
                file={p.file}
                className={`oauth-icon-medium oauth-icon--${p.providerId}`}
                style={{ marginRight: "0.5rem", flexShrink: 0 }}
              />
              <span className="oauth-btn-label">
                {ctaPrefix ? `${ctaPrefix} ${p.label}` : p.label}
              </span>
            </span>
          </DSButton>
        ))}
      </div>
    );
  }

  return (
    <div
      className={`oauth-container-vertical${useNewStyle && isSingleProvider ? " oauth-container-single" : ""}`}
    >
      {providers.map((p) => (
        <div
          key={p.id}
          title={`${t("login.signInWith", "Sign in with")} ${p.label}`}
        >
          <DSButton
            onClick={() => onProviderClick(p.id)}
            disabled={!demoMode && isSubmitting}
            className={`oauth-button-vertical${useNewStyle && isSingleProvider ? " oauth-button-vertical-single" : ""}${!useNewStyle ? " oauth-button-vertical-legacy" : ""}${isTinted ? " oauth-button-vertical-tinted" : ""}${isOutline ? " oauth-button-vertical-outline" : ""}${isLight ? " oauth-button-vertical-light" : ""}`}
            aria-label={`${t("login.signInWith", "Sign in with")} ${p.label}`}
            variant="tertiary"
            style={
              isTinted
                ? ({
                    "--oauth-accent": accentMap[p.providerId] || "#334155",
                  } as React.CSSProperties)
                : undefined
            }
          >
            <span className="oauth-button-left">
              <span className="oauth-icon-wrapper">
                <ProviderMark file={p.file} className="oauth-icon-tiny" />
              </span>
              <span className="oauth-button-text">
                {ctaPrefix ? `${ctaPrefix} ${p.label}` : p.label}
              </span>
            </span>
            {useNewStyle && isSingleProvider && (
              <span className="oauth-button-right" aria-hidden="true">
                <Icon
                  name="arrow-right"
                  strokeWidth={2}
                  className="oauth-arrow-icon"
                />
              </span>
            )}
          </DSButton>
        </div>
      ))}
    </div>
  );
}