'use client'; import React, { FC, useCallback, useEffect, useState } from 'react'; import { DelayIcon, DropdownArrowIcon } from '@gitroom/frontend/components/ui/icons'; import clsx from 'clsx'; import { useLaunchStore } from '@gitroom/frontend/components/new-launch/store'; import { useShallow } from 'zustand/react/shallow'; import { useT } from '@gitroom/react/translation/get.transation.service.client'; import { useClickOutside } from '@mantine/hooks'; const delayOptions = [ { value: 1, label: '1m' }, { value: 2, label: '2m' }, { value: 5, label: '5m' }, { value: 10, label: '10m' }, { value: 15, label: '15m' }, { value: 30, label: '30m' }, { value: 60, label: '1h' }, { value: 120, label: '2h' }, ]; export const DelayComponent: FC<{ currentIndex: number; currentDelay: number; }> = ({ currentIndex, currentDelay }) => { const t = useT(); const [isOpen, setIsOpen] = useState(false); const [customValue, setCustomValue] = useState(''); const isCustomDelay = currentDelay > 0 && !delayOptions.some((opt) => opt.value === currentDelay); useEffect(() => { if (isOpen && isCustomDelay) { setCustomValue(String(currentDelay)); } else if (isOpen && !isCustomDelay) { setCustomValue(''); } }, [isOpen, isCustomDelay, currentDelay]); const { current, setInternalDelay, setGlobalDelay } = useLaunchStore( useShallow((state) => ({ current: state.current, setGlobalDelay: state.setGlobalDelay, setInternalDelay: state.setInternalDelay, })) ); const ref = useClickOutside(() => { if (!isOpen) { return; } setIsOpen(false); }); const setDelay = useCallback( (index: number) => (minutes: number) => { if (current !== 'global') { return setInternalDelay(current, index, minutes); } return setGlobalDelay(index, minutes); }, [currentIndex, current] ); const handleSelectDelay = useCallback( (minutes: number) => { setDelay(currentIndex)(minutes); setIsOpen(false); }, [currentIndex, setDelay] ); const getCurrentDelayLabel = () => { if (!currentDelay) return null; const option = delayOptions.find((opt) => opt.value === currentDelay); return option?.label || `${currentDelay} min`; }; return (