import { MouseEventHandler, ReactNode, Ref, useEffect, useRef } from "react"; import LinkOrAhref from "./LinkOrAhref"; import { Button } from "../button"; import { useLocation, useMatches } from "react-router"; import { useRootData } from "~/utils/data/useRootData"; import AnalyticsHelper from "~/utils/helpers/AnalyticsHelper"; import clsx from "clsx"; interface Props { className?: string; type?: "button" | "submit" | "reset" | undefined; to?: string; target?: undefined | "_blank"; rel?: string; disabled?: boolean; destructive?: boolean; isExternal?: boolean; onClick?: MouseEventHandler; autoFocus?: boolean; children: ReactNode; prefetch?: "intent" | "render" | "none"; event?: { action: string; category: string; label: string; value: string }; sendEvent?: boolean; dataUiId?: string; } export default function ButtonPrimary({ className = "", type = "button", onClick, disabled, destructive, to, target, rel, isExternal = false, autoFocus, prefetch, children, event, sendEvent = true, dataUiId, }: Props) { // const combinedClassName = clsx( // className, // "inline-flex items-center space-x-2 px-4 py-2 border border-transparent shadow-2xs text-sm font-medium rounded-md text-white focus:outline-hidden focus:ring-2 focus:ring-accent-300", // disabled && "cursor-not-allowed opacity-75", // !destructive && "bg-primary", // destructive && "bg-red-600", // !disabled && !destructive && !className && "hover:bg-accent-900 focus:ring-ring hover:text-accent-100", // !disabled && destructive && "hover:bg-red-700 focus:ring-red-500" // ); let location = useLocation(); const rootData = useRootData(); const matches = useMatches(); const focusRef = useRef(null); useEffect(() => { if (!autoFocus) { return; } const timeout = window.setTimeout(() => focusRef.current?.focus(), 0); return () => window.clearTimeout(timeout); }, [autoFocus]); function onClicked(e: React.MouseEvent) { if (onClick) { onClick(e); } if (!event || !sendEvent) { return; } const routeMatch = matches.find((m) => m.pathname == location.pathname); AnalyticsHelper.addEvent({ url: location.pathname, route: routeMatch?.id, rootData, action: event.action, category: event.category, label: event.label, value: event.value, }); } return ( {(() => { if (!to || disabled) { return ( ); } else if (to && isExternal) { return ( ); } else { return ( ); } })()} ); }