2024-06-11 05:33:45 -04:00
import { useWindowSize } from '@docusaurus/theme-common' ;
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem' ;
import React , { useEffect , useState } from 'react' ;
export default function VersionSwitcher ( ) : JSX . Element {
const [ versions , setVersions ] = useState ( [ ] ) ;
2025-03-11 06:41:12 -04:00
const [ activeLabel , setLabel ] = useState ( 'Versions' ) ;
2024-06-11 05:33:45 -04:00
const windowSize = useWindowSize ( ) ;
useEffect ( ( ) = > {
async function getVersions() {
try {
2025-09-25 15:54:34 +01:00
let baseUrl = 'https://docs.immich.app' ;
2024-06-11 05:33:45 -04:00
if ( window . location . origin === 'http://localhost:3005' ) {
baseUrl = window . location . origin ;
}
const response = await fetch ( ` ${ baseUrl } /archived-versions.json ` ) ;
const archiveVersions = await response . json ( ) ;
const allVersions = [
2025-09-25 15:54:34 +01:00
{ label : 'Next' , url : 'https://docs.main.preview.immich.app' } ,
{ label : 'Latest' , url : 'https://docs.immich.app' } ,
2024-06-11 05:33:45 -04:00
. . . archiveVersions ,
2025-09-25 15:54:34 +01:00
] . map ( ( { label , url , rootPath } ) = > ( {
2025-02-25 10:34:46 -05:00
label ,
url : new URL ( url ) ,
2025-09-25 15:54:34 +01:00
rootPath ,
2025-02-25 10:34:46 -05:00
} ) ) ;
2024-06-11 05:33:45 -04:00
setVersions ( allVersions ) ;
2025-02-25 10:34:46 -05:00
const activeVersion = allVersions . find ( ( version ) = > version . url . origin === window . location . origin ) ;
2024-06-11 05:33:45 -04:00
if ( activeVersion ) {
setLabel ( activeVersion . label ) ;
}
} catch ( error ) {
console . error ( 'Failed to fetch versions' , error ) ;
}
}
if ( versions . length === 0 ) {
getVersions ( ) ;
}
} , [ ] ) ;
return (
versions . length > 0 && (
< DropdownNavbarItem
2025-02-06 16:00:52 -05:00
className = "version-switcher-34ab39"
2025-03-11 06:41:12 -04:00
label = { activeLabel }
2024-06-11 05:33:45 -04:00
mobile = { windowSize === 'mobile' }
2025-09-25 15:54:34 +01:00
items = { versions . map ( ( { label , url , rootPath } ) = > {
let path = location . pathname + location . search + location . hash ;
if ( rootPath && ! path . startsWith ( rootPath ) ) {
path = rootPath + path ;
}
return {
label ,
to : new URL ( path , url ) . href ,
target : '_self' ,
className : label === activeLabel ? 'dropdown__link--active menu__link--active' : '' , // workaround because React Router `<NavLink>` only supports using URL path for checking if active: https://v5.reactrouter.com/web/api/NavLink/isactive-func
} ;
} ) }
2024-06-11 05:33:45 -04:00
/ >
)
) ;
}