Skip to content

Commit ad5b277

Browse files
committed
1.优化代码,充分利用表单
2.优化其他代码细节
1 parent 76290f6 commit ad5b277

12 files changed

Lines changed: 418 additions & 390 deletions

File tree

network/src/network.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ impl Network {
3232
gossip,
3333
})
3434
}
35-
pub async fn shutdown(&self) -> Result<()> {
36-
Ok(self.router.shutdown().await?)
37-
}
3835
pub fn account(&self) -> Arc<Account> {
3936
self.user_protocol.account.clone()
4037
}
38+
pub async fn shutdown(&self) -> Result<()> {
39+
Ok(self.router.shutdown().await?)
40+
}
4141
pub async fn search_user(&self, user_id: impl AsRef<str>) -> Result<User> {
4242
self.user_protocol.request_user_info(user_id).await
4343
}

network/src/wasm/network.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ impl Network {
4545
pub async fn new(account: Account) -> Result<Self, JsError> {
4646
Ok(Self(network::Network::new(account.into()).await.mje()?))
4747
}
48-
pub async fn shutdown(&self) -> Result<(), JsError> {
49-
Ok(self.0.shutdown().await.mje()?)
50-
}
5148
pub fn account(&self) -> Account {
5249
(*self.0.account()).clone().into()
5350
}
51+
pub async fn shutdown(&self) -> Result<(), JsError> {
52+
Ok(self.0.shutdown().await.mje()?)
53+
}
5454
pub async fn search_user(&self, user_id: String) -> Result<User, JsError> {
5555
Ok(self.0.search_user(user_id).await.mje()?.into())
5656
}

src/lib/database.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@ export class Database {
88
}
99
static async new() {
1010
const dexie = new Dexie("database");
11-
dexie.version(1).stores({ accounts: "&id,name,key,avatar" });
11+
dexie.version(1).stores({
12+
accounts: "&id,name,key,avatar"
13+
});
1214
return new Database(await dexie.open());
1315
}
14-
async add<T>(table: string, value: T) {
15-
await this.dexie.table<T>(table).add(value);
16+
async add(table: string, value: any) {
17+
await this.dexie.table(table).add(value);
1618
}
17-
async put<T>(table: string, value: T) {
18-
await this.dexie.table<T>(table).put(value);
19+
async put(table: string, value: any) {
20+
await this.dexie.table(table).put(value);
1921
}
20-
async get<T>(table: string, id: string) {
21-
return await this.dexie.table<T>(table).get(id);
22+
async get(table: string, id: string) {
23+
return await this.dexie.table<Record<string, any>>(table).get(id);
2224
}
23-
async get_all<T>(table: string) {
24-
return await this.dexie.table<T>(table).toArray();
25+
async get_all(table: string) {
26+
return await this.dexie.table<Record<string, any>>(table).toArray();
2527
}
26-
async delete() {
27-
await this.dexie.delete({ disableAutoOpen: false });
28+
async delete_record(table: string, id: string) {
29+
await this.dexie.table(table).delete(id);
30+
}
31+
async clear_all_table() {
32+
for (const table of this.dexie.tables) {
33+
await table.clear();
34+
}
35+
}
36+
async query(table: string, where: string, equals: string) {
37+
return await this.dexie.table<Record<string, any>>(table).where(where).equals(equals).first();
2838
}
2939
}

src/lib/network.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export class Network {
99
static async new(account: Account) {
1010
return new Network(await Net.new(account));
1111
}
12-
async shutdown() {
13-
await this.net.shutdown();
14-
}
1512
account() {
1613
return this.net.account();
1714
}
15+
async shutdown() {
16+
await this.net.shutdown();
17+
}
1818
async search_user(user_id: string) {
1919
return await this.net.search_user(user_id);
2020
}

src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { render } from "./render";
1+
import { render } from "./render"
22

33
async function main() {
4-
render();
4+
render()
55
}
6-
await main();
6+
await main()

src/render.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { StrictMode } from "react";
2-
import { createRoot } from "react-dom/client";
3-
import { RouterProvider, createHashHistory, createRouter } from "@tanstack/react-router";
4-
import { routeTree } from "./routeTree.gen";
1+
import { StrictMode } from "react"
2+
import { createRoot } from "react-dom/client"
3+
import { RouterProvider, createHashHistory, createRouter } from "@tanstack/react-router"
4+
import { routeTree } from "./routeTree.gen"
55

6-
const router = createRouter({ routeTree, history: createHashHistory() });
6+
const router = createRouter({ routeTree, history: createHashHistory() })
77
declare module "@tanstack/react-router" {
88
interface Register {
99
router: typeof router
@@ -13,5 +13,5 @@ declare module "@tanstack/react-router" {
1313
export function render() {
1414
createRoot(document.body).render(<StrictMode>
1515
<RouterProvider router={router} />
16-
</StrictMode>);
16+
</StrictMode>)
1717
}

src/routes/__root.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import "@/style.css";
2-
import { Toaster } from "@/components/ui/sonner";
3-
import { createRootRoute, HeadContent, Outlet } from "@tanstack/react-router";
4-
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
1+
import "@/style.css"
2+
import { Toaster } from "@/components/ui/sonner"
3+
import { createRootRoute, HeadContent, Outlet } from "@tanstack/react-router"
4+
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools"
55

66
export const Route = createRootRoute({
77
component: Component,
88
head: () => ({
99
meta: [{ title: "星链" }]
1010
})
11-
});
11+
})
1212

1313
function Component() {
1414
return <>
1515
<HeadContent />
1616
<Toaster />
1717
<Outlet />
1818
<TanStackRouterDevtools />
19-
</>;
19+
</>
2020
}

src/routes/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createFileRoute, Navigate } from "@tanstack/react-router";
1+
import { createFileRoute, Navigate } from "@tanstack/react-router"
22

33
export const Route = createFileRoute("/")({
44
component: () => <Navigate to="/window" />,
5-
});
5+
})

src/routes/window.tsx

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,64 @@
1-
import { createFileRoute, Navigate, Outlet } from "@tanstack/react-router";
2-
import { isTauri } from "@tauri-apps/api/core";
3-
import { getCurrentWindow } from "@tauri-apps/api/window";
4-
import { useEffect, useState } from "react";
1+
import { createFileRoute, Navigate, Outlet } from "@tanstack/react-router"
2+
import { isTauri } from "@tauri-apps/api/core"
3+
import { getCurrentWindow } from "@tauri-apps/api/window"
4+
import { useEffect, useState } from "react"
55

66
export const Route = createFileRoute("/window")({
77
component: Component,
88
pendingComponent: PendingComponent,
9-
});
9+
})
1010

1111
function Component() {
12-
const [is_maximized, set_is_maximized] = useState(false);
12+
const [is_maximized, set_is_maximized] = useState(false)
1313
useEffect(() => {
1414
if (isTauri()) {
1515
//设置窗口标题
16-
getCurrentWindow().setTitle(document.title).catch(console.error);
16+
getCurrentWindow().setTitle(document.title).catch(console.error)
1717
//监控网页标题变化
1818
const mutation_observer = new MutationObserver(async (mutations) => {
1919
for (const mutation of mutations) {
2020
if (mutation.type === "childList") {
21-
await getCurrentWindow().setTitle(document.title);
21+
await getCurrentWindow().setTitle(document.title)
2222
}
2323
}
24-
});
25-
mutation_observer.observe(document.querySelector("title")!, { childList: true });
24+
})
25+
mutation_observer.observe(document.querySelector("title")!, { childList: true })
2626
//监控窗口缩放
27-
const un_on_resized = getCurrentWindow().onResized(async () => set_is_maximized(await getCurrentWindow().isMaximized()));
27+
const un_on_resized = getCurrentWindow().onResized(async () => set_is_maximized(await getCurrentWindow().isMaximized()))
2828
return () => {
2929
mutation_observer.disconnect();
30-
(async () => (await un_on_resized)())();
31-
};
30+
(async () => (await un_on_resized)())()
31+
}
3232
}
33-
}, []);
34-
return <>
35-
<div className="w-screen h-screen flex flex-col">
36-
<div data-tauri-drag-region className="h-8 flex justify-end">
37-
{isTauri() && <>
38-
<div className="w-8 flex items-center justify-center hover:bg-neutral-200 active:bg-neutral-300 cursor-pointer"
39-
onClick={async () => await getCurrentWindow().minimize()}
40-
>
41-
<div className="icon-[mdi--window-minimize]" />
42-
</div>
43-
<div className="w-8 flex items-center justify-center hover:bg-neutral-200 active:bg-neutral-300 cursor-pointer"
44-
onClick={async () => await getCurrentWindow().toggleMaximize()}
45-
>
46-
{!is_maximized ? <div className="icon-[mdi--window-maximize]" /> : <div className="icon-[mdi--window-restore]" />}
47-
</div>
48-
<div className="w-8 flex items-center justify-center hover:bg-red-600 active:bg-red-500 cursor-pointer"
49-
onClick={async () => await getCurrentWindow().close()}
50-
>
51-
<div className="icon-[mdi--window-close]" />
52-
</div>
53-
</>}
54-
</div>
55-
<Outlet />
56-
<Navigate to="/window/login" />
33+
}, [])
34+
return <div className="w-screen h-screen flex flex-col">
35+
<div data-tauri-drag-region className="h-8 flex justify-end">
36+
{isTauri() && <>
37+
<div className="w-8 flex items-center justify-center hover:bg-neutral-200 active:bg-neutral-300 cursor-pointer"
38+
onClick={async () => await getCurrentWindow().minimize()}
39+
>
40+
<div className="icon-[mdi--window-minimize]" />
41+
</div>
42+
<div className="w-8 flex items-center justify-center hover:bg-neutral-200 active:bg-neutral-300 cursor-pointer"
43+
onClick={async () => await getCurrentWindow().toggleMaximize()}
44+
>
45+
{!is_maximized ? <div className="icon-[mdi--window-maximize]" /> : <div className="icon-[mdi--window-restore]" />}
46+
</div>
47+
<div className="w-8 flex items-center justify-center hover:bg-red-600 active:bg-red-500 cursor-pointer"
48+
onClick={async () => await getCurrentWindow().close()}
49+
>
50+
<div className="icon-[mdi--window-close]" />
51+
</div>
52+
</>}
5753
</div>
58-
</>;
54+
<Outlet />
55+
<Navigate to="/window/login" />
56+
</div>
5957
}
6058

6159
function PendingComponent() {
62-
return <>
63-
<div className="w-screen h-screen flex items-center justify-center gap-1">
64-
<div className="select-none font-bold">正在加载窗口</div>
65-
<div className="icon-[line-md--loading-loop] w-6 h-6" />
66-
</div>
67-
</>;
60+
return <div className="w-screen h-screen flex items-center justify-center gap-1">
61+
<div className="select-none font-bold">正在加载窗口</div>
62+
<div className="icon-[line-md--loading-loop] w-6 h-6" />
63+
</div>
6864
}

0 commit comments

Comments
 (0)