add initial data page
This commit is contained in:
@@ -3,7 +3,6 @@ import { ChannelBlackList } from '@lume/shared/channels/channelBlackList';
|
|||||||
import { ChannelProfile } from '@lume/shared/channels/channelProfile';
|
import { ChannelProfile } from '@lume/shared/channels/channelProfile';
|
||||||
import { UpdateChannelModal } from '@lume/shared/channels/updateChannelModal';
|
import { UpdateChannelModal } from '@lume/shared/channels/updateChannelModal';
|
||||||
import { FormChannel } from '@lume/shared/form/channel';
|
import { FormChannel } from '@lume/shared/form/channel';
|
||||||
import NewsfeedLayout from '@lume/shared/layouts/newsfeed';
|
|
||||||
import { RelayContext } from '@lume/shared/relaysProvider';
|
import { RelayContext } from '@lume/shared/relaysProvider';
|
||||||
import { channelMessagesAtom, channelReplyAtom } from '@lume/stores/channel';
|
import { channelMessagesAtom, channelReplyAtom } from '@lume/stores/channel';
|
||||||
import { FULL_RELAYS } from '@lume/stores/constants';
|
import { FULL_RELAYS } from '@lume/stores/constants';
|
||||||
@@ -80,7 +79,6 @@ export function Page() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NewsfeedLayout>
|
|
||||||
<div className="flex h-full flex-col justify-between gap-2">
|
<div className="flex h-full flex-col justify-between gap-2">
|
||||||
<div className="flex h-11 w-full shrink-0 items-center justify-between">
|
<div className="flex h-11 w-full shrink-0 items-center justify-between">
|
||||||
<div>
|
<div>
|
||||||
@@ -100,6 +98,5 @@ export function Page() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</NewsfeedLayout>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { AccountContext } from '@lume/shared/accountProvider';
|
import { AccountContext } from '@lume/shared/accountProvider';
|
||||||
import { MessageListItem } from '@lume/shared/chats/messageListItem';
|
import { MessageListItem } from '@lume/shared/chats/messageListItem';
|
||||||
import FormChat from '@lume/shared/form/chat';
|
import FormChat from '@lume/shared/form/chat';
|
||||||
import NewsfeedLayout from '@lume/shared/layouts/newsfeed';
|
|
||||||
import { RelayContext } from '@lume/shared/relaysProvider';
|
import { RelayContext } from '@lume/shared/relaysProvider';
|
||||||
import { FULL_RELAYS } from '@lume/stores/constants';
|
import { FULL_RELAYS } from '@lume/stores/constants';
|
||||||
import { usePageContext } from '@lume/utils/hooks/usePageContext';
|
import { usePageContext } from '@lume/utils/hooks/usePageContext';
|
||||||
@@ -46,7 +45,6 @@ export function Page() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NewsfeedLayout>
|
|
||||||
<div className="relative flex h-full w-full flex-col justify-between rounded-lg border border-zinc-800 bg-zinc-900 shadow-input shadow-black/20">
|
<div className="relative flex h-full w-full flex-col justify-between rounded-lg border border-zinc-800 bg-zinc-900 shadow-input shadow-black/20">
|
||||||
<div className="scrollbar-hide flex h-full w-full flex-col-reverse overflow-y-auto">
|
<div className="scrollbar-hide flex h-full w-full flex-col-reverse overflow-y-auto">
|
||||||
{error && <div>failed to load</div>}
|
{error && <div>failed to load</div>}
|
||||||
@@ -81,6 +79,5 @@ export function Page() {
|
|||||||
<FormChat receiverPubkey={pubkey} />
|
<FormChat receiverPubkey={pubkey} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</NewsfeedLayout>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,153 +1,25 @@
|
|||||||
import LumeIcon from '@lume/shared/icons/lume';
|
import LumeIcon from '@lume/shared/icons/lume';
|
||||||
import { RelayContext } from '@lume/shared/relaysProvider';
|
import { getActiveAccount } from '@lume/utils/storage';
|
||||||
import { READONLY_RELAYS } from '@lume/stores/constants';
|
|
||||||
import { dateToUnix, hoursAgo } from '@lume/utils/getDate';
|
|
||||||
import {
|
|
||||||
addToBlacklist,
|
|
||||||
countTotalNotes,
|
|
||||||
createChat,
|
|
||||||
createNote,
|
|
||||||
getActiveAccount,
|
|
||||||
getLastLogin,
|
|
||||||
updateLastLogin,
|
|
||||||
} from '@lume/utils/storage';
|
|
||||||
import { getParentID } from '@lume/utils/transform';
|
|
||||||
|
|
||||||
import { useContext, useEffect, useRef } from 'react';
|
import useSWR from 'swr';
|
||||||
import { navigate } from 'vite-plugin-ssr/client/router';
|
import { navigate } from 'vite-plugin-ssr/client/router';
|
||||||
|
|
||||||
|
const fetcher = () => getActiveAccount();
|
||||||
|
|
||||||
export function Page() {
|
export function Page() {
|
||||||
const pool: any = useContext(RelayContext);
|
const { data, isLoading } = useSWR('account', fetcher, {
|
||||||
const now = useRef(new Date());
|
revalidateIfStale: false,
|
||||||
|
revalidateOnFocus: false,
|
||||||
useEffect(() => {
|
revalidateOnReconnect: false,
|
||||||
let unsubscribe: () => void;
|
|
||||||
let timeout: any;
|
|
||||||
|
|
||||||
const fetchInitalData = async (account: { pubkey: string; id: number }, tags: string) => {
|
|
||||||
const lastLogin = await getLastLogin();
|
|
||||||
const notes = await countTotalNotes();
|
|
||||||
|
|
||||||
const follows = JSON.parse(tags);
|
|
||||||
const query = [];
|
|
||||||
|
|
||||||
let since: number;
|
|
||||||
|
|
||||||
if (notes.total === 0) {
|
|
||||||
since = dateToUnix(hoursAgo(24, now.current));
|
|
||||||
} else {
|
|
||||||
if (parseInt(lastLogin) > 0) {
|
|
||||||
since = parseInt(lastLogin);
|
|
||||||
} else {
|
|
||||||
since = dateToUnix(hoursAgo(24, now.current));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// kind 1 (notes) query
|
|
||||||
query.push({
|
|
||||||
kinds: [1, 6],
|
|
||||||
authors: follows,
|
|
||||||
since: since,
|
|
||||||
until: dateToUnix(now.current),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// kind 4 (chats) query
|
if (!isLoading && !data) {
|
||||||
query.push({
|
|
||||||
kinds: [4],
|
|
||||||
'#p': [account.pubkey],
|
|
||||||
since: 0,
|
|
||||||
until: dateToUnix(now.current),
|
|
||||||
});
|
|
||||||
|
|
||||||
// kind 43, 43 (mute user, hide message) query
|
|
||||||
query.push({
|
|
||||||
authors: [account.pubkey],
|
|
||||||
kinds: [43, 44],
|
|
||||||
since: 0,
|
|
||||||
until: dateToUnix(now.current),
|
|
||||||
});
|
|
||||||
|
|
||||||
// subscribe relays
|
|
||||||
unsubscribe = pool.subscribe(
|
|
||||||
query,
|
|
||||||
READONLY_RELAYS,
|
|
||||||
(event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => {
|
|
||||||
switch (event.kind) {
|
|
||||||
// short text note
|
|
||||||
case 1:
|
|
||||||
const parentID = getParentID(event.tags, event.id);
|
|
||||||
// insert event to local database
|
|
||||||
createNote(
|
|
||||||
event.id,
|
|
||||||
account.id,
|
|
||||||
event.pubkey,
|
|
||||||
event.kind,
|
|
||||||
event.tags,
|
|
||||||
event.content,
|
|
||||||
event.created_at,
|
|
||||||
parentID
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
// chat
|
|
||||||
case 4:
|
|
||||||
if (event.pubkey !== account.pubkey) {
|
|
||||||
createChat(account.id, event.pubkey, event.created_at);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// repost
|
|
||||||
case 6:
|
|
||||||
createNote(
|
|
||||||
event.id,
|
|
||||||
account.id,
|
|
||||||
event.pubkey,
|
|
||||||
event.kind,
|
|
||||||
event.tags,
|
|
||||||
event.content,
|
|
||||||
event.created_at,
|
|
||||||
event.id
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
// hide message (channel only)
|
|
||||||
case 43:
|
|
||||||
if (event.tags[0][0] === 'e') {
|
|
||||||
addToBlacklist(account.id, event.tags[0][1], 43, 1);
|
|
||||||
}
|
|
||||||
// mute user (channel only)
|
|
||||||
case 44:
|
|
||||||
if (event.tags[0][0] === 'p') {
|
|
||||||
addToBlacklist(account.id, event.tags[0][1], 44, 1);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
undefined,
|
|
||||||
() => {
|
|
||||||
updateLastLogin(dateToUnix(now.current));
|
|
||||||
timeout = setTimeout(() => {
|
|
||||||
navigate('/app/newsfeed/following', { overwriteLastHistoryEntry: true });
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
getActiveAccount()
|
|
||||||
.then((res: any) => {
|
|
||||||
if (res) {
|
|
||||||
fetchInitalData(res, res.follows);
|
|
||||||
} else {
|
|
||||||
navigate('/auth', { overwriteLastHistoryEntry: true });
|
navigate('/auth', { overwriteLastHistoryEntry: true });
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
|
|
||||||
return () => {
|
if (!isLoading && data) {
|
||||||
if (unsubscribe) {
|
navigate('/app/inital-data', { overwriteLastHistoryEntry: true });
|
||||||
unsubscribe();
|
|
||||||
}
|
}
|
||||||
clearTimeout(timeout);
|
|
||||||
};
|
|
||||||
}, [pool]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white">
|
<div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white">
|
||||||
|
|||||||
182
src/app/inital-data/pages/index.page.tsx
Normal file
182
src/app/inital-data/pages/index.page.tsx
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
import LumeIcon from '@lume/shared/icons/lume';
|
||||||
|
import { FULL_RELAYS } from '@lume/stores/constants';
|
||||||
|
import { dateToUnix, hoursAgo } from '@lume/utils/getDate';
|
||||||
|
import {
|
||||||
|
addToBlacklist,
|
||||||
|
countTotalNotes,
|
||||||
|
createChat,
|
||||||
|
createNote,
|
||||||
|
getActiveAccount,
|
||||||
|
getLastLogin,
|
||||||
|
updateLastLogin,
|
||||||
|
} from '@lume/utils/storage';
|
||||||
|
import { getParentID, nip02ToArray } from '@lume/utils/transform';
|
||||||
|
|
||||||
|
import { RelayPool } from 'nostr-relaypool';
|
||||||
|
import { useEffect, useRef } from 'react';
|
||||||
|
import { navigate } from 'vite-plugin-ssr/client/router';
|
||||||
|
|
||||||
|
export function Page() {
|
||||||
|
const now = useRef(new Date());
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let unsubscribe: () => void;
|
||||||
|
let timeout: any;
|
||||||
|
|
||||||
|
const fetchInitalData = async () => {
|
||||||
|
const account = await getActiveAccount();
|
||||||
|
const lastLogin = await getLastLogin();
|
||||||
|
const notes = await countTotalNotes();
|
||||||
|
|
||||||
|
const pool = new RelayPool(FULL_RELAYS);
|
||||||
|
const follows = nip02ToArray(JSON.parse(account.follows));
|
||||||
|
const query = [];
|
||||||
|
|
||||||
|
let since: number;
|
||||||
|
|
||||||
|
if (notes === 0) {
|
||||||
|
since = dateToUnix(hoursAgo(24, now.current));
|
||||||
|
} else {
|
||||||
|
if (parseInt(lastLogin) > 0) {
|
||||||
|
since = parseInt(lastLogin);
|
||||||
|
} else {
|
||||||
|
since = dateToUnix(hoursAgo(24, now.current));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// kind 1 (notes) query
|
||||||
|
query.push({
|
||||||
|
kinds: [1, 6],
|
||||||
|
authors: follows,
|
||||||
|
since: since,
|
||||||
|
until: dateToUnix(now.current),
|
||||||
|
});
|
||||||
|
|
||||||
|
// kind 4 (chats) query
|
||||||
|
query.push({
|
||||||
|
kinds: [4],
|
||||||
|
'#p': [account.pubkey],
|
||||||
|
since: 0,
|
||||||
|
until: dateToUnix(now.current),
|
||||||
|
});
|
||||||
|
|
||||||
|
// kind 43, 43 (mute user, hide message) query
|
||||||
|
query.push({
|
||||||
|
authors: [account.pubkey],
|
||||||
|
kinds: [43, 44],
|
||||||
|
since: 0,
|
||||||
|
until: dateToUnix(now.current),
|
||||||
|
});
|
||||||
|
|
||||||
|
// subscribe relays
|
||||||
|
unsubscribe = pool.subscribe(
|
||||||
|
query,
|
||||||
|
FULL_RELAYS,
|
||||||
|
(event: any) => {
|
||||||
|
switch (event.kind) {
|
||||||
|
// short text note
|
||||||
|
case 1:
|
||||||
|
const parentID = getParentID(event.tags, event.id);
|
||||||
|
// insert event to local database
|
||||||
|
createNote(
|
||||||
|
event.id,
|
||||||
|
account.id,
|
||||||
|
event.pubkey,
|
||||||
|
event.kind,
|
||||||
|
event.tags,
|
||||||
|
event.content,
|
||||||
|
event.created_at,
|
||||||
|
parentID
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
// chat
|
||||||
|
case 4:
|
||||||
|
if (event.pubkey !== account.pubkey) {
|
||||||
|
createChat(account.id, event.pubkey, event.created_at);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// repost
|
||||||
|
case 6:
|
||||||
|
createNote(
|
||||||
|
event.id,
|
||||||
|
account.id,
|
||||||
|
event.pubkey,
|
||||||
|
event.kind,
|
||||||
|
event.tags,
|
||||||
|
event.content,
|
||||||
|
event.created_at,
|
||||||
|
event.id
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
// hide message (channel only)
|
||||||
|
case 43:
|
||||||
|
if (event.tags[0][0] === 'e') {
|
||||||
|
addToBlacklist(account.id, event.tags[0][1], 43, 1);
|
||||||
|
}
|
||||||
|
// mute user (channel only)
|
||||||
|
case 44:
|
||||||
|
if (event.tags[0][0] === 'p') {
|
||||||
|
addToBlacklist(account.id, event.tags[0][1], 44, 1);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
() => {
|
||||||
|
updateLastLogin(dateToUnix(now.current));
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
navigate('/app/newsfeed/following', { overwriteLastHistoryEntry: true });
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchInitalData().catch(console.error);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (unsubscribe) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
clearTimeout(timeout);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white">
|
||||||
|
<div className="relative h-full overflow-hidden">
|
||||||
|
{/* dragging area */}
|
||||||
|
<div data-tauri-drag-region className="absolute left-0 top-0 z-20 h-16 w-full bg-transparent" />
|
||||||
|
{/* end dragging area */}
|
||||||
|
<div className="relative flex h-full flex-col items-center justify-center">
|
||||||
|
<div className="flex flex-col items-center gap-2">
|
||||||
|
<LumeIcon className="h-16 w-16 text-black dark:text-white" />
|
||||||
|
<div className="text-center">
|
||||||
|
<h3 className="text-lg font-semibold leading-tight text-zinc-900 dark:text-zinc-100">
|
||||||
|
Here's an interesting fact:
|
||||||
|
</h3>
|
||||||
|
<p className="font-medium text-zinc-300 dark:text-zinc-600">
|
||||||
|
Bitcoin and Nostr can be used by anyone, and no one can stop you!
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="absolute bottom-16 left-1/2 -translate-x-1/2 transform">
|
||||||
|
<svg
|
||||||
|
className="h-5 w-5 animate-spin text-black dark:text-white"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||||
|
<path
|
||||||
|
className="opacity-75"
|
||||||
|
fill="currentColor"
|
||||||
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||||
|
></path>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,19 +1,9 @@
|
|||||||
import { AccountContext } from '@lume/shared/accountProvider';
|
import { AccountContext } from '@lume/shared/accountProvider';
|
||||||
import { ChatListItem } from '@lume/shared/chats/chatListItem';
|
|
||||||
import { ChatModal } from '@lume/shared/chats/chatModal';
|
import { ChatModal } from '@lume/shared/chats/chatModal';
|
||||||
import { DEFAULT_AVATAR } from '@lume/stores/constants';
|
import { DEFAULT_AVATAR } from '@lume/stores/constants';
|
||||||
|
|
||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
|
|
||||||
let list: any = [];
|
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
const { getChats, getActiveAccount } = await import('@lume/utils/storage');
|
|
||||||
const activeAccount = await getActiveAccount();
|
|
||||||
|
|
||||||
list = await getChats(activeAccount.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ChatList() {
|
export default function ChatList() {
|
||||||
const activeAccount: any = useContext(AccountContext);
|
const activeAccount: any = useContext(AccountContext);
|
||||||
const profile = typeof window !== 'undefined' ? JSON.parse(activeAccount.metadata) : null;
|
const profile = typeof window !== 'undefined' ? JSON.parse(activeAccount.metadata) : null;
|
||||||
@@ -37,9 +27,6 @@ export default function ChatList() {
|
|||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
{list.map((item: { id: string; pubkey: string }) => (
|
|
||||||
<ChatListItem key={item.id} pubkey={item.pubkey} />
|
|
||||||
))}
|
|
||||||
<ChatModal />
|
<ChatModal />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import ActiveLink from '@lume/shared/activeLink';
|
import ActiveLink from '@lume/shared/activeLink';
|
||||||
import ChannelList from '@lume/shared/channels/channelList';
|
import ChannelList from '@lume/shared/channels/channelList';
|
||||||
import ChatList from '@lume/shared/chats/chatList';
|
|
||||||
|
|
||||||
import { Disclosure } from '@headlessui/react';
|
import { Disclosure } from '@headlessui/react';
|
||||||
import { Bonfire, NavArrowUp, PeopleTag } from 'iconoir-react';
|
import { Bonfire, NavArrowUp, PeopleTag } from 'iconoir-react';
|
||||||
@@ -80,9 +79,7 @@ export default function Navigation() {
|
|||||||
</div>
|
</div>
|
||||||
<h3 className="text-[11px] font-bold uppercase tracking-widest text-zinc-600">Chats</h3>
|
<h3 className="text-[11px] font-bold uppercase tracking-widest text-zinc-600">Chats</h3>
|
||||||
</Disclosure.Button>
|
</Disclosure.Button>
|
||||||
<Disclosure.Panel>
|
<Disclosure.Panel></Disclosure.Panel>
|
||||||
<ChatList />
|
|
||||||
</Disclosure.Panel>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Disclosure>
|
</Disclosure>
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ export async function countTotalChannels() {
|
|||||||
export async function countTotalNotes() {
|
export async function countTotalNotes() {
|
||||||
const db = await connect();
|
const db = await connect();
|
||||||
const result = await db.select('SELECT COUNT(*) AS "total" FROM notes;');
|
const result = await db.select('SELECT COUNT(*) AS "total" FROM notes;');
|
||||||
return result[0];
|
return result[0].total;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all notes
|
// get all notes
|
||||||
|
|||||||
Reference in New Issue
Block a user