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

85.71% Statements 6/7
77.77% Branches 7/9
75% Functions 3/4
85.71% Lines 6/7

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                                                1x                                                       1x   1x             1x                                         5x                                                       1x  
import React, { useCallback, useId } from "react";
import clsx from "clsx";
import { INTERVAL_OPTIONS } from "../../constants";
import styles from "./IntervalSelector.module.scss";
 
export interface IntervalSelectorProps {
  /** Current interval in milliseconds */
  value: number;
  /** Callback when interval changes */
  onChange: (interval: number) => void;
  /** Custom interval options */
  options?: readonly { readonly value: number; readonly label: string }[];
  /** Disabled state */
  disabled?: boolean;
  /** Show label */
  showLabel?: boolean;
  /** Custom class name */
  className?: string;
}
 
/**
 * Clock icon
 */
function ClockIcon({ 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}
    >
      <circle cx="12" cy="12" r="10" />
      <polyline points="12 6 12 12 16 14" />
    </svg>
  );
}
 
/**
 * Interval selector component for polling interval configuration
 */
export function IntervalSelector({
  value,
  onChange,
  options = INTERVAL_OPTIONS,
  disabled = false,
  showLabel = true,
  className,
}: IntervalSelectorProps) {
  const id = useId();
 
  const handleChange = useCallback(
    (e: React.ChangeEvent<HTMLSelectElement>) => {
      onChange(Number(e.target.value));
    },
    [onChange]
  );
 
  return (
    <div className={clsx(styles.container, className)}>
      {showLabel && (
        <label
          htmlFor={id}
          className={clsx(styles.label, disabled && styles.disabled)}
        >
          <ClockIcon className={styles.labelIcon} />
          <span>Polling Interval</span>
        </label>
      )}
 
      <div className={styles.selectWrapper}>
        <select
          id={id}
          value={value}
          onChange={handleChange}
          disabled={disabled}
          className={clsx(styles.select, disabled && styles.disabled)}
        >
          {options.map((option) => (
            <option key={option.value} value={option.value}>
              {option.label}
            </option>
          ))}
        </select>
 
        {/* Dropdown arrow */}
        <div className={styles.dropdownArrow}>
          <svg
            className={styles.arrowIcon}
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
            strokeLinejoin="round"
          >
            <polyline points="6 9 12 15 18 9" />
          </svg>
        </div>
      </div>
 
      <p className={styles.helperText}>How often to poll for memory updates</p>
    </div>
  );
}
 
IntervalSelector.displayName = "IntervalSelector";