Commit 479b78a7 authored by timel's avatar timel

fix: remove suspence

parent 990ac633
...@@ -67,10 +67,6 @@ export async function searchCard( ...@@ -67,10 +67,6 @@ export async function searchCard(
type?: number type?: number
): Promise<CardMeta[]> { ): Promise<CardMeta[]> {
// TODO: 让type支持多种filter类型 // TODO: 让type支持多种filter类型
console.log({
cmd: sqliteCmd.FTS,
payload: { query, type },
});
const res = await sqliteMiddleWare({ const res = await sqliteMiddleWare({
cmd: sqliteCmd.FTS, cmd: sqliteCmd.FTS,
payload: { query, type }, payload: { query, type },
......
...@@ -4,12 +4,10 @@ import { type NeosStore } from "./shared"; ...@@ -4,12 +4,10 @@ import { type NeosStore } from "./shared";
export const initStore = proxy({ export const initStore = proxy({
sqlite: { sqlite: {
initialized: false, progress: 0, // 0 -> 1
progress: 0,
},
i18n: {
initialized: false,
}, },
decks: false,
i18n: false,
// ... // ...
reset() {}, reset() {},
} satisfies NeosStore); } satisfies NeosStore);
...@@ -15,14 +15,9 @@ import { ...@@ -15,14 +15,9 @@ import {
Space, Space,
type ThemeConfig, type ThemeConfig,
} from "antd"; } from "antd";
import { Suspense, useEffect, useState } from "react"; import { useEffect, useState } from "react";
import {
Await,
defer,
type LoaderFunction,
useLoaderData,
} from "react-router-dom";
import { useSnapshot } from "valtio"; import { useSnapshot } from "valtio";
import { subscribeKey } from "valtio/utils";
import { deckStore, type IDeck, initStore } from "@/stores"; import { deckStore, type IDeck, initStore } from "@/stores";
import { Background, Loading, ScrollableArea, YgoCard } from "@/ui/Shared"; import { Background, Loading, ScrollableArea, YgoCard } from "@/ui/Shared";
...@@ -30,7 +25,7 @@ import { Background, Loading, ScrollableArea, YgoCard } from "@/ui/Shared"; ...@@ -30,7 +25,7 @@ import { Background, Loading, ScrollableArea, YgoCard } from "@/ui/Shared";
import { CardDetail } from "./CardDetail"; import { CardDetail } from "./CardDetail";
import { DeckSelect } from "./DeckSelect"; import { DeckSelect } from "./DeckSelect";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
import { initSqlite } from "./utils"; import { LoaderFunction } from "react-router-dom";
const theme: ThemeConfig = { const theme: ThemeConfig = {
components: { components: {
...@@ -41,18 +36,18 @@ const theme: ThemeConfig = { ...@@ -41,18 +36,18 @@ const theme: ThemeConfig = {
}; };
export const loader: LoaderFunction = async () => { export const loader: LoaderFunction = async () => {
return defer({ // 必须先加载卡组,不然页面会崩溃
initialized: initStore.sqlite.initialized if (!initStore.decks) {
? true await new Promise<void>((rs) => {
: initSqlite().then(() => (initStore.sqlite.initialized = true)), subscribeKey(initStore, "decks", (done) => done && rs());
}); });
}
return null;
}; };
export const Component: React.FC = () => { export const Component: React.FC = () => {
const data = useLoaderData<{
initialized: boolean;
}>();
const snapDecks = useSnapshot(deckStore); const snapDecks = useSnapshot(deckStore);
const { sqlite } = useSnapshot(initStore);
const [selectedDeck, setSelectedDeck] = useState<IDeck>(deckStore.decks[0]); const [selectedDeck, setSelectedDeck] = useState<IDeck>(deckStore.decks[0]);
return ( return (
...@@ -79,17 +74,13 @@ export const Component: React.FC = () => { ...@@ -79,17 +74,13 @@ export const Component: React.FC = () => {
<DeckEditor deck={selectedDeck} onSave={() => {}} /> <DeckEditor deck={selectedDeck} onSave={() => {}} />
</div> </div>
<div className={styles.select}> <div className={styles.select}>
<Suspense {sqlite.progress === 1 ? (
fallback={ <CardSelect />
) : (
<div className={styles.container}> <div className={styles.container}>
<Loading /> <Loading />
</div> </div>
} )}
>
<Await resolve={data.initialized}>
<CardSelect />
</Await>
</Suspense>
</div> </div>
</div> </div>
</div> </div>
...@@ -156,6 +147,7 @@ const DeckEditor: React.FC<{ ...@@ -156,6 +147,7 @@ const DeckEditor: React.FC<{
/** 卡片库,选择卡片加入正在编辑的卡组 */ /** 卡片库,选择卡片加入正在编辑的卡组 */
const CardSelect: React.FC = () => { const CardSelect: React.FC = () => {
const [searchResult, setSearchResult] = useState<number[]>([]);
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.title}> <div className={styles.title}>
......
...@@ -9,16 +9,22 @@ import { useSnapshot } from "valtio"; ...@@ -9,16 +9,22 @@ import { useSnapshot } from "valtio";
import { CookieKeys, getCookie } from "@/api"; import { CookieKeys, getCookie } from "@/api";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { accountStore, deckStore, type User } from "@/stores"; import { accountStore, deckStore, type User, initStore } from "@/stores";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
import { initSqlite } from "./utils";
const NeosConfig = useConfig(); const NeosConfig = useConfig();
export const loader: LoaderFunction = async () => { export const loader: LoaderFunction = async () => {
const user = getCookie<User>(CookieKeys.USER); const user = getCookie<User>(CookieKeys.USER);
if (user) accountStore.login(user); if (user) accountStore.login(user);
await deckStore.initialize(); // 加载卡组
deckStore.initialize().then(() => (initStore.decks = true));
// 加载ygodb
initSqlite().then(() => (initStore.sqlite.progress = 1));
initStore.sqlite.progress = 0.01;
return null; return null;
}; };
......
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import sqliteMiddleWare, { sqliteCmd } from "@/middleware/sqlite"; import sqliteMiddleWare, { sqliteCmd } from "@/middleware/sqlite";
import { initStore } from "@/stores";
const { cardsDbUrl } = useConfig(); const { cardsDbUrl } = useConfig();
export const initSqlite = () => export const initSqlite = async () => {
sqliteMiddleWare({ const { sqlite } = initStore;
sqlite.progress = 0.01;
await sqliteMiddleWare({
cmd: sqliteCmd.INIT, cmd: sqliteCmd.INIT,
initInfo: { dbUrl: cardsDbUrl }, initInfo: { dbUrl: cardsDbUrl },
}); });
sqlite.progress = 1;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment