wip: convert tauri sql to prisma

This commit is contained in:
Ren Amamiya
2023-04-02 16:22:50 +07:00
parent 9b8a96c651
commit 39e7c9bf34
11 changed files with 228 additions and 162 deletions

View File

@@ -2,24 +2,27 @@ import BaseLayout from '@layouts/base';
import { activeAccountAtom } from '@stores/account';
import { getActiveAccount } from '@utils/storage';
import LumeSymbol from '@assets/icons/Lume';
import { useSetAtom } from 'jotai';
import { useRouter } from 'next/router';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useEffect } from 'react';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useCallback, useEffect } from 'react';
export default function Page() {
const router = useRouter();
const setActiveAccount = useSetAtom(activeAccountAtom);
const fetchActiveAccount = useCallback(async () => {
const { getAccount } = await import('@utils/bindings');
return await getAccount();
}, []);
useEffect(() => {
getActiveAccount()
fetchActiveAccount()
.then((res: any) => {
if (res) {
if (res.length > 0) {
// update local storage
setActiveAccount(res);
setActiveAccount(res[0]);
// redirect
router.replace('/init');
} else {
@@ -27,7 +30,7 @@ export default function Page() {
}
})
.catch(console.error);
}, [router, setActiveAccount]);
}, [fetchActiveAccount, setActiveAccount, router]);
return (
<div className="relative h-full overflow-hidden">

View File

@@ -2,13 +2,20 @@ import BaseLayout from '@layouts/base';
import { RelayContext } from '@components/relaysProvider';
import { createAccount } from '@utils/storage';
import { ArrowLeftIcon, EyeClosedIcon, EyeOpenIcon } from '@radix-ui/react-icons';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { generatePrivateKey, getEventHash, getPublicKey, nip19, signEvent } from 'nostr-tools';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useContext, useMemo, useState } from 'react';
import {
JSXElementConstructor,
ReactElement,
ReactFragment,
ReactPortal,
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import { Config, names, uniqueNamesGenerator } from 'unique-names-generator';
const config: Config = {
@@ -39,23 +46,11 @@ export default function Page() {
display_name: name,
name: name,
username: name.toLowerCase(),
picture: 'https://void.cat/d/KmypFh2fBdYCEvyJrPiN89',
picture: 'https://void.cat/d/KmypFh2fBdYCEvyJrPiN89.webp',
}),
[name]
);
// build profile
const data = useMemo(
() => ({
pubkey: pubKey,
privkey: privKey,
npub: npub,
nsec: nsec,
metadata: metadata,
}),
[metadata, npub, nsec, privKey, pubKey]
);
// toggle privatek key
const showPrivateKey = () => {
if (type === 'password') {
@@ -66,7 +61,8 @@ export default function Page() {
};
// create account and broadcast to all relays
const submit = () => {
const submit = useCallback(async () => {
const { createAccount } = await import('@utils/bindings');
setLoading(true);
// build event
@@ -81,16 +77,16 @@ export default function Page() {
event.sig = signEvent(event, privKey);
// insert to database then broadcast
createAccount(data)
.then(() => {
createAccount({ pubkey: pubKey, privkey: privKey, metadata: JSON.stringify(metadata) })
.then((res) => {
pool.publish(event, relays);
router.push({
pathname: '/onboarding/create/step-2',
query: { id: pubKey, privkey: privKey },
query: { id: res.id, privkey: res.privkey },
});
})
.catch(console.error);
};
}, [pool, pubKey, privKey, metadata, relays, router]);
return (
<div className="grid h-full w-full grid-rows-5">

View File

@@ -3,7 +3,7 @@ import BaseLayout from '@layouts/base';
import { RelayContext } from '@components/relaysProvider';
import { UserBase } from '@components/user/base';
import { createFollows } from '@utils/storage';
import { followsTag } from '@utils/transform';
import { CheckCircledIcon } from '@radix-ui/react-icons';
import { createClient } from '@supabase/supabase-js';
@@ -15,6 +15,7 @@ import {
ReactElement,
ReactFragment,
ReactPortal,
useCallback,
useContext,
useEffect,
useState,
@@ -76,18 +77,9 @@ export default function Page() {
setFollows(arr);
};
// build event tags
const tags = () => {
const arr = [];
// push item to tags
follows.forEach((item) => {
arr.push(['p', item]);
});
return arr;
};
// save follows to database then broadcast
const submit = () => {
const submit = useCallback(async () => {
const { createFollow } = await import('@utils/bindings');
setLoading(true);
// build event
@@ -96,21 +88,18 @@ export default function Page() {
created_at: Math.floor(Date.now() / 1000),
kind: 3,
pubkey: id,
tags: tags(),
tags: followsTag(follows),
};
event.id = getEventHash(event);
event.sig = signEvent(event, privkey);
createFollows(follows, id, 0)
.then((res) => {
if (res === 'ok') {
// publish to relays
pool.publish(event, relays);
router.replace('/');
}
})
.catch(console.error);
};
follows.forEach((item) => {
createFollow({ pubkey: item, kind: 0, metadata: JSON.stringify({}), account_id: id });
});
pool.publish(event, relays);
router.replace('/');
}, [follows, id, pool, privkey, relays, router]);
useEffect(() => {
const fetchData = async () => {

26
src/utils/bindings.ts Normal file
View File

@@ -0,0 +1,26 @@
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.
declare global {
interface Window {
__TAURI_INVOKE__<T>(cmd: string, args?: Record<string, unknown>): Promise<T>;
}
}
const invoke = window.__TAURI_INVOKE__;
export function getAccount() {
return invoke<Account[]>('get_account');
}
export function createAccount(data: CreateAccountData) {
return invoke<Account>('create_account', { data });
}
export function createFollow(data: CreateFollowData) {
return invoke<Follow>('create_follow', { data });
}
export type Account = { id: number; pubkey: string; privkey: string; active: boolean; metadata: string };
export type Follow = { id: number; pubkey: string; kind: number; metadata: string; accountId: number };
export type CreateFollowData = { pubkey: string; kind: number; metadata: string; account_id: number };
export type CreateAccountData = { pubkey: string; privkey: string; metadata: string };

View File

@@ -9,6 +9,15 @@ export const tagsToArray = (arr) => {
return newarr;
};
export const followsTag = (arr) => {
const newarr = [];
// push item to tags
arr.forEach((item) => {
arr.push(['p', item]);
});
return newarr;
};
export const pubkeyArray = (arr) => {
const newarr = [];
// push item to newarr