All files / src/components/admin/monitoring SnoozeDropdown.tsx

93.25% Statements 152/163
100% Branches 16/16
55.55% Functions 5/9
93.25% Lines 152/163

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 1641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 20x           20x 20x 20x 31x 31x 31x 2x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x             31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 36x 36x 36x 36x 36x 36x 36x 36x 6x 6x 7x 7x 31x 31x 31x 31x 1x 1x  
'use client';
 
import { useState, useRef, useEffect } from 'react';
import { Button } from '@/components/ui';
import { Icon } from '@/components/ui/icons';
 
export interface SnoozeDropdownProps {
  /** Current snooze date (null if not snoozed) */
  currentSnoozedUntil?: Date | string | null;
  /** Callback when snooze is selected */
  onSnooze: (until: Date | null) => void;
  /** Disable the dropdown */
  disabled?: boolean;
}
 
const snoozeOptions = [
  { label: '1 hour', hours: 1 },
  { label: '4 hours', hours: 4 },
  { label: '1 day', hours: 24 },
  { label: '3 days', hours: 72 },
  { label: '1 week', hours: 168 },
  { label: 'Custom...', hours: -1 },
];
 
/**
 * SnoozeDropdown Component
 *
 * Dropdown menu for snoozing errors for a specific duration.
 */
export function SnoozeDropdown({
  currentSnoozedUntil,
  onSnooze,
  disabled = false,
}: SnoozeDropdownProps) {
  const [isOpen, setIsOpen] = useState(false);
  const [showCustom, setShowCustom] = useState(false);
  const [customDate, setCustomDate] = useState('');
  const dropdownRef = useRef<HTMLDivElement>(null);
 
  const isSnoozed = currentSnoozedUntil !== null && currentSnoozedUntil !== undefined;
 
  // Close dropdown on outside click
  useEffect(() => {
    function handleClickOutside(event: MouseEvent) {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
        setShowCustom(false);
      }
    }
 
    document.addEventListener('mousedown', handleClickOutside);
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, []);
 
  const handleSelect = (hours: number) => {
    if (hours === -1) {
      setShowCustom(true);
      return;
    }
 
    const until = new Date();
    until.setHours(until.getHours() + hours);
    onSnooze(until);
    setIsOpen(false);
  };
 
  const handleCustomSubmit = () => {
    if (customDate) {
      onSnooze(new Date(customDate));
      setIsOpen(false);
      setShowCustom(false);
      setCustomDate('');
    }
  };
 
  const handleUnsnooze = () => {
    onSnooze(null);
    setIsOpen(false);
  };
 
  return (
    <div className="relative" ref={dropdownRef}>
      <Button
        variant="secondary"
        size="sm"
        onClick={() => setIsOpen(!isOpen)}
        disabled={disabled}
        data-testid="snooze-button"
      >
        <Icon name="clock" size={16} className="mr-1" />
        {isSnoozed ? 'Snoozed' : 'Snooze'}
        <Icon name="chevron-down" size={14} className="ml-1" />
      </Button>
 
      {isOpen && (
        <div
          className="absolute right-0 mt-1 w-48 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-50"
          data-testid="snooze-dropdown"
        >
          {showCustom ? (
            <div className="p-3 space-y-2">
              <label className="text-sm font-medium text-gray-700 dark:text-gray-300">
                Snooze until:
              </label>
              <input
                type="datetime-local"
                value={customDate}
                onChange={(e) => setCustomDate(e.target.value)}
                min={new Date().toISOString().slice(0, 16)}
                className="w-full px-2 py-1 text-sm border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                data-testid="snooze-custom-input"
              />
              <div className="flex gap-2">
                <Button
                  size="sm"
                  onClick={handleCustomSubmit}
                  disabled={!customDate}
                  className="flex-1"
                >
                  Set
                </Button>
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => setShowCustom(false)}
                >
                  Back
                </Button>
              </div>
            </div>
          ) : (
            <div className="py-1">
              {isSnoozed && (
                <>
                  <button
                    onClick={handleUnsnooze}
                    className="w-full px-4 py-2 text-left text-sm text-red-600 dark:text-red-400 hover:bg-gray-100 dark:hover:bg-gray-700"
                    data-testid="unsnooze-button"
                  >
                    Unsnooze
                  </button>
                  <div className="border-t border-gray-200 dark:border-gray-700 my-1" />
                </>
              )}
              {snoozeOptions.map((option) => (
                <button
                  key={option.label}
                  onClick={() => handleSelect(option.hours)}
                  className="w-full px-4 py-2 text-left text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
                  data-testid={`snooze-option-${option.hours}`}
                >
                  {option.label}
                </button>
              ))}
            </div>
          )}
        </div>
      )}
    </div>
  );
}
 
export default SnoozeDropdown;