@@ -40,12 +40,24 @@ export const CodeSnippetContext = React.createContext({
4040
4141const loadingComponent = < NonIdealState title = "Loading Content" icon = { < Spinner /> } /> ;
4242
43+ const AVAILABLE_SICP_TB_LANGS = [ 'en' , 'zh_CN' ] as const ;
44+
45+ const loadInitialLang = ( ) => {
46+ const saved = readSicpLangLocalStorage ( ) ;
47+ if ( AVAILABLE_SICP_TB_LANGS . includes ( saved ) ) {
48+ return saved ;
49+ } else {
50+ setSicpLangLocalStorage ( SICP_DEF_TB_LANG ) ;
51+ return SICP_DEF_TB_LANG ;
52+ }
53+ } ;
54+
4355const Sicp : React . FC = ( ) => {
4456 const [ data , setData ] = useState ( < > </ > ) ;
4557 const [ loading , setLoading ] = useState ( false ) ;
4658 const [ active , setActive ] = useState ( '0' ) ;
47- const { param_lang , section } = useParams < { param_lang : string ; section : string } > ( ) ;
48- const [ lang , setLang ] = useState ( readSicpLangLocalStorage ( ) ) ;
59+ const { paramLang , section } = useParams < { paramLang : string ; section : string } > ( ) ;
60+ const [ lang , setLang ] = useState ( loadInitialLang ( ) ) ;
4961 const parentRef = useRef < HTMLDivElement > ( null ) ;
5062 const refs = useRef < Record < string , HTMLElement | null > > ( { } ) ;
5163 const navigate = useNavigate ( ) ;
@@ -94,31 +106,30 @@ const Sicp: React.FC = () => {
94106
95107 // Handle loading of latest viewed section and fetch json data
96108 React . useEffect ( ( ) => {
97- const valid_langs = [ 'en' , 'zh_CN' ] ;
98-
99- if ( ( section && valid_langs . includes ( section ) ) || param_lang ) {
100- const plang = param_lang ? param_lang : section ? section : SICP_DEF_TB_LANG ;
101- if ( ! valid_langs . includes ( plang ) ) {
109+ if ( paramLang || ( section && AVAILABLE_SICP_TB_LANGS . includes ( section ) ) ) {
110+ const pLang = paramLang ? paramLang : section ;
111+ if ( AVAILABLE_SICP_TB_LANGS . includes ( pLang ) ) {
112+ setLang ( pLang ) ;
113+ setSicpLangLocalStorage ( pLang ) ;
114+ } else {
102115 setLang ( SICP_DEF_TB_LANG ) ;
103116 setSicpLangLocalStorage ( SICP_DEF_TB_LANG ) ;
104- } else {
105- setLang ( plang ) ;
106- setSicpLangLocalStorage ( plang ) ;
107117 }
108- if ( section && valid_langs . includes ( section ) ) {
109- navigate ( `/sicpjs/${ SICP_INDEX } ` , { replace : true } ) ;
110- } else {
118+ if ( paramLang ) {
111119 navigate ( `/sicpjs/${ section } ` , { replace : true } ) ;
120+ } else {
121+ navigate ( `/sicpjs/${ readSicpSectionLocalStorage ( ) } ` , { replace : true } ) ;
112122 }
113123 return ;
114124 }
125+
115126 if ( ! section ) {
116127 /**
117128 * Handles rerouting to the latest viewed section when clicking from
118129 * the main application navbar. Navigate replace logic is used to allow the
119130 * user to still use the browser back button to navigate the app.
120131 */
121- navigate ( path . join ( ' sicpjs' , readSicpSectionLocalStorage ( ) ) , { replace : true } ) ;
132+ navigate ( `/ sicpjs/ ${ readSicpSectionLocalStorage ( ) } ` , { replace : true } ) ;
122133 return ;
123134 }
124135
@@ -129,7 +140,7 @@ const Sicp: React.FC = () => {
129140
130141 setLoading ( true ) ;
131142
132- if ( ! valid_langs . includes ( lang ) ) {
143+ if ( ! AVAILABLE_SICP_TB_LANGS . includes ( lang ) ) {
133144 setLang ( SICP_DEF_TB_LANG ) ;
134145 setSicpLangLocalStorage ( SICP_DEF_TB_LANG ) ;
135146 }
@@ -166,7 +177,7 @@ const Sicp: React.FC = () => {
166177 . finally ( ( ) => {
167178 setLoading ( false ) ;
168179 } ) ;
169- } , [ param_lang , section , lang , navigate ] ) ;
180+ } , [ paramLang , section , lang , navigate ] ) ;
170181
171182 // Scroll to correct position
172183 React . useEffect ( ( ) => {
0 commit comments