import { Anchor, AspectRatio, Avatar, Box, Flex, PasswordInput, Skeleton, Text, Transition} from "@mantine/core";
import classes from './Lockscreen.module.css'
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import useWindow from "@/app/hooks/useWindow";
import { decodeWithPassword, generateHashFromPrivateKey } from "@/app/crypto/crypto";
import { useAccountProvider } from "@/app/providers/AccountProvider/useAccountProvider";
import { Account, AccountBase } from "@/app/providers/AccountProvider/AccountProvider";
import { useUserCache } from "@/app/providers/InformationProvider/useUserCache";
import { useRosettaColors } from "@/app/hooks/useRosettaColors";
import { IconArrowsExchange, IconFingerprint } from "@tabler/icons-react";
import { SIZE_LOGIN_WIDTH_PX } from "@/app/constants";
import { modals } from "@mantine/modals";
import { useAvatars } from "@/app/providers/AvatarProvider/useAvatars";
import { AnimatedButton } from "@/app/components/AnimatedButton/AnimatedButton";
import { DiceDropdown } from "@/app/components/DiceDropdown/DiceDropdown";
import { dotCenterIfNeeded } from "@/app/utils/utils";
export function Lockscreen() {
const [password, setPassword] = useState("");
const navigate = useNavigate();
const { setSize, setResizeble } = useWindow();
const { allAccounts, selectAccountToLoginDice, loginDiceAccount, loginAccount } = useAccountProvider();
/**
* Добавлено условие ИЛИ для того, чтобы аккаунт отображался
* даже если пользователь в кэше не найден
*/
const userInfo = useUserCache(loginDiceAccount.publicKey) || {
title: loginDiceAccount.publicKey,
};
const [error, setError] = useState(false);
const colors = useRosettaColors();
const avatars = useAvatars(loginDiceAccount.publicKey);
const [mounted, setMounted] = useState(false);
useEffect(() => {
setSize(385, 555);
setResizeble(false);
setTimeout(() => setMounted(true), 100);
}, []);
useEffect(() => {
if (loginDiceAccount.publicKey == "" && allAccounts.length <= 0) {
navigate("/");
return;
}
}, [loginDiceAccount])
const onUnlockPressed = async () => {
try {
const decryptedHex = await decodeWithPassword(password, loginDiceAccount.privateKey);
const privateKeyHash = await generateHashFromPrivateKey(decryptedHex);
const account: Account = {
privateKey: loginDiceAccount.privateKey,
publicKey: loginDiceAccount.publicKey,
privateHash: privateKeyHash,
privatePlain: decryptedHex,
seedPhraseEncrypted: loginDiceAccount.seedPhraseEncrypted
};
loginAccount(account);
navigate("/main");
} catch (e) {
setError(true);
}
}
const createAccount = () => {
modals.openConfirmModal({
title: 'Create account',
centered: true,
children: (
You may be create new account or import existing
),
withCloseButton: false,
labels: { confirm: 'Create new', cancel: "Import" },
cancelProps: {
autoFocus: false,
style: {
outline: 'none'
}
},
onCancel: () => {
navigate("/exists-seed");
},
onConfirm: () => {
navigate("/create-seed");
}
});
}
const selectAccount = (account: AccountBase) => {
selectAccountToLoginDice(account);
}
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Enter") {
event.preventDefault();
onUnlockPressed();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [onUnlockPressed]);
return (
{userInfo && (
0 ? avatars[0].avatar : undefined} size={80} color={'initials'} name={userInfo.title}>
{dotCenterIfNeeded(userInfo.title, 20, 5)}
For unlock account enter password
)}
{!userInfo && (
)}
{(styles) => (
{ setPassword(e.target.value); setError(false) }}
size="md"
w={SIZE_LOGIN_WIDTH_PX}
error={error && "Invalid password"}
styles={{
input: {
border: '0px solid ' + colors.borderColor,
backgroundColor: colors.mainColor
},
error: {
color: colors.error
}
}}
/>
} animated={['#2DA5FF', '#87DBFF']} size={'md'} onClick={onUnlockPressed}>Enter
)}
{(styles) => (
You can also navigate('/exists-seed')}>recover your password or create a new account.
)}
)
}