#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { useServerPlan } from "@editor/portal/hooks/useServerPlan";
import { ManageBillingButton } from "@editor/components/shared/ManageBillingButton";
import { useCallback, useEffect, useRef } from "react";
import { Navigate, useSearchParams } from "react-router-dom";
import { ServerLicenseSection } from "@editor/portal/components/billing/ServerLicenseSection";
import {
useApplyLinkFacts,
useLinkOptional,
} from "@editor/portal/contexts/LinkContext";
import { useConnectGate } from "@editor/portal/hooks/useConnectGate";
import { useAccountLinkOwner } from "@editor/portal/hooks/useAccountLinkOwner";
import { AccountConnectionNotice } from "@editor/portal/components/account-link/AccountConnectionNotice";
import { SaasSessionBanner } from "@editor/portal/components/account-link/SaasSessionBanner";
import { FreeTierPlanView } from "@editor/portal/components/billing/FreeTierPlanView";
import { Usage } from "@editor/portal/views/Usage";
import type { Wallet } from "@editor/portal/api/billing";
import { requestsTeamCheckout } from "@editor/routes/teamCheckoutLink";
import { legacyProcurementTarget } from "@processor/components/procurement/enterprisePath";
/**
* The seam the SaaS build shadows: picks which usage page this instance has one of.
*
* <p>Two sources, never one: {@link onWalletLoaded} reports {@code linked} as a fact, and a
* browser can hold a SaaS session with no link to this server, so routing the unlinked page through
* the wallet would flip the whole portal to linked.
*/
export function PortalBillingGate() {
const applyLinkFacts = useApplyLinkFacts();
const { loading, gated, connect } = useConnectGate();
const isOwner = useAccountLinkOwner();
const {
serverPlan,
usersInUse,
userLimit,
loading: licenseLoading,
} = useServerPlan(isOwner);
const serverPlanAction = serverPlan ? <ManageBillingButton /> : undefined;
const link = useLinkOptional();
const [searchParams] = useSearchParams();
const prompted = useRef(false);
const accountLinkRequested = requestsTeamCheckout(searchParams);
useEffect(() => {
if (!accountLinkRequested || link?.isLinked) prompted.current = false;
else if (isOwner && !loading && gated && !prompted.current) {
prompted.current = true;
connect();
}
}, [accountLinkRequested, link?.isLinked, isOwner, loading, gated, connect]);
const onWalletLoaded = useCallback(
(w: Wallet) => applyLinkFacts(true, w.status === "subscribed"),
[applyLinkFacts],
);
const procurementTarget = legacyProcurementTarget(searchParams.toString());
if (procurementTarget) return <Navigate to={procurementTarget} replace />;
// Only the organization owner manages the server's account and billing.
// The nav hides these sections too; this guards a directly entered URL.
if (!isOwner) return null;
// Neither page while the answer is unknown: showing the local meter to a linked instance would
// present a dormant ledger as its live one.
if (loading || licenseLoading) return null;
// A positively known link, not merely "not gated": linking turned off and a failed status
// check are neither, and must not reach a SaaS this instance has no address for.
if (!link?.isLinked)
return (
<FreeTierPlanView
serverPlan={serverPlan}
serverPlanAction={serverPlanAction}
licenseSection={<ServerLicenseSection onSaved={() => {}} />}
/>
);
return (
<Usage
localUsersInUse={usersInUse}
localUserLimit={userLimit}
serverPlan={serverPlan}
serverPlanAction={serverPlanAction}
onWalletLoaded={onWalletLoaded}
renderLicenseSection={(onSaved) => (
<ServerLicenseSection onSaved={onSaved} />
)}
sessionRecovery={
<>
<SaasSessionBanner />
<AccountConnectionNotice />
</>
}
/>
);
}