Skip to content

Commit

Permalink
fix: build error
Browse files Browse the repository at this point in the history
  • Loading branch information
rongquan1 committed Dec 23, 2024
1 parent b590ceb commit a42cfc9
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions src/theme/Root/index.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';

const Root = ({ children }) => {
const [isClient, setIsClient] = useState(false);

// Set the version on the <html> tag
const setHtmlDataVersion = (version) => {
document.documentElement.setAttribute('data-docs-version', version);
};

const initializeVersion = () => {
const savedVersion = localStorage.getItem('docs-preferred-version-default');
if (savedVersion) {
setHtmlDataVersion(savedVersion);
} else {
// Default to "current" if no saved version exists
setHtmlDataVersion('current');
if (typeof window !== 'undefined') {
const savedVersion = localStorage.getItem('docs-preferred-version-default');
if (savedVersion) {
setHtmlDataVersion(savedVersion);
} else {
// Default to "current" if no saved version exists
setHtmlDataVersion('current');
}
}
};

useEffect(() => {
// Initialize version on first load
initializeVersion();

// Handle changes to localStorage via the storage event
const handleStorageChange = (event) => {
if (event.key === 'docs-preferred-version-default' && event.newValue) {
setHtmlDataVersion(event.newValue);

// Redirect to a specific URL based on the new version
if (event.newValue === 'current') {
window.location.replace('/docs/topics/introduction/what-is-tradetrust/');
}
else if (event.newValue === '4.x') {
window.location.replace('/');
if (typeof window !== 'undefined') {
// Set the client state to true after the component is mounted
setIsClient(true);

// Initialize version on first load
initializeVersion();

// Handle changes to localStorage via the storage event
const handleStorageChange = (event) => {
if (event.key === 'docs-preferred-version-default' && event.newValue) {
setHtmlDataVersion(event.newValue);

// Redirect to a specific URL based on the new version
if (event.newValue === 'current') {
window.location.replace('/docs/topics/introduction/what-is-tradetrust/');
} else if (event.newValue === '4.x') {
window.location.replace('/');
}
}
}
};
};

// Listen for storage events
window.addEventListener('storage', handleStorageChange);
// Listen for storage events
window.addEventListener('storage', handleStorageChange);

// Cleanup event listener
return () => {
window.removeEventListener('storage', handleStorageChange);
};
// Cleanup event listener
return () => {
window.removeEventListener('storage', handleStorageChange);
};
}
}, []);

if (!isClient) {
return null; // Or a loading spinner if needed
}

return <>{children}</>;
};

Expand Down

0 comments on commit a42cfc9

Please sign in to comment.