"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { cn } from "@suphark/ui/lib/utils"; import { useEffect, useRef, useState } from "react"; import { ActionClose, ActionConfirm, ActionSave, StatusLoading } from "../semantic-icons"; interface FloatingSaveButtonProps { isPending: boolean; hasChanges: boolean; onSave: () => void; onCancel?: () => void; saveText?: string; successText?: string; pendingText?: string; cancelText?: string; className?: string; buttonClassName?: string; } export function FloatingSaveButton({ isPending, hasChanges, onSave, onCancel, saveText, successText, pendingText, cancelText, className, buttonClassName, }: FloatingSaveButtonProps) { const text = useUiText(); const saveLabel = saveText ?? text.save; const successLabel = successText ?? text.buttons?.saved ?? "บันทึกแล้ว"; const pendingLabel = pendingText ?? text.buttons?.saving ?? "กำลังบันทึก..."; const cancelLabel = cancelText ?? text.cancel; const [showSuccess, setShowSuccess] = useState(false); const wasPending = useRef(false); useEffect(() => { if (wasPending.current && !isPending && !hasChanges) { setShowSuccess(true); const timer = setTimeout(() => setShowSuccess(false), 2000); return () => clearTimeout(timer); } wasPending.current = isPending; }, [isPending, hasChanges]); if (!hasChanges && !showSuccess) return null; return (