Skip to content

Commit becbe52

Browse files
committed
copy key from cache resource because less request to server
1 parent ef42a06 commit becbe52

File tree

6 files changed

+58
-45
lines changed

6 files changed

+58
-45
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "electron-gp",
33
"private": true,
4-
"version": "0.2.10",
4+
"version": "0.2.11",
55
"type": "module",
66
"main": "dist-main/app.js",
77
"author": "traeop",

src/main/app/window.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { menu } from "../config.js";
1818
options: {
1919
resizable: isDev(),
2020
show: false,
21-
width: 280,
21+
width: 320,
2222
height: 500,
2323
},
2424
})
@@ -59,12 +59,11 @@ export class AppWindow implements TWindowManager {
5959
}
6060

6161
private async checkAuthenticated(window: BrowserWindow) {
62-
const response = await this.authService.checkAuthenticated(window);
63-
if (response !== undefined) {
64-
ipcWebContentsSend("authSocialNetwork", window.webContents, {
65-
isAuthenticated: response.isAuthenticated,
66-
});
67-
}
62+
const result = await this.authService.checkAuthenticated(window);
63+
ipcWebContentsSend("authSocialNetwork", window.webContents, {
64+
isAuthenticated:
65+
result !== undefined && result.isAuthenticated !== undefined,
66+
});
6867
}
6968

7069
private buildTray(window: BrowserWindow): void {

src/main/auth/service.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,33 @@ export class AuthService {
5555
ipcWebContentsSend("sync", window.webContents, {
5656
isAuthenticated: cacheAccess.ok,
5757
});
58-
}
59-
60-
ipcWebContentsSend("sync", window.webContents, {
61-
isAuthenticated: false,
62-
});
63-
64-
const response = await this.access();
65-
if (response !== undefined) {
66-
ipcWebContentsSend("sync", window.webContents, {
67-
isAuthenticated: response.ok,
68-
});
6958

7059
return {
7160
isAuthenticated: true,
7261
};
73-
} else {
74-
ipcWebContentsSend("sync", window.webContents, {
75-
isAuthenticated: true,
76-
});
7762
}
7863
}
7964

8065
setCheckAccessInterval(window: BrowserWindow) {
8166
const interval = setInterval(async () => {
82-
await this.checkAuthenticated(window);
67+
ipcWebContentsSend("sync", window.webContents, {
68+
isAuthenticated: false,
69+
});
70+
71+
const response = await this.access();
72+
if (response !== undefined) {
73+
ipcWebContentsSend("sync", window.webContents, {
74+
isAuthenticated: response.ok,
75+
});
76+
77+
return {
78+
isAuthenticated: true,
79+
};
80+
} else {
81+
ipcWebContentsSend("sync", window.webContents, {
82+
isAuthenticated: true,
83+
});
84+
}
8385

8486
const authToken = getElectronStorage("authToken");
8587
if (authToken === undefined) {

src/main/master-key/ipc.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BrowserWindow, clipboard } from "electron";
22
import { IpcHandler } from "../@core/decorators/ipc-handler.js";
33
import { getWindow as getWindows } from "../@core/control-window/receive.js";
44
import { TParamOnInit } from "../@core/types/ipc-handler.js";
5-
import { ResourcesService } from "../resources/services/resources.js";
65
import { CryptoService } from "../crypto/service.js";
76
import {
87
ipcMainHandle,
@@ -14,15 +13,13 @@ import {
1413
getElectronStorage,
1514
deleteFromElectronStorage,
1615
} from "../$shared/store.js";
16+
import { restApi } from "../config.js";
1717

1818
@IpcHandler()
1919
export class MasterKeyIpc {
2020
masterKeyWindow: BrowserWindow | undefined;
2121

22-
constructor(
23-
private resourcesService: ResourcesService,
24-
private cryptoService: CryptoService
25-
) {}
22+
constructor(private cryptoService: CryptoService) {}
2623

2724
onInit({ getWindow }: TParamOnInit<TWindows["masterKey"]>): void {
2825
const masterKeyWindow = getWindow("window:master-key");
@@ -50,21 +47,43 @@ export class MasterKeyIpc {
5047
});
5148
}
5249

50+
private cacheResources(): TResource[] | undefined {
51+
let resources: TResource[] | undefined = undefined;
52+
const cacheResponse = getElectronStorage("response");
53+
54+
if (cacheResponse !== undefined) {
55+
resources =
56+
cacheResponse[
57+
`${restApi.urls.base}${restApi.urls.baseApi}${restApi.urls.resources.base}`
58+
];
59+
}
60+
61+
if (resources !== undefined) {
62+
return resources;
63+
}
64+
65+
return undefined;
66+
}
67+
5368
private ipcCopyMasterKey(): void {
5469
ipcMainHandle("copyMasterKey", async (payload) => {
5570
if (payload !== undefined) {
56-
const resource = await this.resourcesService.byId(payload.id);
71+
const cacheResources = this.cacheResources();
72+
const foundUser =
73+
cacheResources !== undefined
74+
? cacheResources.find((res) => res.id + "" === payload.id)
75+
: undefined;
5776
const masterKey = getElectronStorage("masterKey");
5877

5978
if (
6079
masterKey !== undefined &&
61-
resource !== undefined &&
62-
resource.salt !== null
80+
foundUser !== undefined &&
81+
foundUser.salt !== null
6382
) {
6483
const encryptedVault = await this.cryptoService.decrypt(masterKey, {
65-
iv: resource.iv,
66-
salt: resource.salt,
67-
encryptedData: resource.key,
84+
iv: foundUser.iv,
85+
salt: foundUser.salt,
86+
encryptedData: foundUser.key,
6887
});
6988
clipboard.writeText(encryptedVault);
7089

src/renderer/ui-business/Resources/components/SubmitCopyKeyButton.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@ import { memo } from "react";
22
import { useFormStatus } from "react-dom";
33
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
44
import IconButton, { IconButtonProps } from "@mui/material/IconButton";
5-
import { useControlContext } from "../hooks/useControlContext";
65

76
export const SubmitButton = memo(({ ...other }: IconButtonProps) => {
8-
const { isDisabledActions } = useControlContext();
97
const { pending } = useFormStatus();
108

119
return (
12-
<IconButton
13-
disabled={pending || isDisabledActions}
14-
loading={pending}
15-
type="submit"
16-
{...other}
17-
>
10+
<IconButton loading={pending} type="submit" {...other}>
1811
<ContentCopyIcon fontSize="small" />
1912
</IconButton>
2013
);

src/renderer/windows/home/components/Resources.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const Container = memo(({ children }: { children: React.ReactElement }) => {
1515
<Stack
1616
spacing={2}
1717
direction="row"
18-
sx={{ flexWrap: "wrap", width: "87%", height: "100%" }}
18+
sx={{ flexWrap: "wrap", width: "100%", height: "100%" }}
1919
useFlexGap
2020
>
2121
<Typography
@@ -32,7 +32,7 @@ const Container = memo(({ children }: { children: React.ReactElement }) => {
3232
return (
3333
<Box
3434
sx={{
35-
width: "87%",
35+
width: "100%",
3636
height: "calc(100vh - 80px)",
3737
overflowY: "auto",
3838
overflowX: "hidden",

0 commit comments

Comments
 (0)