mirror of
https://github.com/lumehq/lume.git
synced 2025-03-29 03:02:14 +01:00
refactor user component and updated onboarding
This commit is contained in:
parent
39e7c9bf34
commit
33000979ed
@ -43,7 +43,7 @@ struct CreateFollowData {
|
|||||||
#[specta::specta]
|
#[specta::specta]
|
||||||
async fn get_account(db: DbState<'_>) -> Result<Vec<account::Data>, ()> {
|
async fn get_account(db: DbState<'_>) -> Result<Vec<account::Data>, ()> {
|
||||||
db.account()
|
db.account()
|
||||||
.find_many(vec![account::active::equals(true)])
|
.find_many(vec![account::active::equals(false)])
|
||||||
.exec()
|
.exec()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
@ -1,33 +1,22 @@
|
|||||||
import { ImageWithFallback } from '@components/imageWithFallback';
|
import { ImageWithFallback } from '@components/imageWithFallback';
|
||||||
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||||
|
|
||||||
import { createCacheProfile } from '@utils/storage';
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import { fetch } from '@tauri-apps/api/http';
|
import { Author } from 'nostr-relaypool';
|
||||||
import destr from 'destr';
|
import { memo, useContext, useEffect, useMemo, useState } from 'react';
|
||||||
import { memo, useCallback, useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) {
|
export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) {
|
||||||
const [profile, setProfile] = useState(null);
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
|
|
||||||
const fetchProfile = useCallback(async (id: string) => {
|
const [profile, setProfile] = useState(null);
|
||||||
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
|
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
|
||||||
method: 'GET',
|
|
||||||
timeout: 30,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchProfile(pubkey)
|
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
|
||||||
.then((res: any) => {
|
}, [user]);
|
||||||
setProfile(destr(res.content));
|
|
||||||
createCacheProfile(res.pubkey, res.content);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}, [fetchProfile, pubkey]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
|
@ -1,51 +1,34 @@
|
|||||||
import { ImageWithFallback } from '@components/imageWithFallback';
|
import { ImageWithFallback } from '@components/imageWithFallback';
|
||||||
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||||
|
|
||||||
import { createCacheProfile, getCacheProfile } from '@utils/storage';
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
|
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
|
||||||
import { fetch } from '@tauri-apps/api/http';
|
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import relativeTime from 'dayjs/plugin/relativeTime';
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
||||||
import destr from 'destr';
|
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { memo, useCallback, useEffect, useState } from 'react';
|
import { Author } from 'nostr-relaypool';
|
||||||
|
import { memo, useContext, useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
dayjs.extend(relativeTime);
|
dayjs.extend(relativeTime);
|
||||||
|
|
||||||
export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: string; time: any }) {
|
export const UserExtend = memo(function UserExtend({ pubkey, time }: { pubkey: string; time: any }) {
|
||||||
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const [profile, setProfile] = useState(null);
|
const [profile, setProfile] = useState(null);
|
||||||
|
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
|
||||||
|
|
||||||
const openUserPage = (e) => {
|
const openUserPage = (e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
router.push(`/users/${pubkey}`);
|
router.push(`/users/${pubkey}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchProfile = useCallback(async (id: string) => {
|
|
||||||
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
|
|
||||||
method: 'GET',
|
|
||||||
timeout: 30,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getCacheProfile(pubkey).then((res) => {
|
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
|
||||||
if (res) {
|
}, [user]);
|
||||||
setProfile(destr(res.metadata));
|
|
||||||
} else {
|
|
||||||
fetchProfile(pubkey)
|
|
||||||
.then((res: any) => {
|
|
||||||
setProfile(destr(res.content));
|
|
||||||
createCacheProfile(pubkey, res.content);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [fetchProfile, pubkey]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="group flex items-start gap-2">
|
<div className="group flex items-start gap-2">
|
||||||
|
@ -1,44 +1,27 @@
|
|||||||
import { ImageWithFallback } from '@components/imageWithFallback';
|
import { ImageWithFallback } from '@components/imageWithFallback';
|
||||||
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||||
|
|
||||||
import { createCacheProfile, getCacheProfile } from '@utils/storage';
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
|
import { DotsHorizontalIcon } from '@radix-ui/react-icons';
|
||||||
import { fetch } from '@tauri-apps/api/http';
|
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import relativeTime from 'dayjs/plugin/relativeTime';
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
||||||
import destr from 'destr';
|
import { Author } from 'nostr-relaypool';
|
||||||
import { memo, useCallback, useEffect, useState } from 'react';
|
import { memo, useContext, useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
dayjs.extend(relativeTime);
|
dayjs.extend(relativeTime);
|
||||||
|
|
||||||
export const UserLarge = memo(function UserLarge({ pubkey, time }: { pubkey: string; time: any }) {
|
export const UserLarge = memo(function UserLarge({ pubkey, time }: { pubkey: string; time: any }) {
|
||||||
const [profile, setProfile] = useState(null);
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
|
|
||||||
const fetchProfile = useCallback(async (id: string) => {
|
const [profile, setProfile] = useState(null);
|
||||||
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
|
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
|
||||||
method: 'GET',
|
|
||||||
timeout: 30,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getCacheProfile(pubkey).then((res) => {
|
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
|
||||||
if (res) {
|
}, [user]);
|
||||||
setProfile(destr(res.metadata));
|
|
||||||
} else {
|
|
||||||
fetchProfile(pubkey)
|
|
||||||
.then((res: any) => {
|
|
||||||
setProfile(destr(res.content));
|
|
||||||
createCacheProfile(pubkey, res.content);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [fetchProfile, pubkey]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
|
@ -1,35 +1,19 @@
|
|||||||
import { createCacheProfile, getCacheProfile } from '@utils/storage';
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import { fetch } from '@tauri-apps/api/http';
|
import { Author } from 'nostr-relaypool';
|
||||||
import destr from 'destr';
|
import { memo, useContext, useEffect, useMemo, useState } from 'react';
|
||||||
import { memo, useCallback, useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
export const UserMention = memo(function UserMention({ pubkey }: { pubkey: string }) {
|
export const UserMention = memo(function UserMention({ pubkey }: { pubkey: string }) {
|
||||||
const [profile, setProfile] = useState(null);
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
|
|
||||||
const fetchProfile = useCallback(async (id: string) => {
|
const [profile, setProfile] = useState(null);
|
||||||
const res = await fetch(`https://rbr.bio/${id}/metadata.json`, {
|
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
|
||||||
method: 'GET',
|
|
||||||
timeout: 30,
|
|
||||||
});
|
|
||||||
return res.data;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getCacheProfile(pubkey).then((res) => {
|
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
|
||||||
if (res) {
|
}, [user]);
|
||||||
setProfile(destr(res.metadata));
|
|
||||||
} else {
|
|
||||||
fetchProfile(pubkey)
|
|
||||||
.then((res: any) => {
|
|
||||||
setProfile(destr(res.content));
|
|
||||||
createCacheProfile(pubkey, res.content);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [fetchProfile, pubkey]);
|
|
||||||
|
|
||||||
return <span className="cursor-pointer text-fuchsia-500">@{profile?.name || truncate(pubkey, 16, ' .... ')}</span>;
|
return <span className="cursor-pointer text-fuchsia-500">@{profile?.name || truncate(pubkey, 16, ' .... ')}</span>;
|
||||||
});
|
});
|
||||||
|
@ -1,24 +1,22 @@
|
|||||||
import { ImageWithFallback } from '@components/imageWithFallback';
|
import { ImageWithFallback } from '@components/imageWithFallback';
|
||||||
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { DEFAULT_AVATAR } from '@stores/constants';
|
import { DEFAULT_AVATAR } from '@stores/constants';
|
||||||
|
|
||||||
import { getCacheProfile } from '@utils/storage';
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { Author } from 'nostr-relaypool';
|
||||||
|
import { useContext, useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
export const UserMini = ({ pubkey }: { pubkey: string }) => {
|
export const UserMini = ({ pubkey }: { pubkey: string }) => {
|
||||||
const [profile, setProfile] = useState(null);
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
|
|
||||||
const fetchCacheProfile = useCallback(async (id: string) => {
|
const [profile, setProfile] = useState(null);
|
||||||
const res = await getCacheProfile(id);
|
const user = useMemo(() => new Author(pool, relays, pubkey), [pubkey, pool, relays]);
|
||||||
const data = JSON.parse(res.metadata);
|
|
||||||
setProfile(data);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchCacheProfile(pubkey).catch(console.error);
|
user.metaData((res) => setProfile(JSON.parse(res.content)), 0);
|
||||||
}, [fetchCacheProfile, pubkey]);
|
}, [user]);
|
||||||
|
|
||||||
if (profile) {
|
if (profile) {
|
||||||
return (
|
return (
|
||||||
|
@ -82,7 +82,7 @@ export default function Page() {
|
|||||||
pool.publish(event, relays);
|
pool.publish(event, relays);
|
||||||
router.push({
|
router.push({
|
||||||
pathname: '/onboarding/create/step-2',
|
pathname: '/onboarding/create/step-2',
|
||||||
query: { id: res.id, privkey: res.privkey },
|
query: { id: res.id, pubkey: res.pubkey, privkey: res.privkey },
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
@ -3,6 +3,7 @@ import BaseLayout from '@layouts/base';
|
|||||||
import { RelayContext } from '@components/relaysProvider';
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
import { UserBase } from '@components/user/base';
|
import { UserBase } from '@components/user/base';
|
||||||
|
|
||||||
|
import { fetchMetadata } from '@utils/metadata';
|
||||||
import { followsTag } from '@utils/transform';
|
import { followsTag } from '@utils/transform';
|
||||||
|
|
||||||
import { CheckCircledIcon } from '@radix-ui/react-icons';
|
import { CheckCircledIcon } from '@radix-ui/react-icons';
|
||||||
@ -65,7 +66,7 @@ export default function Page() {
|
|||||||
const [pool, relays]: any = useContext(RelayContext);
|
const [pool, relays]: any = useContext(RelayContext);
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { id, privkey }: any = router.query || '';
|
const { id, pubkey, privkey }: any = router.query || '';
|
||||||
|
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [list, setList]: any = useState(initialList);
|
const [list, setList]: any = useState(initialList);
|
||||||
@ -82,24 +83,25 @@ export default function Page() {
|
|||||||
const { createFollow } = await import('@utils/bindings');
|
const { createFollow } = await import('@utils/bindings');
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
|
for (const follow of follows) {
|
||||||
|
const metadata: any = await fetchMetadata(follow, pool, relays);
|
||||||
|
createFollow({ pubkey: follow, kind: 0, metadata: metadata.content, account_id: parseInt(id) });
|
||||||
|
}
|
||||||
|
|
||||||
// build event
|
// build event
|
||||||
const event: any = {
|
const event: any = {
|
||||||
content: '',
|
content: '',
|
||||||
created_at: Math.floor(Date.now() / 1000),
|
created_at: Math.floor(Date.now() / 1000),
|
||||||
kind: 3,
|
kind: 3,
|
||||||
pubkey: id,
|
pubkey: pubkey,
|
||||||
tags: followsTag(follows),
|
tags: followsTag(follows),
|
||||||
};
|
};
|
||||||
event.id = getEventHash(event);
|
event.id = getEventHash(event);
|
||||||
event.sig = signEvent(event, privkey);
|
event.sig = signEvent(event, privkey);
|
||||||
|
|
||||||
follows.forEach((item) => {
|
|
||||||
createFollow({ pubkey: item, kind: 0, metadata: JSON.stringify({}), account_id: id });
|
|
||||||
});
|
|
||||||
|
|
||||||
pool.publish(event, relays);
|
pool.publish(event, relays);
|
||||||
router.replace('/');
|
router.replace('/');
|
||||||
}, [follows, id, pool, privkey, relays, router]);
|
}, [follows, id, pool, pubkey, privkey, relays, router]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
|
@ -2,21 +2,21 @@ import BaseLayout from '@layouts/base';
|
|||||||
|
|
||||||
import { RelayContext } from '@components/relaysProvider';
|
import { RelayContext } from '@components/relaysProvider';
|
||||||
|
|
||||||
import { createAccount, createFollows } from '@utils/storage';
|
import { fetchMetadata } from '@utils/metadata';
|
||||||
import { tagsToArray } from '@utils/transform';
|
|
||||||
import { truncate } from '@utils/truncate';
|
import { truncate } from '@utils/truncate';
|
||||||
|
|
||||||
import destr from 'destr';
|
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { getPublicKey, nip19 } from 'nostr-tools';
|
import { getPublicKey } from 'nostr-tools';
|
||||||
import {
|
import {
|
||||||
JSXElementConstructor,
|
JSXElementConstructor,
|
||||||
ReactElement,
|
ReactElement,
|
||||||
ReactFragment,
|
ReactFragment,
|
||||||
ReactPortal,
|
ReactPortal,
|
||||||
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
@ -30,6 +30,28 @@ export default function Page() {
|
|||||||
const [profile, setProfile] = useState(null);
|
const [profile, setProfile] = useState(null);
|
||||||
const [done, setDone] = useState(false);
|
const [done, setDone] = useState(false);
|
||||||
|
|
||||||
|
const accountId = useRef(null);
|
||||||
|
|
||||||
|
const insertAccountToStorage = useCallback(async (pubkey, privkey, metadata) => {
|
||||||
|
const { createAccount } = await import('@utils/bindings');
|
||||||
|
createAccount({ pubkey: pubkey, privkey: privkey, metadata: JSON.stringify(metadata) }).then(
|
||||||
|
(res) => (accountId.current = res.id)
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const insertFollowsToStorage = useCallback(
|
||||||
|
async (tags) => {
|
||||||
|
const { createFollow } = await import('@utils/bindings');
|
||||||
|
if (accountId.current !== null) {
|
||||||
|
for (const tag of tags) {
|
||||||
|
const metadata: any = await fetchMetadata(tag[1], pool, relays);
|
||||||
|
createFollow({ pubkey: tag[1], kind: 0, metadata: metadata.content, account_id: accountId.current });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[pool, relays]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = pool.subscribe(
|
const unsubscribe = pool.subscribe(
|
||||||
[
|
[
|
||||||
@ -42,18 +64,11 @@ export default function Page() {
|
|||||||
relays,
|
relays,
|
||||||
(event: any) => {
|
(event: any) => {
|
||||||
if (event.kind === 0) {
|
if (event.kind === 0) {
|
||||||
const data = {
|
setProfile(JSON.parse(event.content));
|
||||||
pubkey: pubkey,
|
insertAccountToStorage(pubkey, privkey, event.content);
|
||||||
privkey: privkey,
|
|
||||||
npub: nip19.npubEncode(pubkey),
|
|
||||||
nsec: nip19.nsecEncode(privkey),
|
|
||||||
metadata: event.content,
|
|
||||||
};
|
|
||||||
setProfile(destr(event.content));
|
|
||||||
createAccount(data);
|
|
||||||
} else {
|
} else {
|
||||||
if (event.tags.length > 0) {
|
if (event.tags.length > 0) {
|
||||||
createFollows(tagsToArray(event.tags), pubkey, 0);
|
insertFollowsToStorage(event.tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -69,7 +84,7 @@ export default function Page() {
|
|||||||
return () => {
|
return () => {
|
||||||
unsubscribe;
|
unsubscribe;
|
||||||
};
|
};
|
||||||
}, [pool, privkey, pubkey, relays]);
|
}, [insertAccountToStorage, insertFollowsToStorage, pool, relays, privkey, pubkey]);
|
||||||
|
|
||||||
// submit then redirect to home
|
// submit then redirect to home
|
||||||
const submit = () => {
|
const submit = () => {
|
||||||
|
6
src/utils/metadata.tsx
Normal file
6
src/utils/metadata.tsx
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { Author } from 'nostr-relaypool';
|
||||||
|
|
||||||
|
export const fetchMetadata = (pubkey: string, pool: any, relays: any) => {
|
||||||
|
const author = new Author(pool, relays, pubkey);
|
||||||
|
return new Promise((resolve) => author.metaData(resolve, 0));
|
||||||
|
};
|
@ -13,7 +13,7 @@ export const followsTag = (arr) => {
|
|||||||
const newarr = [];
|
const newarr = [];
|
||||||
// push item to tags
|
// push item to tags
|
||||||
arr.forEach((item) => {
|
arr.forEach((item) => {
|
||||||
arr.push(['p', item]);
|
newarr.push(['p', item]);
|
||||||
});
|
});
|
||||||
return newarr;
|
return newarr;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user