davidzollo commented on code in PR #429:
URL: https://github.com/apache/seatunnel-website/pull/429#discussion_r2782940226


##########
src/theme/NavbarItem/LocaleDropdownNavbarItem/index.js:
##########
@@ -34,84 +34,150 @@ export default function LocaleDropdownNavbarItem({
     const alternatePageUtils = useAlternatePageUtils();
     const { search, hash } = useLocation();
 
-    // Clear Google Translate cookie if we are on a native locale page
-    React.useEffect(() => {
-        if (currentLocale === 'en' || currentLocale === 'zh-CN') {
-            const clearCookie = (name) => {
-                document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 
UTC; path=/;';
-                document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 
UTC; path=/seatunnel-website;';
-                document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 
UTC; domain=.' + document.domain + '; path=/;';
-            };
-            clearCookie('googtrans');
-            const frame = document.querySelector('.goog-te-banner-frame');
-            if (frame) {
-                frame.style.display = 'none';
+    // Helper function to get current Google Translate language code from 
cookie
+    const getGoogleTranslateLangCode = () => {
+        if (typeof document === 'undefined') return null;
+        const cookie = document.cookie.split('; ').find(row => 
row.startsWith('googtrans='));
+        if (cookie) {
+            const match = cookie.match(/googtrans=\/[^\/]+\/([^;]+)/);
+            return match ? match[1] : null;
+        }
+        return null;
+    };
+
+    // Helper function to get language label from cookie
+    const getLanguageLabelFromCookie = () => {
+        const langCode = getGoogleTranslateLangCode();
+        if (langCode) {
+            const googleLang = GOOGLE_LANGUAGES.find(lang => lang.code === 
langCode);
+            if (googleLang) {
+                return googleLang.label;
             }
         }
-    }, [currentLocale]);
+        return localeConfigs[currentLocale]?.label || 'English';
+    };
+
+    // State to track current language label
+    const [currentLangLabel, setCurrentLangLabel] = 
React.useState(getLanguageLabelFromCookie());
+
+    // Update language label based on Google Translate cookie
+    React.useEffect(() => {
+        const updateLabel = () => {
+            setCurrentLangLabel(getLanguageLabelFromCookie());
+        };
+
+        // Initial update
+        updateLabel();
+
+        // Listen for storage events (from other tabs/windows)
+        window.addEventListener('storage', updateLabel);
+
+        // Poll for cookie changes (fallback)
+        const interval = setInterval(updateLabel, 1000);
+

Review Comment:
   Using `setInterval` to poll for cookie changes every second is inefficient 
and wastes main thread resources.
   **Recommendation**:
       1.  Update the state (`setCurrentLangLabel`) immediately within the 
`onClick` handlers when the user changes the language.
       2.  Keep the `window.addEventListener('storage', ...)` for cross-tab 
synchronization.
       3.  Remove the `setInterval` entirely.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to