Skip to content

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

2.1 KB
Raw Blame
import { useState } from "react";
import { Stack, Text } from "@mantine/core";
import { useTranslation } from "react-i18next";
import { Button } from "@editor/ui/Button";
import { Banner } from "@editor/ui/Banner";
import { openExternal } from "@editor/platform/openExternal";
import { connectionModeService } from "@editor/services/connectionModeService";
import { webPageUrl } from "@editor/utils/webPageUrl";

/** Billing runs on the web because desktop does not ship the processor's billing UI. */
export function BillingSettingsSection({
  mode,
}: {
  mode: "saas" | "selfhosted";
}) {
  const { t } = useTranslation();
  const [opening, setOpening] = useState(false);
  const [failed, setFailed] = useState(false);

  async function openBilling() {
    setOpening(true);
    setFailed(false);
    try {
      const connection = await connectionModeService.getCurrentConfig();
      if (connection.mode !== mode) throw new Error("Connection changed");
      const base =
        mode === "saas"
          ? import.meta.env.VITE_SAAS_FRONTEND_URL
          : connection.server_config?.url;
      await openExternal(webPageUrl(base, "settings/billing"));
    } catch {
      setFailed(true);
    } finally {
      setOpening(false);
    }
  }

  return (
    <Stack align="flex-start">
      <Text>
        {mode === "selfhosted"
          ? t(
              "desktopBilling.selfHostedDescription",
              "Manage usage and billing on your connected server in your browser. Sign in with your server owner account.",
            )
          : t(
              "desktopBilling.description",
              "Manage usage and billing in your browser. Sign in with the same Stirling account you use here.",
            )}
      </Text>
      {failed && (
        <Banner tone="danger">
          {t(
            "desktopBilling.openFailed",
            "Could not open Usage & Billing. Check your connection and try again.",
          )}
        </Banner>
      )}
      <Button loading={opening} onClick={() => void openBilling()}>
        {t("desktopBilling.open", "Open Usage & Billing in browser")}
      </Button>
    </Stack>
  );
}