2025-08-27 23:20:19 +03:00
|
|
|
import { useEffect, useRef, useState } from "preact/hooks";
|
2025-08-24 22:56:47 +03:00
|
|
|
import { CommandNames } from "../../components/app_context";
|
2025-08-27 23:20:19 +03:00
|
|
|
import { useStaticTooltip } from "./hooks";
|
|
|
|
|
import keyboard_actions from "../../services/keyboard_actions";
|
2025-08-24 22:56:47 +03:00
|
|
|
|
2025-08-27 23:36:50 +03:00
|
|
|
export interface ActionButtonProps {
|
2025-08-06 18:01:26 +03:00
|
|
|
text: string;
|
2025-08-28 21:03:33 +03:00
|
|
|
titlePosition?: "bottom" | "left";
|
2025-08-06 18:01:26 +03:00
|
|
|
icon: string;
|
2025-08-23 10:47:46 +03:00
|
|
|
className?: string;
|
2025-08-23 19:48:01 +03:00
|
|
|
onClick?: (e: MouseEvent) => void;
|
2025-08-24 22:56:47 +03:00
|
|
|
triggerCommand?: CommandNames;
|
2025-08-27 23:36:50 +03:00
|
|
|
noIconActionClass?: boolean;
|
2025-09-06 12:26:42 +03:00
|
|
|
frame?: boolean;
|
2025-08-06 18:01:26 +03:00
|
|
|
}
|
|
|
|
|
|
2025-09-06 12:26:42 +03:00
|
|
|
export default function ActionButton({ text, icon, className, onClick, triggerCommand, titlePosition, noIconActionClass, frame }: ActionButtonProps) {
|
2025-08-27 23:20:19 +03:00
|
|
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
|
|
|
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
|
2025-09-06 12:26:42 +03:00
|
|
|
|
2025-08-27 23:20:19 +03:00
|
|
|
useStaticTooltip(buttonRef, {
|
|
|
|
|
title: keyboardShortcut?.length ? `${text} (${keyboardShortcut?.join(",")})` : text,
|
|
|
|
|
placement: titlePosition ?? "bottom",
|
|
|
|
|
fallbackPlacements: [ titlePosition ?? "bottom" ]
|
|
|
|
|
});
|
2025-09-06 12:26:42 +03:00
|
|
|
|
2025-08-27 23:20:19 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (triggerCommand) {
|
2025-08-30 10:29:03 +03:00
|
|
|
keyboard_actions.getAction(triggerCommand, true).then(action => setKeyboardShortcut(action?.effectiveShortcuts));
|
2025-08-27 23:20:19 +03:00
|
|
|
}
|
|
|
|
|
}, [triggerCommand]);
|
|
|
|
|
|
2025-08-06 18:01:26 +03:00
|
|
|
return <button
|
2025-08-27 23:20:19 +03:00
|
|
|
ref={buttonRef}
|
2025-09-06 12:26:42 +03:00
|
|
|
class={`${className ?? ""} ${!noIconActionClass ? "icon-action" : "btn"} ${icon} ${frame ? "btn btn-primary" : ""}`}
|
|
|
|
|
onClick={onClick}
|
2025-08-24 22:56:47 +03:00
|
|
|
data-trigger-command={triggerCommand}
|
2025-08-06 18:01:26 +03:00
|
|
|
/>;
|
2025-09-06 12:26:42 +03:00
|
|
|
}
|