Skip to content

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

3.1 KB
Raw Blame
import { useAccountLinkOwner } from "@editor/portal/hooks/useAccountLinkOwner";
import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { useLink } from "@editor/portal/contexts/LinkContext";
import { walletQuery } from "@editor/portal/queries/wallet";
import {
  readCachedCredits,
  writeCachedCredits,
  type CachedCredits,
} from "@editor/services/navFooterCache";
import { type NavFooterCredits } from "@editor/components/shared/navFooter/NavFooterCreditsRow";

/**
 * Free credits left on this team's allowance, for the processor's sidebar
 * footer meter. Null hides the meter.
 *
 * This is the portal's own seam rather than the editor's {@code
 * @editor/hooks/useFreeCreditsSummary}, because self-hosted resolves {@code @editor/*}
 * as proprietary → core: the cloud wallet hook isn't in that cascade, and the
 * implementation can't move down into proprietary either, since core/desktop
 * builds ship no portal and must never resolve {@code @processor}. Keeping it here
 * means only builds that actually have a processor pull in the wallet read.
 *
 * Self-hosted reads the same {@code GET /api/v1/payg/wallet} the Usage page's
 * trial meter renders — {@code apiClient.saas} with the admin's Supabase JWT,
 * since the wallet lives in the cloud even when the instance doesn't. Gated on
 * linkage: an unlinked instance has no wallet to read.
 *
 * Free teams only, matching the editor and the Plan page: a payer's headline
 * number is spend against cap, and their usage lives on Usage & Billing.
 */
export function useFreeCreditsSummary(): NavFooterCredits | null {
  const { isLinked } = useLink();
  // Shared definition, not just a shared key: per-observer options are resolved
  // per-observer, so differing retry or interval settings here would make the
  // behaviour depend on which reader happened to fetch.
  const isOwner = useAccountLinkOwner();
  const canRead = isLinked && isOwner;
  const { data: wallet } = useQuery({
    ...walletQuery(isLinked),
    enabled: canRead,
  });
  // Shared with the editor's seam, so crossing between the two apps shows the
  // figures the other one last saw rather than re-fetching into an empty row.
  const [seed] = useState(readCachedCredits);

  const live: CachedCredits | undefined = !wallet
    ? undefined
    : wallet.status === "subscribed"
      ? null
      : { remaining: wallet.freeRemaining, total: wallet.freeAllowance };

  useEffect(() => {
    // Only once linked: an unlinked instance never asks, so it has no answer of
    // its own and must not overwrite what the editor recorded.
    if (canRead && live !== undefined) writeCachedCredits(live);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [wallet, canRead]);

  // Linkage gates the seed as well as the fetch. The cache outlives an unlink
  // — nothing refetches or rewrites it once the instance stops asking — so
  // without this an unlinked instance would keep showing the figures from when
  // it was linked, indefinitely.
  if (!canRead) return null;
  return (live !== undefined ? live : seed) ?? null;
}