"use client"; import { Button, type ButtonTooltipSide } from "@suphark/ui/components/shared/button/button"; import type { TooltipContent } from "@suphark/ui/components/ui/tooltip"; import { type CheckPermissionOptions, type CheckPermissionRequirement, usePermission, } from "@suphark/ui/lib/permission"; import { cn } from "@suphark/ui/lib/utils"; import * as React from "react"; import { StatusLoading } from "../../semantic-icons"; // ดึง Type Props มาจาก Component โดยตรง — tooltip ของตัวนี้คุมเอง (ReactNode + side ของ Base UI) type ButtonProps = Omit< React.ComponentProps, "tooltip" | "tooltipSide" | "tooltipDelay" >; type TooltipContentProps = React.ComponentProps; /** * Extends standard ButtonProps and adds tooltip and external loading state. */ export interface ActionButtonProps extends ButtonProps, CheckPermissionOptions { /** * Optional: Permission requirement to render this button (e.g. "po-tracking:create") */ permission?: CheckPermissionRequirement; /** * Optional text or element to display in a tooltip on hover. */ tooltip?: React.ReactNode; /** * Specifies the preferred side of the trigger to render against. */ tooltipSide?: TooltipContentProps["side"]; // "bottom" | "top" | "right" | "left" /** * An optional icon element (e.g., ) to display. * It will be replaced by a spinner when isLoading is true. */ icon?: React.ReactElement<{ className?: string }>; /** * A boolean prop passed from the parent to control the loading state. */ isLoading?: boolean; /** * If true, the label will be hidden on mobile screens (max-sm) and the tooltip * will automatically show the label if not explicitly provided. */ responsive?: boolean; } /** * A component that combines Button, Tooltip, and an external loading state * for in-page actions (e.g., opening dialogs, submitting forms). */ export function ActionButton({ permission, organizationId, orgTag, tooltip, tooltipSide, icon, isLoading, responsive, children, className, disabled, onClick, variant, ...props }: ActionButtonProps) { // Call usePermission unconditionally to comply with Rules of Hooks const hasAccess = usePermission(permission || "public", { organizationId, orgTag }); if (permission && !hasAccess) { return null; } // 1. Determine which icon to display const displayIcon = isLoading ? ( ) : icon ? ( // Clone the provided icon to add standard styling React.cloneElement(icon, { className: cn("h-4 w-4", children && (responsive ? "sm:mr-2" : "mr-2"), icon.props.className), }) ) : null; // 2. Wrap children if responsive const content = responsive ? ( // max-sm:sr-only (ไม่ใช่ hidden) — จอเล็กซ่อนข้อความแต่ยังเป็นชื่อปุ่มให้โปรแกรมอ่านจอ {children} ) : ( children ); // 3. Auto-tooltip for responsive mode const finalTooltip = tooltip || (responsive ? children : null); // 4. Create the core button content const buttonContent = ( ); return buttonContent; }