1
- import React , { useState , Suspense } from "react" ;
1
+ import React , { useState , Suspense , useEffect } from "react" ;
2
2
import { Button , Table , Empty , Tabs , Image } from "antd" ;
3
3
4
4
import "highlight.js/styles/github.css" ;
@@ -12,7 +12,7 @@ import { LEETCODE_CN_URL } from "./constant/index";
12
12
import ProblemDetail from "./Detail" ;
13
13
import Roadmap from "./roadmap/roadmap.jsx" ;
14
14
import TagOrLink from "./components/TagOrLink" ;
15
- import tempaltes from "./codeTemplates/index" ;
15
+ import templates from "./codeTemplates/index" ;
16
16
import checkUpdate from "./checkUpdates" ;
17
17
18
18
import { isInExtension , getUrlParameter } from "./utils" ;
@@ -22,7 +22,7 @@ import "./App.css";
22
22
import CodeTemplates from "./codeTemplates/codeTemplate" ;
23
23
import ComplexityRating from "./complexityRating/index" ;
24
24
import SolutionTemplate from "./solutionTemplate/index" ;
25
- import { t , initLang } from "./locales" ;
25
+ import { t , initLang , setLang , lang } from "./locales" ;
26
26
// import { data as a } from "./db/binary-tree";
27
27
28
28
const DataStrutureVis = isInExtension ( )
@@ -106,13 +106,20 @@ function App() {
106
106
// setHasSolution(!!problems[problemId]);
107
107
// }, 1000);
108
108
109
+ const [ langReady , setLangReady ] = useState ( false ) ;
109
110
const [ problemId , setProblemId ] = useState ( "" ) ;
110
111
111
112
const [ hasSolution , setHasSolution ] = useState ( false ) ;
112
113
const [ inSelected , setInSelected ] = useState ( false ) ; // 是否被精选题解(其实就是合集)收录
113
114
const [ page , setPage ] = useState ( "" ) ;
114
115
const [ tab , setTab ] = useState ( initialTab ) ;
115
116
117
+ useEffect ( ( ) => {
118
+ process . env . NODE_ENV === "development" && initLang ( ) ;
119
+ setLangReady ( true ) ;
120
+ } , [ ] ) ;
121
+
122
+
116
123
// const [inLeetCode, setInLeetCode] = useState(true);
117
124
118
125
// if (!inLeetCode) return window.open(LEETCODE_CN_URL + "/problemset/all/");
@@ -124,152 +131,168 @@ function App() {
124
131
// }, 1000);
125
132
// console.log(a);
126
133
127
- return (
128
- < div className = "container" >
129
- < div
130
- className = "tree-vis"
131
- style = { {
132
- display : "none" ,
133
- position : "fixed" ,
134
- zIndex : 99 ,
135
- top : 0 ,
136
- bottom : 0 ,
137
- left : 0 ,
138
- right : 0 ,
139
- backgroundColor : "rgba(0,0,0,.4)" ,
140
- } }
141
- >
142
- < div > { /* <pre>{a}</pre> */ } </ div >
143
- < canvas width = "1000" height = "1000" id = "canvas" > </ canvas >
144
- </ div >
145
- { isInExtension ( ) && (
146
- < >
147
- < div className = "guide-wrapper" >
148
- < div className = "guide" >
149
- { page !== "" ? (
150
- < Button type = "link" onClick = { ( ) => setPage ( "" ) } >
151
- { t ( "Locale.app.back" ) }
152
- </ Button >
153
- ) : (
154
- ""
155
- ) }
156
- { hasSolution && page === "" ? (
157
- < Button type = "link" onClick = { ( ) => setPage ( "detail" ) } >
158
- { t ( "Locale.app.viewSolution" ) }
159
- < img
160
- src = { viewLogo }
161
- alt = "view-solution"
162
- className = "problem-icon"
163
- style = { { margin : "0 0 0 10px" } }
164
- />
165
- </ Button >
166
- ) : (
167
- ""
168
- ) }
134
+ const changeLang = async ( ) => {
135
+ await setLangReady ( false ) ;
136
+ setLang ( lang === "zh" ? "en" : "zh" ) ;
137
+ setLangReady ( true ) ;
138
+ } ;
169
139
170
- { ! hasSolution &&
171
- page !== "allSolutions" &&
172
- ( inSelected ? (
173
- < Button
174
- type = "link"
175
- target = "_blank"
176
- href = { selected [ problemId ] . url }
177
- >
178
- { t ( "Locale.app.viewInHandpickCollection" , selected [ problemId ] . title ) }
140
+ return (
141
+ langReady && (
142
+ < div className = "container" >
143
+ < div
144
+ className = "tree-vis"
145
+ style = { {
146
+ display : "none" ,
147
+ position : "fixed" ,
148
+ zIndex : 99 ,
149
+ top : 0 ,
150
+ bottom : 0 ,
151
+ left : 0 ,
152
+ right : 0 ,
153
+ backgroundColor : "rgba(0,0,0,.4)" ,
154
+ } }
155
+ >
156
+ < div > { /* <pre>{a}</pre> */ } </ div >
157
+ < canvas width = "1000" height = "1000" id = "canvas" > </ canvas >
158
+ </ div >
159
+ { isInExtension ( ) && (
160
+ < >
161
+ < div className = "guide-wrapper" >
162
+ < div className = "guide" >
163
+ { page !== "" ? (
164
+ < Button type = "link" onClick = { ( ) => setPage ( "" ) } >
165
+ { t ( "Locale.app.back" ) }
166
+ </ Button >
167
+ ) : (
168
+ ""
169
+ ) }
170
+ { hasSolution && page === "" ? (
171
+ < Button type = "link" onClick = { ( ) => setPage ( "detail" ) } >
172
+ { t ( "Locale.app.viewSolution" ) }
179
173
< img
180
- alt = "view-solutions"
181
- src = { collectionLogo }
174
+ src = { viewLogo }
175
+ alt = "view-solution"
182
176
className = "problem-icon"
183
177
style = { { margin : "0 0 0 10px" } }
184
178
/>
185
179
</ Button >
186
180
) : (
187
- < Button type = "link" onClick = { ( ) => setPage ( "allSolutions" ) } >
188
- { t ( "Locale.app.notCollected" ) }
189
- </ Button >
190
- ) ) }
191
- </ div >
192
- { page === "detail" && < ProblemDetail problemId = { problemId } /> }
193
- </ div >
181
+ ""
182
+ ) }
194
183
195
- < div style = { page === "allSolutions" ? { } : { display : "none" } } >
196
- < Empty description = { t ( "Locale.app.writingExplanation" ) } >
197
- < div className = "row" style = { { marginTop : "20px" } } >
198
- { t ( "Locale.app.allCollected" ) }
184
+ { ! hasSolution &&
185
+ page !== "allSolutions" &&
186
+ ( inSelected ? (
187
+ < Button
188
+ type = "link"
189
+ target = "_blank"
190
+ href = { selected [ problemId ] . url }
191
+ >
192
+ { t (
193
+ "Locale.app.viewInHandpickCollection" ,
194
+ selected [ problemId ] . title
195
+ ) }
196
+ < img
197
+ alt = "view-solutions"
198
+ src = { collectionLogo }
199
+ className = "problem-icon"
200
+ style = { { margin : "0 0 0 10px" } }
201
+ />
202
+ </ Button >
203
+ ) : (
204
+ < Button type = "link" onClick = { ( ) => setPage ( "allSolutions" ) } >
205
+ { t ( "Locale.app.notCollected" ) }
206
+ </ Button >
207
+ ) ) }
199
208
</ div >
200
- < Table
201
- pagination = { { hideOnSinglePage : true } }
202
- dataSource = { dataSource }
203
- rowKey = "id"
204
- columns = { columns }
205
- />
206
- </ Empty >
207
- </ div >
208
- </ >
209
- ) }
210
- { page === "" && (
211
- < Tabs type = "card" activeKey = { tab } onChange = { setTab } >
212
- < TabPane key = "code-template" tab = { t ( "Locale.codeTemplate.name" ) } >
213
- < CodeTemplates tempaltes = { tempaltes } > </ CodeTemplates >
214
- </ TabPane >
215
- < TabPane
216
- key = "data-structure-vis"
217
- tab = { t ( "Locale.dataStructureVisualization.name" ) }
218
- >
219
- { isInExtension ( ) ? (
220
- < Button
221
- type = "link"
222
- target = "_blank"
223
- href = "https://leetcode-pp.github.io/leetcode-cheat/?tab=data-structure-vis"
209
+ { page === "detail" && < ProblemDetail problemId = { problemId } /> }
210
+ </ div >
211
+
212
+ < div style = { page === "allSolutions" ? { } : { display : "none" } } >
213
+ < Empty description = { t ( "Locale.app.writingExplanation" ) } >
214
+ < div className = "row" style = { { marginTop : "20px" } } >
215
+ { t ( "Locale.app.allCollected" ) }
216
+ </ div >
217
+ < Table
218
+ pagination = { { hideOnSinglePage : true } }
219
+ dataSource = { dataSource }
220
+ rowKey = "id"
221
+ columns = { columns }
222
+ />
223
+ </ Empty >
224
+ </ div >
225
+ </ >
226
+ ) }
227
+ { page === "" && (
228
+ < Tabs type = "card" activeKey = { tab } onChange = { setTab } >
229
+ < TabPane key = "code-template" tab = { t ( "Locale.codeTemplate.name" ) } >
230
+ < CodeTemplates templates = { templates } > </ CodeTemplates >
231
+ </ TabPane >
232
+ < TabPane
233
+ key = "data-structure-vis"
234
+ tab = { t ( "Locale.dataStructureVisualization.name" ) }
235
+ >
236
+ { isInExtension ( ) ? (
237
+ < Button
238
+ type = "link"
239
+ target = "_blank"
240
+ href = "https://leetcode-pp.github.io/leetcode-cheat/?tab=data-structure-vis"
241
+ >
242
+ { t ( "Locale.app.goToTheWebsiteToUse" ) }
243
+ </ Button >
244
+ ) : (
245
+ < Suspense fallback = { < div > Loading...</ div > } >
246
+ < DataStrutureVis > </ DataStrutureVis >
247
+ </ Suspense >
248
+ ) }
249
+ </ TabPane >
250
+ { ! isInExtension ( ) && (
251
+ < TabPane
252
+ key = "solution-template"
253
+ tab = { t ( "Locale.explanationTemplate.name" ) }
224
254
>
225
- { t ( "Locale.app.goToTheWebsiteToUse" ) }
226
- </ Button >
227
- ) : (
228
- < Suspense fallback = { < div > Loading...</ div > } >
229
- < DataStrutureVis > </ DataStrutureVis >
230
- </ Suspense >
255
+ < SolutionTemplate > </ SolutionTemplate >
256
+ </ TabPane >
231
257
) }
232
- </ TabPane >
233
- { ! isInExtension ( ) && (
258
+
234
259
< TabPane
235
- key = "solution-template "
236
- tab = { t ( "Locale.explanationTemplate .name" ) }
260
+ key = "complexityRating "
261
+ tab = { t ( "Locale.complexityQuickCheck .name" ) }
237
262
>
238
- < SolutionTemplate > </ SolutionTemplate >
263
+ < ComplexityRating / >
239
264
</ TabPane >
240
- ) }
265
+ < TabPane key = "roadmap" tab = { t ( "Locale.learningRoute.name" ) } >
266
+ < Roadmap />
267
+ </ TabPane >
268
+ { isInExtension ( ) && (
269
+ < TabPane key = "checkUpdate" tab = { t ( "Locale.checkForUpdates.name" ) } >
270
+ < div > { t ( "Locale.app.checkTips" ) } </ div >
271
+ < Button
272
+ style = { { margin : "20px 0 0 20px" } }
273
+ type = "primary"
274
+ onClick = { checkUpdate }
275
+ >
276
+ { t ( "Locale.app.checkBtn" ) }
277
+ </ Button >
278
+ </ TabPane >
279
+ ) }
241
280
242
- < TabPane
243
- key = "complexityRating"
244
- tab = { t ( "Locale.complexityQuickCheck.name" ) }
245
- >
246
- < ComplexityRating />
247
- </ TabPane >
248
- < TabPane key = "roadmap" tab = { t ( "Locale.learningRoute.name" ) } >
249
- < Roadmap />
250
- </ TabPane >
251
- { isInExtension ( ) && (
252
- < TabPane key = "checkUpdate" tab = { t ( "Locale.checkForUpdates.name" ) } >
253
- < div > { t ( "Locale.app.checkTips" ) } </ div >
254
- < Button
255
- style = { { margin : "20px 0 0 20px" } }
256
- type = "primary"
257
- onClick = { checkUpdate }
258
- >
259
- { t ( "Locale.app.checkBtn" ) }
281
+ < TabPane key = "about" tab = { t ( "Locale.aboutMe.name" ) } >
282
+ < div >
283
+ < div > { t ( "Locale.app.selfIntroduction" ) } </ div >
284
+ < Image src = "https://p.ipic.vip/h9nm77.jpg" > </ Image >
285
+ </ div >
286
+ </ TabPane >
287
+ < TabPane key = "changeLang" tab = { t ( "app.setLang" ) } onClick = { changeLang } >
288
+ < Button type = "primary" onClick = { changeLang } >
289
+ { t ( "app.changeLang" ) }
260
290
</ Button >
261
291
</ TabPane >
262
- ) }
263
-
264
- < TabPane key = "about" tab = { t ( "Locale.aboutMe.name" ) } >
265
- < div >
266
- < div > { t ( "Locale.app.selfIntroduction" ) } </ div >
267
- < Image src = "https://p.ipic.vip/h9nm77.jpg" > </ Image >
268
- </ div >
269
- </ TabPane >
270
- </ Tabs >
271
- ) }
272
- </ div >
292
+ </ Tabs >
293
+ ) }
294
+ </ div >
295
+ )
273
296
) ;
274
297
}
275
298
0 commit comments