"use client"; import { useState, useEffect, useCallback, useRef } from "react"; export function useChannelInput( channelCount: number, switchChannel: (idx: number) => void, resetIdle: () => void, ) { const [channelInput, setChannelInput] = useState(""); const channelInputTimer = useRef | null>(null); const handleDigit = useCallback( (digit: string) => { setChannelInput((prev) => { const next = prev + digit; if (channelInputTimer.current) clearTimeout(channelInputTimer.current); channelInputTimer.current = setTimeout(() => { const num = parseInt(next, 10); if (num >= 1 && num <= Math.max(channelCount, 1)) { switchChannel(num - 1); resetIdle(); } setChannelInput(""); }, 1500); return next; }); }, [channelCount, switchChannel, resetIdle], ); useEffect(() => { return () => { if (channelInputTimer.current) clearTimeout(channelInputTimer.current); }; }, []); return { channelInput, handleDigit }; }