Skip to content

Open-source status page, incident response and uptime monitoring

1.1 KB
Raw Blame
import React from "react";

import { APP_URL } from "@/lib/metadata/shared-metadata";

/** Adds `ref=<source>` to app links so OpenPanel can attribute the click. */
export function withRef(href: string, source: string) {
  let url: URL;
  try {
    url = new URL(href);
  } catch {
    return href;
  }
  if (url.origin !== new URL(APP_URL).origin) return href;
  url.searchParams.set("ref", source);
  return url.toString();
}

/**
 * Row of `ButtonLink`s. `source` names the CTA slot (`home-hero`) and lands on
 * every app link in the row as `?ref=`. Injected via `cloneElement`, not
 * context: this renders on the server, where `createContext` is unavailable.
 */
export function Actions({
  source,
  children,
}: {
  source: string;
  children: React.ReactNode;
}) {
  return (
    <div className="not-prose my-4 flex flex-wrap items-center gap-2">
      {React.Children.map(children, (child) => {
        if (!React.isValidElement<{ href?: string }>(child)) return child;
        const href = child.props.href;
        if (!href) return child;
        return React.cloneElement(child, { href: withRef(href, source) });
      })}
    </div>
  );
}