Logic change

main
dev-opsss 2 years ago
parent 8c8aef9939
commit bacf4c5ed2

@ -24,6 +24,56 @@ const QueueCommands: React.FC<QueueCommandsProps> = ({
const tooltipRef = useRef<HTMLDivElement>(null) const tooltipRef = useRef<HTMLDivElement>(null)
const { showToast } = useToast() 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(
<LanguageSelector
currentLanguage={currentLanguage}
setLanguage={() => {}}
/>
);
// 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(() => { useEffect(() => {
let tooltipHeight = 0 let tooltipHeight = 0
if (tooltipRef.current && isTooltipVisible) { if (tooltipRef.current && isTooltipVisible) {
@ -384,96 +434,13 @@ const QueueCommands: React.FC<QueueCommandsProps> = ({
<div className="mb-3 px-2"> <div className="mb-3 px-2">
<div <div
className="flex items-center justify-between cursor-pointer hover:bg-white/10 rounded px-2 py-1 transition-colors" className="flex items-center justify-between cursor-pointer hover:bg-white/10 rounded px-2 py-1 transition-colors"
onClick={() => { onClick={() => extractLanguagesAndUpdate('next')}
// 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(
<LanguageSelector
currentLanguage={currentLanguage}
setLanguage={() => {}}
/>
);
// 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
}}
tabIndex={0} tabIndex={0}
onKeyDown={(e) => { onKeyDown={(e) => {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') {
e.key === 'ArrowLeft' || e.key === 'ArrowRight') { extractLanguagesAndUpdate('prev');
} else if (e.key === 'ArrowDown' || e.key === 'ArrowRight') {
// Create a hidden instance of LanguageSelector to extract languages extractLanguagesAndUpdate('next');
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(
<LanguageSelector
currentLanguage={currentLanguage}
setLanguage={() => {}}
/>
);
// 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
} }
}} }}
> >

Loading…
Cancel
Save