Skip to content

Commit 019070c

Browse files
committed
custom key
1 parent 2bbfddd commit 019070c

File tree

13 files changed

+98
-29
lines changed

13 files changed

+98
-29
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.17",
4+
"version": "0.2.18",
55
"type": "module",
66
"main": "dist-main/app.js",
77
"author": "traeop",

src/main/resources/ipc/actions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class ResourcesActionsIpc implements TIpcHandlerInterface {
5959
masterKey !== undefined &&
6060
payload !== undefined &&
6161
typeof payload.key === "string" &&
62+
payload.key.length &&
6263
payload.key.length
6364
) {
6465
encryptedVault = await this.cryptoService.encrypt(
@@ -110,7 +111,8 @@ export class ResourcesActionsIpc implements TIpcHandlerInterface {
110111
masterKey !== undefined &&
111112
payload !== undefined &&
112113
payload.key &&
113-
typeof payload.key === "string"
114+
typeof payload.key === "string" &&
115+
payload.key.length
114116
) {
115117
encryptedVault = await this.cryptoService.encrypt(
116118
masterKey,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useState } from "react";
2+
import FormControlLabel from "@mui/material/FormControlLabel";
3+
import Checkbox from "@mui/material/Checkbox";
4+
import TextField from "@mui/material/TextField";
5+
import { useControlContextComponents } from "../hooks/useControlContext";
6+
7+
export const CheckboxGenerateKey = () => {
8+
const { renderGenerateCharacters } = useControlContextComponents();
9+
const [isCheck, setCheck] = useState(false);
10+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
11+
setCheck(event.target.checked);
12+
};
13+
14+
return (
15+
<>
16+
<FormControlLabel
17+
control={
18+
<Checkbox
19+
checked={isCheck}
20+
onChange={handleChange}
21+
name="generate-key"
22+
/>
23+
}
24+
label="Generate key"
25+
/>
26+
27+
{isCheck ? (
28+
renderGenerateCharacters
29+
) : (
30+
<TextField type="text" variant="outlined" name="password" fullWidth />
31+
)}
32+
</>
33+
);
34+
};

src/renderer/ui-business/AddResource/components/Form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useActionState, memo } from "react";
22
import Stack from "@mui/material/Stack";
33
import TextField from "@mui/material/TextField";
44
import { SubmitButton } from "./SubmitButton";
5-
import type { TPropsForm } from "./types";
65
import { useControl } from "../hooks/useControl";
76
import { useControlContext } from "../hooks/useControlContext";
7+
import { CheckboxGenerateKey } from "./CheckboxGenerateKey";
88

9-
export const Form = memo(({ renderGenerateCharacters }: TPropsForm) => {
9+
export const Form = memo(() => {
1010
const { name } = useControlContext();
1111
const { handleTextInputChange, submitFormAction } = useControl();
1212
const [_, formAction] = useActionState(submitFormAction, undefined);
@@ -23,7 +23,7 @@ export const Form = memo(({ renderGenerateCharacters }: TPropsForm) => {
2323
fullWidth
2424
/>
2525

26-
{renderGenerateCharacters}
26+
<CheckboxGenerateKey />
2727

2828
<SubmitButton />
2929
</Stack>

src/renderer/ui-business/AddResource/components/Provider.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { useState, useMemo } from "react";
22
import type { TPropsProvider } from "./types";
3-
import { Context, ContextActions } from "../context";
3+
import { Context, ContextActions, ContextComponents } from "../context";
44

5-
export const Provider = ({ children }: TPropsProvider) => {
5+
export const Provider = ({
6+
children,
7+
renderGenerateCharacters,
8+
}: TPropsProvider) => {
69
const [name, setName] = useState("");
710

811
const value = useMemo(
@@ -19,11 +22,20 @@ export const Provider = ({ children }: TPropsProvider) => {
1922
[setName]
2023
);
2124

25+
const components = useMemo(
26+
() => ({
27+
renderGenerateCharacters,
28+
}),
29+
[renderGenerateCharacters]
30+
);
31+
2232
return (
23-
<Context.Provider value={value}>
24-
<ContextActions.Provider value={actions}>
25-
{children}
26-
</ContextActions.Provider>
27-
</Context.Provider>
33+
<ContextComponents.Provider value={components}>
34+
<Context.Provider value={value}>
35+
<ContextActions.Provider value={actions}>
36+
{children}
37+
</ContextActions.Provider>
38+
</Context.Provider>
39+
</ContextComponents.Provider>
2840
);
2941
};
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1+
import { ReactElement, ReactNode } from "react";
12
import { IconButtonProps } from "@mui/material/IconButton";
2-
import { ReactNode, ReactElement } from "react";
3-
4-
export type TPropsForm = {
5-
renderGenerateCharacters: ReactElement;
6-
};
73

84
export type TPropsProvider = {
95
children: ReactNode;
6+
renderGenerateCharacters: ReactElement;
107
};
118

129
export type TPropsButtonProvider = IconButtonProps & {};
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { createContext } from "react";
2-
import type { TContext, TContextActions } from "./type";
2+
import type { TContext, TContextActions, TContextComponents } from "./type";
33

44
export const Context = createContext<TContext | undefined>(undefined);
55
export const ContextActions = createContext<TContextActions | undefined>(
66
undefined
77
);
8+
9+
export const ContextComponents = createContext<TContextComponents | undefined>(
10+
undefined
11+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import { ReactElement } from "react";
2+
13
export type TContext = {
24
name: string;
35
};
46

57
export type TContextActions = {
68
setName: React.Dispatch<React.SetStateAction<string>>;
79
};
10+
11+
export type TContextComponents = {
12+
renderGenerateCharacters: ReactElement;
13+
};

src/renderer/ui-business/AddResource/hooks/useControlContext.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useContext } from "react";
2-
import { Context, ContextActions } from "../context";
2+
import { Context, ContextActions, ContextComponents } from "../context";
33

44
export const useControlContext = () => {
55
const context = useContext(Context);
@@ -18,3 +18,12 @@ export const useControlContextActions = () => {
1818
}
1919
return context;
2020
};
21+
22+
export const useControlContextComponents = () => {
23+
const context = useContext(ContextComponents);
24+
25+
if (!context) {
26+
throw new Error("ContextComponents must be used inside Provider");
27+
}
28+
return context;
29+
};

0 commit comments

Comments
 (0)