From bacf4c5ed2c57b02203943c0dbf03a49b0b42f46 Mon Sep 17 00:00:00 2001 From: dev-opsss Date: Wed, 9 Apr 2025 11:01:39 -0500 Subject: [PATCH] Logic change --- src/components/Queue/QueueCommands.tsx | 143 ++++++++++--------------- 1 file changed, 55 insertions(+), 88 deletions(-) diff --git a/src/components/Queue/QueueCommands.tsx b/src/components/Queue/QueueCommands.tsx index 218f83b..88d6c28 100644 --- a/src/components/Queue/QueueCommands.tsx +++ b/src/components/Queue/QueueCommands.tsx @@ -24,6 +24,56 @@ const QueueCommands: React.FC = ({ const tooltipRef = useRef(null) const { showToast } = useToast() + // Extract the repeated language selection logic into a separate function + const extractLanguagesAndUpdate = (direction?: 'next' | 'prev') => { + // Create a hidden instance of LanguageSelector to extract languages + const hiddenRenderContainer = document.createElement('div'); + hiddenRenderContainer.style.position = 'absolute'; + hiddenRenderContainer.style.left = '-9999px'; + document.body.appendChild(hiddenRenderContainer); + + // Create a root and render the LanguageSelector temporarily + const root = createRoot(hiddenRenderContainer); + root.render( + {}} + /> + ); + + // Use a small delay to ensure the component has rendered + // 50ms is generally enough for React to complete a render cycle + setTimeout(() => { + // Extract options from the rendered select element + const selectElement = hiddenRenderContainer.querySelector('select'); + if (selectElement) { + const options = Array.from(selectElement.options); + const values = options.map(opt => opt.value); + + // Find current language index + const currentIndex = values.indexOf(currentLanguage); + let newIndex = currentIndex; + + if (direction === 'prev') { + // Go to previous language + newIndex = (currentIndex - 1 + values.length) % values.length; + } else { + // Default to next language + newIndex = (currentIndex + 1) % values.length; + } + + if (newIndex !== currentIndex) { + setLanguage(values[newIndex]); + window.electronAPI.updateConfig({ language: values[newIndex] }); + } + } + + // Clean up + root.unmount(); + document.body.removeChild(hiddenRenderContainer); + }, 50); + }; + useEffect(() => { let tooltipHeight = 0 if (tooltipRef.current && isTooltipVisible) { @@ -384,96 +434,13 @@ const QueueCommands: React.FC = ({
{ - // Create a hidden instance of LanguageSelector to extract languages - const hiddenRenderContainer = document.createElement('div'); - hiddenRenderContainer.style.position = 'absolute'; - hiddenRenderContainer.style.left = '-9999px'; - document.body.appendChild(hiddenRenderContainer); - - // Create a root and render the LanguageSelector temporarily - const root = createRoot(hiddenRenderContainer); - root.render( - {}} - /> - ); - - // Use a small delay to ensure the component has rendered - // 50ms is generally enough for React to complete a render cycle - setTimeout(() => { - // Extract options from the rendered select element - const selectElement = hiddenRenderContainer.querySelector('select'); - if (selectElement) { - const options = Array.from(selectElement.options); - const values = options.map(opt => opt.value); - - // Find current language index - const currentIndex = values.indexOf(currentLanguage); - const nextIndex = (currentIndex + 1) % values.length; - - // Update language - setLanguage(values[nextIndex]); - window.electronAPI.updateConfig({ language: values[nextIndex] }); - } - - // Clean up - root.unmount(); - document.body.removeChild(hiddenRenderContainer); - }, 50); // Increased timeout to ensure render completes - }} + onClick={() => extractLanguagesAndUpdate('next')} tabIndex={0} onKeyDown={(e) => { - if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || - e.key === 'ArrowLeft' || e.key === 'ArrowRight') { - - // Create a hidden instance of LanguageSelector to extract languages - const hiddenRenderContainer = document.createElement('div'); - hiddenRenderContainer.style.position = 'absolute'; - hiddenRenderContainer.style.left = '-9999px'; - document.body.appendChild(hiddenRenderContainer); - - // Create a root and render the LanguageSelector temporarily - const root = createRoot(hiddenRenderContainer); - root.render( - {}} - /> - ); - - // Use a small delay to ensure the component has rendered - // 50ms is generally enough for React to complete a render cycle - setTimeout(() => { - // Extract options from the rendered select element - const selectElement = hiddenRenderContainer.querySelector('select'); - if (selectElement) { - const options = Array.from(selectElement.options); - const values = options.map(opt => opt.value); - - // Find current language index - const currentIndex = values.indexOf(currentLanguage); - let newIndex = currentIndex; - - if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') { - // Go to previous language - newIndex = (currentIndex - 1 + values.length) % values.length; - } else if (e.key === 'ArrowDown' || e.key === 'ArrowRight') { - // Go to next language - newIndex = (currentIndex + 1) % values.length; - } - - if (newIndex !== currentIndex) { - setLanguage(values[newIndex]); - window.electronAPI.updateConfig({ language: values[newIndex] }); - } - } - - // Clean up - root.unmount(); - document.body.removeChild(hiddenRenderContainer); - }, 50); // Increased timeout to ensure render completes + if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') { + extractLanguagesAndUpdate('prev'); + } else if (e.key === 'ArrowDown' || e.key === 'ArrowRight') { + extractLanguagesAndUpdate('next'); } }} >