Moved from useEffect to useMemo on PasswordGeneratorModal and remove it from secret-share

This commit is contained in:
carlosmonastyrski
2025-04-02 15:27:05 -03:00
parent 0088217fa9
commit d8313a161e
2 changed files with 38 additions and 41 deletions
@@ -1,8 +1,9 @@
import { useEffect, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState } from "react";
import { faCopy, faKey, faRefresh } from "@fortawesome/free-solid-svg-icons"; import { faCheck, faCopy, faKey, faRefresh } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Button, Checkbox, IconButton, Slider } from "@app/components/v2"; import { Button, Checkbox, IconButton, Slider } from "@app/components/v2";
import { useTimedReset } from "@app/hooks";
type PasswordOptionsType = { type PasswordOptionsType = {
length: number; length: number;
@@ -24,11 +25,13 @@ const PasswordGeneratorModal = ({
isOpen, isOpen,
onClose, onClose,
onUsePassword, onUsePassword,
minLength = 6, minLength = 12,
maxLength = 32 maxLength = 64
}: PasswordGeneratorModalProps) => { }: PasswordGeneratorModalProps) => {
const [password, setPassword] = useState(""); const [copyText, isCopying, setCopyText] = useTimedReset<string>({
const [copied, setCopied] = useState(false); initialState: "Copy"
});
const [refresh, setRefresh] = useState(false);
const [passwordOptions, setPasswordOptions] = useState<PasswordOptionsType>({ const [passwordOptions, setPasswordOptions] = useState<PasswordOptionsType>({
length: minLength, length: minLength,
useUppercase: true, useUppercase: true,
@@ -61,8 +64,7 @@ const PasswordGeneratorModal = ({
newPassword += availableChars[randomIndex]; newPassword += availableChars[randomIndex];
} }
setPassword(newPassword); return newPassword;
setCopied(false);
}; };
useEffect(() => { useEffect(() => {
@@ -81,14 +83,20 @@ const PasswordGeneratorModal = ({
return () => {}; return () => {};
}, [isOpen, onClose]); }, [isOpen, onClose]);
useEffect(() => { const password = useMemo(() => {
generatePassword(); return generatePassword();
}, [passwordOptions]); }, [
passwordOptions.length,
passwordOptions.useUppercase,
passwordOptions.useLowercase,
passwordOptions.useNumbers,
passwordOptions.useSpecialChars,
refresh
]);
const copyToClipboard = () => { const copyToClipboard = () => {
navigator.clipboard.writeText(password).then(() => { navigator.clipboard.writeText(password).then(() => {
setCopied(true); setCopyText("Copied");
setTimeout(() => setCopied(false), 2000);
}); });
}; };
@@ -105,7 +113,7 @@ const PasswordGeneratorModal = ({
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 p-4"> <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 p-4">
<div <div
ref={modalRef} ref={modalRef}
className="w-full max-w-md rounded-lg border border-mineshaft-600 bg-mineshaft-800 shadow-xl" className="w-full max-w-lg rounded-lg border border-mineshaft-600 bg-mineshaft-800 shadow-xl"
> >
<div className="p-6"> <div className="p-6">
<h2 className="mb-1 text-xl font-semibold text-bunker-200">Password Generator</h2> <h2 className="mb-1 text-xl font-semibold text-bunker-200">Password Generator</h2>
@@ -119,7 +127,7 @@ const PasswordGeneratorModal = ({
size="xs" size="xs"
colorSchema="secondary" colorSchema="secondary"
variant="outline_bg" variant="outline_bg"
onClick={generatePassword} onClick={() => setRefresh((prev) => !prev)}
className="w-full text-bunker-300 hover:text-bunker-100" className="w-full text-bunker-300 hover:text-bunker-100"
> >
<FontAwesomeIcon icon={faRefresh} className="mr-1 h-3 w-3" /> <FontAwesomeIcon icon={faRefresh} className="mr-1 h-3 w-3" />
@@ -133,8 +141,8 @@ const PasswordGeneratorModal = ({
onClick={copyToClipboard} onClick={copyToClipboard}
className="w-full text-bunker-300 hover:text-bunker-100" className="w-full text-bunker-300 hover:text-bunker-100"
> >
<FontAwesomeIcon icon={faCopy} className="mr-1 h-3 w-3" /> <FontAwesomeIcon icon={isCopying ? faCheck : faCopy} className="mr-1 h-3 w-3" />
{copied ? "Copied!" : "Copy"} {copyText}
</Button> </Button>
</div> </div>
</div> </div>
@@ -235,8 +243,8 @@ export type PasswordGeneratorProps = {
export const PasswordGenerator = ({ export const PasswordGenerator = ({
onUsePassword, onUsePassword,
isDisabled = false, isDisabled = false,
minLength = 6, minLength = 12,
maxLength = 32 maxLength = 64
}: PasswordGeneratorProps) => { }: PasswordGeneratorProps) => {
const [showGenerator, setShowGenerator] = useState(false); const [showGenerator, setShowGenerator] = useState(false);
@@ -6,15 +6,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod"; import { z } from "zod";
import { createNotification } from "@app/components/notifications"; import { createNotification } from "@app/components/notifications";
import { import { Button, FormControl, IconButton, Input, Select, SelectItem } from "@app/components/v2";
Button,
FormControl,
IconButton,
Input,
PasswordGenerator,
Select,
SelectItem
} from "@app/components/v2";
import { useTimedReset } from "@app/hooks"; import { useTimedReset } from "@app/hooks";
import { useCreatePublicSharedSecret, useCreateSharedSecret } from "@app/hooks/api"; import { useCreatePublicSharedSecret, useCreateSharedSecret } from "@app/hooks/api";
import { SecretSharingAccessType } from "@app/hooks/api/secretSharing"; import { SecretSharingAccessType } from "@app/hooks/api/secretSharing";
@@ -175,19 +167,16 @@ export const ShareSecretForm = ({
errorText={error?.message} errorText={error?.message}
isOptional isOptional
> >
<div className="flex items-center gap-2"> <Input
<Input {...field}
{...field} placeholder="Password"
placeholder="Password" type="password"
type="password" autoComplete="new-password"
autoComplete="new-password" autoCorrect="off"
autoCorrect="off" spellCheck="false"
spellCheck="false" aria-autocomplete="none"
aria-autocomplete="none" data-form-type="other"
data-form-type="other" />
/>
<PasswordGenerator onUsePassword={field.onChange} />
</div>
</FormControl> </FormControl>
)} )}
/> />