update dependencies & clean up
This commit is contained in:
@@ -4,30 +4,23 @@ import { ChatsListItem } from '@app/chat/components/item';
|
||||
import { NewMessageModal } from '@app/chat/components/modal';
|
||||
import { ChatsListSelfItem } from '@app/chat/components/self';
|
||||
|
||||
import { getChatsByPubkey } from '@libs/storage';
|
||||
import { getChats } from '@libs/storage';
|
||||
|
||||
import { useAccount } from '@utils/hooks/useAccount';
|
||||
|
||||
export function ChatsList() {
|
||||
const { account } = useAccount();
|
||||
|
||||
const {
|
||||
status,
|
||||
data: chats,
|
||||
isFetching,
|
||||
} = useQuery(
|
||||
['chats'],
|
||||
async () => {
|
||||
const chats = await getChatsByPubkey(account.pubkey);
|
||||
const sorted = chats.sort(
|
||||
(a, b) => parseInt(a.new_messages) - parseInt(b.new_messages)
|
||||
);
|
||||
return sorted;
|
||||
},
|
||||
{
|
||||
enabled: account ? true : false,
|
||||
}
|
||||
);
|
||||
} = useQuery(['chats'], async () => {
|
||||
const chats = await getChats();
|
||||
const sorted = chats.sort(
|
||||
(a, b) => parseInt(a.new_messages) - parseInt(b.new_messages)
|
||||
);
|
||||
return sorted;
|
||||
});
|
||||
|
||||
if (status === 'loading') {
|
||||
return (
|
||||
|
||||
@@ -66,6 +66,33 @@ export function Root() {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUsersProfile() {
|
||||
const authors = [];
|
||||
|
||||
users.forEach((user) => {
|
||||
if (user.sender_pubkey) {
|
||||
authors.push(user.sender_pubkey);
|
||||
} else {
|
||||
authors.push(user.pubkey);
|
||||
}
|
||||
});
|
||||
|
||||
const filter: NDKFilter = {
|
||||
authors: authors,
|
||||
kinds: [0],
|
||||
};
|
||||
|
||||
const events = await ndk.fetchEvents(filter);
|
||||
|
||||
events.forEach((event: NDKEvent) => {
|
||||
const profile = JSON.parse(event.content);
|
||||
profile['image'] = profile.picture;
|
||||
queryClient.setQueryData(['user', event.pubkey], profile);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async function fetchChats() {
|
||||
try {
|
||||
const sendFilter: NDKFilter = {
|
||||
@@ -101,34 +128,6 @@ export function Root() {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUsersProfile() {
|
||||
const authors = [];
|
||||
|
||||
users.forEach((user) => {
|
||||
if (user.sender_pubkey) {
|
||||
authors.push(user.sender_pubkey);
|
||||
} else {
|
||||
authors.push(user.pubkey);
|
||||
}
|
||||
});
|
||||
|
||||
const filter: NDKFilter = {
|
||||
authors: authors,
|
||||
kinds: [0],
|
||||
};
|
||||
|
||||
const events = await ndk.fetchEvents(filter);
|
||||
console.log('authors', events);
|
||||
|
||||
events.forEach((event: NDKEvent) => {
|
||||
const profile = JSON.parse(event.content);
|
||||
profile['image'] = profile.picture;
|
||||
queryClient.setQueryData(['user', event.pubkey], profile);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
async function fetchChannelMessages() {
|
||||
try {
|
||||
@@ -172,15 +171,12 @@ export function Root() {
|
||||
useEffect(() => {
|
||||
async function prefetch() {
|
||||
const notes = await fetchNotes();
|
||||
if (notes) {
|
||||
const chats = await fetchChats();
|
||||
const users = await fetchUsersProfile();
|
||||
// const channels = await fetchChannelMessages();
|
||||
if (chats && users) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
await updateLastLogin(now);
|
||||
navigate('/app/space', { replace: true });
|
||||
}
|
||||
const chats = await fetchChats();
|
||||
const users = await fetchUsersProfile();
|
||||
if (notes && users && chats) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
await updateLastLogin(now);
|
||||
navigate('/app/space', { replace: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -269,10 +269,11 @@ export async function getChannelUsers(channel_id: string) {
|
||||
}
|
||||
|
||||
// get all chats by pubkey
|
||||
export async function getChatsByPubkey(pubkey: string) {
|
||||
export async function getChats() {
|
||||
const db = await connect();
|
||||
const account = await getActiveAccount();
|
||||
const result: any = await db.select(
|
||||
`SELECT DISTINCT sender_pubkey FROM chats WHERE receiver_pubkey = "${pubkey}" ORDER BY created_at DESC;`
|
||||
`SELECT DISTINCT sender_pubkey FROM chats WHERE receiver_pubkey = "${account.pubkey}" ORDER BY created_at DESC;`
|
||||
);
|
||||
const newArr: any = result.map((v) => ({ ...v, new_messages: 0 }));
|
||||
return newArr;
|
||||
|
||||
12
src/main.tsx
12
src/main.tsx
@@ -1,7 +1,5 @@
|
||||
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister';
|
||||
import { QueryClient } from '@tanstack/react-query';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
||||
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
|
||||
import { getSetting } from '@libs/storage';
|
||||
@@ -20,18 +18,14 @@ const queryClient = new QueryClient({
|
||||
},
|
||||
});
|
||||
|
||||
const persister = createSyncStoragePersister({
|
||||
storage: window.localStorage,
|
||||
});
|
||||
|
||||
const container = document.getElementById('root');
|
||||
const root = createRoot(container);
|
||||
|
||||
root.render(
|
||||
<PersistQueryClientProvider client={queryClient} persistOptions={{ persister }}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RelayProvider>
|
||||
<App />
|
||||
</RelayProvider>
|
||||
<ReactQueryDevtools initialIsOpen={false} position="top-right" />
|
||||
</PersistQueryClientProvider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user