Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

4.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { formatCityStatePostalCode } from "@carbon/utils";
import { Text, View } from "@react-pdf/renderer";
import { DEFAULT_HEADER_OPTIONS, type HeaderOptions } from "../../template";
import type { Company } from "../../types";
import { useTw } from "../blocks/tw";
import { LogoImage } from "./LogoImage";

type HeaderProps = {
  company: Company;
  title: string;
  documentId?: string | null;
  documentSubId?: string | null;
  date?: string | null;
  currencyCode?: string | null;
  locale?: string;
  /** Per-template display options (logo + which fields show). */
  options?: HeaderOptions;
  /**
   * When true, the header is wrapped in <View fixed> so it repeats on
   * every page. Used by PO PDF; sales PDFs default to non-fixed (page 1 only).
   */
  fixed?: boolean;
};

const Header = ({
  company,
  title,
  documentId,
  documentSubId,
  options,
  fixed
}: HeaderProps) => {
  const tw = useTw();
  const opts = { ...DEFAULT_HEADER_OPTIONS, ...options };
  // `icon` variant prefers the square logo; `mark` prefers the full logo.
  const logoSrc =
    opts.logoVariant === "icon"
      ? (company.logoLightIcon ?? company.logoLight)
      : (company.logoLight ?? company.logoLightIcon);
  const showLogo = opts.showLogo && Boolean(logoSrc);
  // Name fallback only when a logo is wanted but missing AND the details block
  // is not already printing the name — otherwise the 2xl fallback and the 9px
  // details line both draw `company.name` and visibly overlap.
  const showNameFallback =
    opts.showLogo && !logoSrc && !opts.showCompanyDetails;

  const headerView = (
    <View style={tw("flex flex-row justify-between mb-1")}>
      {/* Unconstrained flex children overlap in react-pdf when both sides are
          wide (long titles like "Return Merchandise Authorization") — bound
          each side so the title wraps instead of colliding. */}
      <View style={tw("flex flex-row grow shrink items-center pr-3")}>
        {showLogo ? (
          <LogoImage
            src={logoSrc!}
            height={opts.logoHeight}
            crop={opts.logoCrop}
            marginRight={12}
          />
        ) : showNameFallback ? (
          <Text style={tw("text-2xl font-bold text-gray-800 mr-3")}>
            {company.name}
          </Text>
        ) : null}
        {opts.showCompanyDetails ? (
          <View
            style={tw(
              "flex flex-col grow items-center text-[9px] text-gray-800"
            )}
          >
            {company.name ? (
              <Text style={tw("font-bold")}>{company.name}</Text>
            ) : null}
            {company.addressLine1 ? <Text>{company.addressLine1}</Text> : null}
            {company.addressLine2 ? <Text>{company.addressLine2}</Text> : null}
            {company.city || company.stateProvince || company.postalCode ? (
              <Text>
                {formatCityStatePostalCode(
                  company.city,
                  company.stateProvince,
                  company.postalCode
                )}
              </Text>
            ) : null}
          </View>
        ) : null}
      </View>
      {/* `shrink` (not `shrink-0`) so a long title — "Return Merchandise
          Authorization" — wraps inside its 55% bound instead of overflowing
          the page. Short titles still size to their content. */}
      <View
        style={tw("flex flex-col items-end justify-start shrink max-w-[55%]")}
      >
        {opts.showDocumentTitle ? (
          <Text style={tw("text-2xl font-bold text-gray-800 text-right")}>
            {title}
          </Text>
        ) : null}
        {opts.showDocumentId && documentId ? (
          <Text style={tw("text-sm font-bold text-gray-600 -mt-4")}>
            {documentId}
          </Text>
        ) : null}
        {documentSubId ? (
          <Text style={tw("text-[8px] font-bold text-gray-600")}>
            {documentSubId}
          </Text>
        ) : null}
      </View>
    </View>
  );

  if (fixed) {
    return (
      <>
        <View fixed>{headerView}</View>
        <View fixed style={tw("h-[1px] bg-gray-200 mb-4")} />
      </>
    );
  }

  return (
    <>
      {headerView}
      <View style={tw("h-[1px] bg-gray-200 mb-4")} />
    </>
  );
};

export { Header };