import React, { useState, useEffect } from 'react' import { useRouter } from 'next/router' import { DocsThemeConfig } from 'nextra-theme-docs' import Link from 'next/link' // Global lock to prevent race conditions. // With a hard reload strategy, this lock effectively persists until the page is unloaded. let isTransitioning = false const LanguageSwitcher = () => { const { asPath } = useRouter() const [disabled, setDisabled] = useState(false) useEffect(() => { // Sync local state with global lock on mount setDisabled(isTransitioning) }, []) const isId = asPath.startsWith('/id') const toggleLanguage = () => { if (isTransitioning) return // Prevent default activity if confirmed isTransitioning = true setDisabled(true) let newPath = asPath // Robust path replacement logic if (isId) { newPath = asPath.replace('/id', '/en') } else { // Handle implicit default or explicit /en if (asPath.startsWith('/en')) { newPath = asPath.replace('/en', '/id') } else { // If path is root '/' or other, prepend '/id' // Assuming structural parity, / -> /id newPath = '/id' + (asPath === '/' ? '' : asPath) } } // Use hard reload to ensure clean sidebar state for static exports if (typeof window !== 'undefined') { window.location.href = newPath } } return ( ) } const config: DocsThemeConfig = { logo: () => { const { asPath } = useRouter() const isId = asPath.startsWith('/id') const homePath = isId ? '/id' : '/en' const guidePath = isId ? '/id/guide' : '/en/guide' const devPath = isId ? '/id/developer' : '/en/developer' const guideTitle = isId ? 'Panduan Pengguna' : 'User Guide' const devTitle = isId ? 'Developer API' : 'Developer API' return (
Docs