All files / memory-monitor/src/components/Controls ActionButtons.tsx

50% Statements 8/16
66.66% Branches 16/24
44.44% Functions 4/9
53.33% Lines 8/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249                                                      27x                                               27x                                                                                                                                                                                                           54x   54x               54x           54x                                                                 27x                                                                                         1x  
import React, { useCallback, useState } from "react";
import clsx from "clsx";
import styles from "./ActionButtons.module.scss";
 
export interface ActionButtonsProps {
  /** Request garbage collection callback */
  onRequestGC: () => void;
  /** Take snapshot callback */
  onTakeSnapshot?: () => void;
  /** Export data callback */
  onExport?: () => void;
  /** Reset history callback */
  onResetHistory?: () => void;
  /** Whether GC is supported */
  gcSupported?: boolean;
  /** Whether snapshot feature is enabled */
  snapshotEnabled?: boolean;
  /** Whether export feature is enabled */
  exportEnabled?: boolean;
  /** Custom class name */
  className?: string;
}
 
/**
 * Trash/GC icon
 */
function TrashIcon({ className }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      className={className}
    >
      <path d="M3 6h18" />
      <path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6" />
      <path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2" />
      <line x1="10" y1="11" x2="10" y2="17" />
      <line x1="14" y1="11" x2="14" y2="17" />
    </svg>
  );
}
 
/**
 * Camera/Snapshot icon
 */
function CameraIcon({ className }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      className={className}
    >
      <path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z" />
      <circle cx="12" cy="13" r="3" />
    </svg>
  );
}
 
/**
 * Download/Export icon
 */
function DownloadIcon({ className }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      className={className}
    >
      <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
      <polyline points="7 10 12 15 17 10" />
      <line x1="12" y1="15" x2="12" y2="3" />
    </svg>
  );
}
 
/**
 * Refresh/Reset icon
 */
function RefreshIcon({ className }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      className={className}
    >
      <path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" />
      <path d="M21 3v5h-5" />
      <path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" />
      <path d="M3 21v-5h5" />
    </svg>
  );
}
 
/**
 * Check icon for success state
 */
function CheckIcon({ className }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      className={className}
    >
      <polyline points="20 6 9 17 4 12" />
    </svg>
  );
}
 
/**
 * Action button component
 */
interface ActionButtonProps {
  icon: React.FC<{ className?: string }>;
  label: string;
  onClick: () => void;
  variant?: "default" | "primary" | "danger";
  disabled?: boolean;
  showSuccessState?: boolean;
}
 
function ActionButton({
  icon: Icon,
  label,
  onClick,
  variant = "default",
  disabled = false,
  showSuccessState = false,
}: ActionButtonProps) {
  const [showSuccess, setShowSuccess] = useState(false);
 
  const handleClick = useCallback(() => {
    onClick();
    if (showSuccessState) {
      setShowSuccess(true);
      setTimeout(() => setShowSuccess(false), 1500);
    }
  }, [onClick, showSuccessState]);
 
  const variantClassMap = {
    default: styles.variantDefault,
    primary: styles.variantPrimary,
    danger: styles.variantDanger,
  };
 
  return (
    <button
      type="button"
      onClick={handleClick}
      disabled={disabled || showSuccess}
      className={clsx(
        styles.button,
        showSuccess ? styles.variantSuccess : variantClassMap[variant]
      )}
    >
      {showSuccess ? (
        <CheckIcon className={styles.icon} />
      ) : (
        <Icon className={styles.icon} />
      )}
      <span>{showSuccess ? "Done" : label}</span>
    </button>
  );
}
 
/**
 * Action buttons component for memory monitoring controls
 */
export function ActionButtons({
  onRequestGC,
  onTakeSnapshot,
  onExport,
  onResetHistory,
  gcSupported = true,
  snapshotEnabled = true,
  exportEnabled = true,
  className,
}: ActionButtonsProps) {
  return (
    <div className={clsx(styles.container, className)}>
      {/* Request GC */}
      <ActionButton
        icon={TrashIcon}
        label="Request GC"
        onClick={onRequestGC}
        variant="primary"
        disabled={!gcSupported}
        showSuccessState
      />
 
      {/* Take Snapshot */}
      {snapshotEnabled && onTakeSnapshot && (
        <ActionButton
          icon={CameraIcon}
          label="Snapshot"
          onClick={onTakeSnapshot}
          showSuccessState
        />
      )}
 
      {/* Export Data */}
      {exportEnabled && onExport && (
        <ActionButton
          icon={DownloadIcon}
          label="Export"
          onClick={onExport}
          showSuccessState
        />
      )}
 
      {/* Reset History */}
      {onResetHistory && (
        <ActionButton
          icon={RefreshIcon}
          label="Reset"
          onClick={onResetHistory}
          variant="danger"
        />
      )}
    </div>
  );
}
 
ActionButtons.displayName = "ActionButtons";