fix some errors cause app crash

This commit is contained in:
Ren Amamiya
2023-08-19 08:56:19 +07:00
parent c85502e427
commit eda18f8c34
14 changed files with 265 additions and 253 deletions

View File

@@ -76,6 +76,9 @@ export function OnboardStep3Screen() {
}
}
// update last login
await db.updateLastLogin();
clearStep();
navigate('/', { replace: true });
} catch (e) {

View File

@@ -46,14 +46,6 @@ export function UnlockScreen() {
} = useForm<FormValues>({ resolver });
const onSubmit = async (data: { [x: string]: string }) => {
if (data.password.length < 3) {
setError('password', {
type: 'custom',
message: 'Password is required and must be greater than 3',
});
return;
}
try {
setLoading(true);
@@ -89,7 +81,7 @@ export function UnlockScreen() {
<div className="flex flex-col gap-1">
<div className="relative">
<input
{...register('password', { required: true })}
{...register('password', { required: true, minLength: 4 })}
type={'password'}
placeholder="Password"
className="relative h-12 w-full rounded-lg bg-white/10 py-1 text-center text-white !outline-none placeholder:text-white/50"

View File

@@ -60,6 +60,9 @@ export class LumeStorage {
if (typeof account.network === 'string')
account.network = JSON.parse(account.network);
if (typeof account.last_login_at === 'string')
account.last_login_at = parseInt(account.last_login_at);
this.account = account;
return account;
} else {
@@ -94,9 +97,10 @@ export class LumeStorage {
}
public async updateLastLogin() {
const now = Math.floor(Date.now() / 1000);
return await this.db.execute(
'UPDATE accounts SET last_login_at = $1 WHERE id = $2;',
[Math.floor(Date.now() / 1000), this.account.id]
[now, this.account.id]
);
}
@@ -133,13 +137,14 @@ export class LumeStorage {
id: string,
event: string,
author: string,
kind: number,
root_id: string,
reply_id: string,
created_at: number
) {
return await this.db.execute(
'INSERT OR IGNORE INTO events (id, account_id, event, author, root_id, reply_id, created_at) VALUES ($1, $2, $3, $4, $5, $6, $7);',
[id, this.account.id, event, author, root_id, reply_id, created_at]
'INSERT OR IGNORE INTO events (id, account_id, event, author, kind, root_id, reply_id, created_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8);',
[id, this.account.id, event, author, kind, root_id, reply_id, created_at]
);
}

View File

@@ -29,8 +29,9 @@ export function NoteContent({ content, long }: { content: RichContent; long?: bo
components={{
del: ({ children }) => {
const key = children[0] as string;
if (key.startsWith('pub')) return <MentionUser pubkey={key.slice(3)} />;
if (key.startsWith('tag')) return <Hashtag tag={key.slice(3)} />;
if (key.startsWith('pub') && key.length > 50 && key.length < 100)
return <MentionUser pubkey={key.replace('pub-', '')} />;
if (key.startsWith('tag')) return <Hashtag tag={key.replace('tag-', '')} />;
},
}}
>

View File

@@ -63,7 +63,12 @@ export function useNostr() {
}
// build user's network
const events = await ndk.fetchEvents({ kinds: [3], authors: [...follows] });
const events = await ndk.fetchEvents({
kinds: [3],
authors: [...follows],
limit: 300,
});
events.forEach((event: NDKEvent) => {
event.tags.forEach((tag) => {
if (tag[0] === 'p') lruNetwork.set(tag[1], tag[1]);
@@ -88,12 +93,11 @@ export function useNostr() {
try {
if (!ndk) return { status: 'failed', data: [], message: 'NDK instance not found' };
// setup nostr-fetch
const fetcher = NostrFetcher.withCustomPool(ndkAdapter(ndk));
const dbEventsEmpty = await db.isEventsEmpty();
let since: number;
if (dbEventsEmpty) {
if (dbEventsEmpty || db.account.last_login_at === 0) {
since = nHoursAgo(24);
} else {
since = db.account.last_login_at ?? nHoursAgo(24);
@@ -125,6 +129,7 @@ export function useNostr() {
event.id,
JSON.stringify(event),
event.pubkey,
event.kind,
root,
reply,
event.created_at

View File

@@ -1,19 +1,20 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
export function useProfile(pubkey: string, fallback?: string) {
export function useProfile(pubkey: string, embed?: string) {
const { ndk } = useNDK();
const {
status,
data: user,
error,
isFetching,
} = useQuery(
['user', pubkey],
async () => {
if (!fallback) {
const user = ndk.getUser({ hexpubkey: pubkey });
if (!embed) {
const cleanPubkey = pubkey.replace('-', '');
const user = ndk.getUser({ hexpubkey: cleanPubkey });
await user.fetchProfile();
if (user.profile) {
user.profile.display_name = user.profile.displayName;
@@ -22,7 +23,7 @@ export function useProfile(pubkey: string, fallback?: string) {
throw new Error('User not found');
}
} else {
const profile = JSON.parse(fallback);
const profile: NDKUserProfile = JSON.parse(embed);
return profile;
}
},
@@ -35,5 +36,5 @@ export function useProfile(pubkey: string, fallback?: string) {
}
);
return { status, user, error, isFetching };
return { status, user, error };
}

View File

@@ -43,7 +43,6 @@ export function useImageUploader() {
if (res.ok) {
const url =
res.file?.metadata?.url ?? `https://void.cat/d/${res.file?.id}.${filetype}`;
console.log(url);
if (nip94) {
const tags = [

View File

@@ -18,6 +18,24 @@ export function parser(event: NDKEvent) {
links: [],
};
// parse nostr references
references?.forEach((item) => {
const profile = item.profile;
const event = item.event;
const addr = item.address;
if (event) {
content.notes.push(event.id);
content.parsed = content.parsed.replace(item.text, '');
}
if (profile) {
content.parsed = content.parsed.replace(item.text, `~pub-${item.profile.pubkey}~`);
}
if (addr) {
content.notes.push(addr.identifier);
content.parsed = content.parsed.replace(item.text, '');
}
});
// parse urls
urls?.forEach((url: string) => {
if (url.match(/\.(jpg|jpeg|gif|png|webp|avif)$/)) {
@@ -47,25 +65,7 @@ export function parser(event: NDKEvent) {
// parse hashtag
const hashtags = content.parsed.split(/\s/gm).filter((s) => s.startsWith('#'));
hashtags?.forEach((tag) => {
content.parsed = content.parsed.replace(tag, `~tag${tag}~`);
});
// parse nostr
references?.forEach((item) => {
const profile = item.profile;
const event = item.event;
const addr = item.address;
if (event) {
content.notes.push(event.id);
content.parsed = content.parsed.replace(item.text, '');
}
if (profile) {
content.parsed = content.parsed.replace(item.text, `~pub${item.profile.pubkey}~`);
}
if (addr) {
content.notes.push(addr.identifier);
content.parsed = content.parsed.replace(item.text, '');
}
content.parsed = content.parsed.replace(tag, ` ~tag-${tag}~ `);
});
return content;

View File

@@ -13,6 +13,7 @@ export interface DBEvent {
account_id: number;
event: string | NDKEvent;
author: string;
kind: number;
root_id: string;
reply_id: string;
created_at: number;