wip: use new storage layer

This commit is contained in:
Ren Amamiya
2023-08-15 08:29:04 +07:00
parent adca37223c
commit 6e28bcdb96
19 changed files with 182 additions and 104 deletions

View File

@@ -4,7 +4,7 @@ import { useEffect, useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { createAccount } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { LoaderIcon } from '@shared/icons';
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
@@ -31,6 +31,8 @@ const resolver: Resolver<FormValues> = async (values) => {
};
export function ImportStep1Screen() {
const { db } = useStorage();
const queryClient = useQueryClient();
const navigate = useNavigate();
const setPrivkey = useStronghold((state) => state.setPrivkey);
@@ -47,10 +49,10 @@ export function ImportStep1Screen() {
follows: null | string[];
is_active: number | boolean;
}) => {
return createAccount(data.npub, data.pubkey, null, 1);
return db.createAccount(data.npub, data.pubkey);
},
onSuccess: (data) => {
queryClient.setQueryData(['currentAccount'], data);
queryClient.setQueryData(['account'], data);
},
});
@@ -87,7 +89,7 @@ export function ImportStep1Screen() {
});
// redirect to step 2
setTimeout(() => navigate('/auth/import/step-2', { replace: true }), 1200);
navigate('/auth/import/step-2', { replace: true });
}
} catch (error) {
setError('privkey', {

View File

@@ -1,15 +1,17 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { useEffect, useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { useStorage } from '@libs/storage/provider';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
import { useOnboarding } from '@stores/onboarding';
import { useStronghold } from '@stores/stronghold';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
type FormValues = {
password: string;
};
@@ -37,7 +39,7 @@ export function ImportStep2Screen() {
const [passwordInput, setPasswordInput] = useState('password');
const [loading, setLoading] = useState(false);
const { save } = useSecureStorage();
const { db } = useStorage();
// toggle private key
const showPassword = () => {
@@ -58,8 +60,13 @@ export function ImportStep2Screen() {
const onSubmit = async (data: { [x: string]: string }) => {
setLoading(true);
if (data.password.length > 3) {
const dir = await appConfigDir();
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
db.secureDB = stronghold;
// save privkey to secure storage
await save(pubkey, privkey, data.password);
await db.secureSave(pubkey, privkey);
// redirect to next step
navigate('/auth/import/step-3', { replace: true });

View File

@@ -4,7 +4,7 @@ import { useNavigate } from 'react-router-dom';
import { User } from '@app/auth/components/user';
import { updateLastLogin } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons';
@@ -20,22 +20,27 @@ export function ImportStep3Screen() {
const [loading, setLoading] = useState(false);
const { db } = useStorage();
const { status, account } = useAccount();
const { fetchNotes, fetchChats } = useNostr();
const { fetchUserData } = useNostr();
const submit = async () => {
try {
// show loading indicator
setLoading(true);
const now = Math.floor(Date.now() / 1000);
await fetchNotes();
await fetchChats();
await updateLastLogin(now);
const data = await fetchUserData();
queryClient.invalidateQueries(['currentAccount']);
if (data.status === 'ok') {
// update last login
await db.updateLastLogin(Math.floor(Date.now() / 1000));
navigate('/auth/onboarding/step-2', { replace: true });
queryClient.invalidateQueries(['account']);
navigate('/auth/onboarding/step-2', { replace: true });
} else {
console.log('error: ', data.message);
setLoading(false);
}
} catch (e) {
console.log('error: ', e);
setLoading(false);