Skip to content

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

996 B
Raw Blame
import { createContext, useContext, type ReactNode } from "react";
import {
  useAccountLink,
  type UseAccountLink,
} from "@processor/hooks/useAccountLink";

/**
 * Single app-wide {@link useAccountLink} instance, so status is fetched once on mount rather than per consumer.
 */
const AccountLinkContext = createContext<UseAccountLink | null>(null);

export function AccountLinkProvider({ children }: { children: ReactNode }) {
  const link = useAccountLink();
  return (
    <AccountLinkContext.Provider value={link}>
      {children}
    </AccountLinkContext.Provider>
  );
}

export function useAccountLinkContext(): UseAccountLink {
  const v = useContext(AccountLinkContext);
  if (!v) {
    throw new Error(
      "useAccountLinkContext must be used inside <AccountLinkProvider>",
    );
  }
  return v;
}

/** No status reader on SaaS, where instance linking does not apply. */
export function useAccountLinkOptional(): UseAccountLink | null {
  return useContext(AccountLinkContext);
}