diff --git a/src/App.tsx b/src/App.tsx index 7abf58f..62b0603 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -183,18 +183,26 @@ const Symbol: FC = ({ x, y, icon, isCover, status, onClick }) => { const themeFromPath: string = parsePathThemeName(location.href); const customThemeIdFromPath = parsePathCustomThemeId(location.href); const CUSTOM_THEME_FAIL_TIP = '查询配置失败'; +const initTheme = customThemeIdFromPath + ? { title: '', icons: [], sounds: [], name: '' } + : themeFromPath + ? builtInThemes.find((theme) => theme.name === themeFromPath) ?? + defaultTheme + : defaultTheme; + +// 读取缓存关卡数 +const LAST_LEVEL_STORAGE_KEY = 'lastLevel'; +const initLevel = Number(localStorage.getItem(LAST_LEVEL_STORAGE_KEY) || '1'); const App: FC = () => { - const [curTheme, setCurTheme] = useState>( - customThemeIdFromPath - ? { title: '', icons: [], sounds: [], name: '' } - : defaultTheme - ); + const [curTheme, setCurTheme] = useState>(initTheme); const [themes, setThemes] = useState[]>(builtInThemes); const [pureMode, setPureMode] = useState(!!customThemeIdFromPath); - const [scene, setScene] = useState(makeScene(1, curTheme.icons)); - const [level, setLevel] = useState(1); + const [scene, setScene] = useState( + makeScene(initLevel, curTheme.icons) + ); + const [level, setLevel] = useState(initLevel); const [queue, setQueue] = useState([]); const [sortedQueue, setSortedQueue] = useState< Record @@ -221,6 +229,11 @@ const App: FC = () => { } }, [bgmOn]); + // 关卡缓存 + useEffect(() => { + localStorage.setItem(LAST_LEVEL_STORAGE_KEY, level.toString()); + }, [level]); + // 初始化主题 useEffect(() => { if (customThemeIdFromPath) { @@ -245,12 +258,6 @@ const App: FC = () => { setCurTheme({ ...curTheme, title: CUSTOM_THEME_FAIL_TIP }); console.log(e); }); - } else if (themeFromPath) { - // 内置主题 - setCurTheme( - themes.find((theme) => theme.name === themeFromPath) ?? - defaultTheme - ); } }, []);