diff --git a/src/components/note/meta/comment.tsx b/src/components/note/meta/comment.tsx
index 21df6f1d..7388ffc8 100644
--- a/src/components/note/meta/comment.tsx
+++ b/src/components/note/meta/comment.tsx
@@ -1,3 +1,4 @@
+import { AccountContext } from '@components/accountProvider';
import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend';
@@ -6,7 +7,6 @@ import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate';
import * as Dialog from '@radix-ui/react-dialog';
-import useLocalStorage from '@rehooks/local-storage';
import { ChatLines, OpenNewWindow } from 'iconoir-react';
import { getEventHash, signEvent } from 'nostr-tools';
import { useContext, useState } from 'react';
@@ -26,12 +26,12 @@ export const NoteComment = ({
eventContent: any;
}) => {
const pool: any = useContext(RelayContext);
+ const activeAccount: any = useContext(AccountContext);
const [open, setOpen] = useState(false);
const [value, setValue] = useState('');
- const [activeAccount]: any = useLocalStorage('account', {});
- const profile = activeAccount.metadata ? JSON.parse(activeAccount.metadata) : null;
+ const profile = activeAccount ? JSON.parse(activeAccount.metadata) : null;
const openThread = () => {
navigate(`/newsfeed/note?id=${eventID}`);
diff --git a/src/components/note/meta/reaction.tsx b/src/components/note/meta/reaction.tsx
index 80b6b007..aca22eb2 100644
--- a/src/components/note/meta/reaction.tsx
+++ b/src/components/note/meta/reaction.tsx
@@ -1,10 +1,10 @@
+import { AccountContext } from '@components/accountProvider';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
import { dateToUnix } from '@utils/getDate';
-import useLocalStorage from '@rehooks/local-storage';
import { Heart } from 'iconoir-react';
import { getEventHash, signEvent } from 'nostr-tools';
import { useContext, useEffect, useState } from 'react';
@@ -21,7 +21,7 @@ export const NoteReaction = ({
eventPubkey: string;
}) => {
const pool: any = useContext(RelayContext);
- const [activeAccount]: any = useLocalStorage('account', {});
+ const activeAccount: any = useContext(AccountContext);
const [isReact, setIsReact] = useState(false);
const [like, setLike] = useState(0);
diff --git a/src/components/note/metadata.tsx b/src/components/note/metadata.tsx
index cdf60453..66419c7a 100644
--- a/src/components/note/metadata.tsx
+++ b/src/components/note/metadata.tsx
@@ -1,10 +1,10 @@
+import { AccountContext } from '@components/accountProvider';
import { NoteComment } from '@components/note/meta/comment';
import { NoteReaction } from '@components/note/meta/reaction';
import { RelayContext } from '@components/relaysProvider';
import { DEFAULT_RELAYS } from '@stores/constants';
-import useLocalStorage from '@rehooks/local-storage';
import { memo, useContext, useEffect, useState } from 'react';
export const NoteMetadata = memo(function NoteMetadata({
@@ -19,7 +19,7 @@ export const NoteMetadata = memo(function NoteMetadata({
eventContent: any;
}) {
const pool: any = useContext(RelayContext);
- const [activeAccount]: any = useLocalStorage('account', {});
+ const activeAccount: any = useContext(AccountContext);
const [liked, setLiked] = useState(false);
const [likeCount, setLikeCount] = useState(0);
diff --git a/src/components/note/parent.tsx b/src/components/note/parent.tsx
index 78592e33..695a38e1 100644
--- a/src/components/note/parent.tsx
+++ b/src/components/note/parent.tsx
@@ -1,3 +1,4 @@
+import { AccountContext } from '@components/accountProvider';
import { NoteMetadata } from '@components/note/metadata';
import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend';
@@ -8,15 +9,13 @@ import { contentParser } from '@utils/parser';
import { createNote, getNoteByID } from '@utils/storage';
import { getParentID } from '@utils/transform';
-import useLocalStorage from '@rehooks/local-storage';
import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
const pool: any = useContext(RelayContext);
+ const activeAccount: any = useContext(AccountContext);
- const [activeAccount]: any = useLocalStorage('account', {});
const [event, setEvent] = useState(null);
-
const content = event ? contentParser(event.content, event.tags) : '';
const fetchEvent = useCallback(async () => {
@@ -55,7 +54,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) {
return () => {
unsubscribe();
};
- }, [activeAccount.id, id, pool]);
+ }, [id, pool]);
const checkNoteIsSaved = useCallback(async () => {
getNoteByID(id)
diff --git a/src/components/note/quote.tsx b/src/components/note/quote.tsx
index 18b7a687..c7601c83 100644
--- a/src/components/note/quote.tsx
+++ b/src/components/note/quote.tsx
@@ -1,3 +1,4 @@
+import { AccountContext } from '@components/accountProvider';
import { RelayContext } from '@components/relaysProvider';
import { UserExtend } from '@components/user/extend';
@@ -7,15 +8,13 @@ import { contentParser } from '@utils/parser';
import { createNote, getNoteByID } from '@utils/storage';
import { getParentID } from '@utils/transform';
-import useLocalStorage from '@rehooks/local-storage';
import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) {
const pool: any = useContext(RelayContext);
+ const activeAccount: any = useContext(AccountContext);
- const [activeAccount]: any = useLocalStorage('account', {});
const [event, setEvent] = useState(null);
-
const content = event ? contentParser(event.content, event.tags) : '';
const fetchEvent = useCallback(async () => {
@@ -53,7 +52,7 @@ export const NoteQuote = memo(function NoteQuote({ id }: { id: string }) {
return () => {
unsubscribe();
};
- }, [activeAccount.id, id, pool]);
+ }, [id, pool]);
const checkNoteIsSaved = useCallback(async () => {
getNoteByID(id)
diff --git a/src/pages/channel/index.page.client.tsx b/src/pages/channel/index.page.client.tsx
index 49481522..59d47ebf 100644
--- a/src/pages/channel/index.page.client.tsx
+++ b/src/pages/channel/index.page.client.tsx
@@ -1,3 +1,4 @@
+import { AccountContext } from '@components/accountProvider';
import { FormChannel } from '@components/form/channel';
import NewsfeedLayout from '@components/layouts/newsfeed';
import { RelayContext } from '@components/relaysProvider';
@@ -8,7 +9,6 @@ import { MESSAGE_RELAYS } from '@stores/constants';
import { dateToUnix, hoursAgo } from '@utils/getDate';
import { usePageContext } from '@utils/hooks/usePageContext';
-import useLocalStorage from '@rehooks/local-storage';
import { useSetAtom } from 'jotai';
import { useResetAtom } from 'jotai/utils';
import { Suspense, lazy, useContext, useRef } from 'react';
@@ -23,7 +23,7 @@ export function Page() {
const id = searchParams.id;
const pool: any = useContext(RelayContext);
- const [activeAccount]: any = useLocalStorage('account', {});
+ const activeAccount: any = useContext(AccountContext);
const setChannelMessages = useSetAtom(channelMessagesAtom);
const resetChannelMessages = useResetAtom(channelMessagesAtom);
diff --git a/src/pages/chat/index.page.client.tsx b/src/pages/chat/index.page.client.tsx
index 4f3a4b64..63b87d53 100644
--- a/src/pages/chat/index.page.client.tsx
+++ b/src/pages/chat/index.page.client.tsx
@@ -1,3 +1,4 @@
+import { AccountContext } from '@components/accountProvider';
import FormChat from '@components/form/chat';
import NewsfeedLayout from '@components/layouts/newsfeed';
import { RelayContext } from '@components/relaysProvider';
@@ -7,7 +8,6 @@ import { MESSAGE_RELAYS } from '@stores/constants';
import { usePageContext } from '@utils/hooks/usePageContext';
-import useLocalStorage from '@rehooks/local-storage';
import { useSetAtom } from 'jotai';
import { useResetAtom } from 'jotai/utils';
import { Suspense, lazy, useContext } from 'react';
@@ -22,7 +22,7 @@ export function Page() {
const pubkey = searchParams.pubkey;
const pool: any = useContext(RelayContext);
- const [activeAccount]: any = useLocalStorage('account', {});
+ const activeAccount: any = useContext(AccountContext);
const setChatMessages = useSetAtom(chatMessagesAtom);
const resetChatMessages = useResetAtom(chatMessagesAtom);
diff --git a/src/pages/index.page.tsx b/src/pages/index.page.tsx
index 4c8f447b..9d0c4e2a 100644
--- a/src/pages/index.page.tsx
+++ b/src/pages/index.page.tsx
@@ -10,14 +10,12 @@ import {
createNote,
getActiveAccount,
getLastLogin,
- getPlebs,
updateLastLogin,
} from '@utils/storage';
import { getParentID } from '@utils/transform';
import LumeSymbol from '@assets/icons/Lume';
-import { writeStorage } from '@rehooks/local-storage';
import { useCallback, useContext, useEffect, useRef } from 'react';
import { navigate } from 'vite-plugin-ssr/client/router';
@@ -149,23 +147,12 @@ export function Page() {
.then((res: any) => {
if (res) {
const account = res;
- // update local storage
- writeStorage('account', account);
- // fetch data
fetchData(account, account.follows);
} else {
navigate('/onboarding', { overwriteLastHistoryEntry: true });
}
})
.catch(console.error);
-
- getPlebs()
- .then((res) => {
- if (res && !ignore) {
- writeStorage('plebs', res);
- }
- })
- .catch(console.error);
}
return () => {
diff --git a/src/renderer/shell.tsx b/src/renderer/shell.tsx
index d16efe11..b8e200ca 100644
--- a/src/renderer/shell.tsx
+++ b/src/renderer/shell.tsx
@@ -1,3 +1,4 @@
+import AccountProvider from '@components/accountProvider';
import RelayProvider from '@components/relaysProvider';
import { PageContextProvider } from '@utils/hooks/usePageContext';
@@ -10,7 +11,9 @@ export function Shell({ children, pageContext }: { children: React.ReactNode; pa
return (
- {children}
+
+ {children}
+
);
diff --git a/src/utils/hooks/useProfileMetadata.tsx b/src/utils/hooks/useProfileMetadata.tsx
index c64d108b..6adedb93 100644
--- a/src/utils/hooks/useProfileMetadata.tsx
+++ b/src/utils/hooks/useProfileMetadata.tsx
@@ -1,8 +1,7 @@
-import { createPleb } from '@utils/storage';
+import { createPleb, getPleb } from '@utils/storage';
-import useLocalStorage from '@rehooks/local-storage';
import { fetch } from '@tauri-apps/api/http';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import { useCallback, useEffect, useState } from 'react';
export const fetchProfileMetadata = async (pubkey: string) => {
const result = await fetch(`https://rbr.bio/${pubkey}/metadata.json`, {
@@ -13,25 +12,11 @@ export const fetchProfileMetadata = async (pubkey: string) => {
};
export const useProfileMetadata = (pubkey: string) => {
- const [activeAccount]: any = useLocalStorage('account', {});
- const [plebs] = useLocalStorage('plebs', []);
const [profile, setProfile] = useState(null);
- const cacheProfile = useMemo(() => {
- let metadata = false;
-
- if (pubkey === activeAccount.pubkey) {
- metadata = JSON.parse(activeAccount.metadata);
- } else {
- const findInStorage = plebs.find((item) => item.pubkey === pubkey);
-
- if (findInStorage !== undefined) {
- metadata = JSON.parse(findInStorage.metadata);
- }
- }
-
- return metadata;
- }, [plebs, pubkey, activeAccount.pubkey, activeAccount.metadata]);
+ const getProfileFromDB = useCallback(async (pubkey: string) => {
+ return await getPleb(pubkey);
+ }, []);
const insertPlebToDB = useCallback(async (pubkey: string, metadata: string) => {
return createPleb(pubkey, metadata);
@@ -40,13 +25,22 @@ export const useProfileMetadata = (pubkey: string) => {
useEffect(() => {
let ignore = false;
- if (!cacheProfile && !ignore) {
- fetchProfileMetadata(pubkey)
+ if (!ignore) {
+ getProfileFromDB(pubkey)
.then((res: any) => {
- // update state
- setProfile(JSON.parse(res.content));
- // save to db
- insertPlebToDB(pubkey, res.content);
+ if (res) {
+ // update state
+ setProfile(JSON.parse(res.metadata));
+ } else {
+ fetchProfileMetadata(pubkey).then((res: any) => {
+ if (res) {
+ // update state
+ setProfile(res);
+ // insert to db
+ insertPlebToDB(pubkey, JSON.stringify(res));
+ }
+ });
+ }
})
.catch(console.error);
}
@@ -54,11 +48,7 @@ export const useProfileMetadata = (pubkey: string) => {
return () => {
ignore = true;
};
- }, [cacheProfile, insertPlebToDB, pubkey]);
+ }, [insertPlebToDB, pubkey]);
- if (cacheProfile) {
- return cacheProfile;
- } else {
- return profile;
- }
+ return profile;
};
diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx
index 8cf6a05f..cbeaa0a7 100644
--- a/src/utils/storage.tsx
+++ b/src/utils/storage.tsx
@@ -48,6 +48,13 @@ export async function getPlebs() {
return await db.select(`SELECT * FROM plebs ORDER BY created_at DESC;`);
}
+// get pleb by pubkey
+export async function getPleb(pubkey: string) {
+ const db = await connect();
+ const result = await db.select(`SELECT * FROM plebs WHERE pubkey = "${pubkey}"`);
+ return result[0];
+}
+
// create pleb
export async function createPleb(pubkey: string, metadata: string) {
const db = await connect();