From 98637feba422ebecddada77fe6b561e2556b8559 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:59:33 +0700 Subject: [PATCH] remove localstorage --- package.json | 1 - pnpm-lock.yaml | 11 ---- src-tauri/Cargo.lock | 13 +++++ src-tauri/Cargo.toml | 1 + src/components/accountProvider.tsx | 14 +++++ .../channels/createChannelModal.tsx | 5 +- .../channels/messages/hideMessageButton.tsx | 6 +-- .../channels/messages/muteButton.tsx | 4 +- src/components/chats/chatList.tsx | 16 +++--- src/components/chats/messageList.tsx | 10 ++-- src/components/eventCollector.tsx | 10 ++-- src/components/form/base.tsx | 5 +- src/components/form/channel.tsx | 4 +- src/components/form/chat.tsx | 6 +-- src/components/form/comment.tsx | 4 +- src/components/multiAccounts/index.tsx | 23 ++++---- src/components/note/meta/comment.tsx | 6 +-- src/components/note/meta/reaction.tsx | 4 +- src/components/note/metadata.tsx | 4 +- src/components/note/parent.tsx | 7 ++- src/components/note/quote.tsx | 7 ++- src/pages/channel/index.page.client.tsx | 4 +- src/pages/chat/index.page.client.tsx | 4 +- src/pages/index.page.tsx | 13 ----- src/renderer/shell.tsx | 5 +- src/utils/hooks/useProfileMetadata.tsx | 54 ++++++++----------- src/utils/storage.tsx | 7 +++ 27 files changed, 123 insertions(+), 125 deletions(-) create mode 100644 src/components/accountProvider.tsx diff --git a/package.json b/package.json index e6b11e4b..845503db 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "@radix-ui/react-popover": "^1.0.5", "@radix-ui/react-tabs": "^1.0.3", "@radix-ui/react-tooltip": "^1.0.5", - "@rehooks/local-storage": "^2.4.4", "@supabase/supabase-js": "^2.21.0", "@tauri-apps/api": "^1.2.0", "dayjs": "^1.11.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c9a1993e..dfd693bd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,7 +8,6 @@ specifiers: '@radix-ui/react-popover': ^1.0.5 '@radix-ui/react-tabs': ^1.0.3 '@radix-ui/react-tooltip': ^1.0.5 - '@rehooks/local-storage': ^2.4.4 '@supabase/supabase-js': ^2.21.0 '@tailwindcss/typography': ^0.5.9 '@tauri-apps/api': ^1.2.0 @@ -66,7 +65,6 @@ dependencies: '@radix-ui/react-popover': 1.0.5_g6eqzmexmujy3zvaqhkicj5z64 '@radix-ui/react-tabs': 1.0.3_biqbaboplfbrettd7655fr4n2y '@radix-ui/react-tooltip': 1.0.5_g6eqzmexmujy3zvaqhkicj5z64 - '@rehooks/local-storage': 2.4.4_react@18.2.0 '@supabase/supabase-js': 2.21.0_encoding@0.1.13 '@tauri-apps/api': 1.2.0 dayjs: 1.11.7 @@ -1164,15 +1162,6 @@ packages: '@babel/runtime': 7.21.0 dev: false - /@rehooks/local-storage/2.4.4_react@18.2.0: - resolution: - { integrity: sha512-zE+kfOkG59n/1UTxdmbwktIosclr67Nlbf2MzUJ9mNtCSypVscNHeD1qT6JCSo5Pjj8DO893IKWNLJqKKzDL/Q== } - peerDependencies: - react: '>=16.8.0' - dependencies: - react: 18.2.0 - dev: false - /@rollup/plugin-virtual/3.0.1: resolution: { integrity: sha512-fK8O0IL5+q+GrsMLuACVNk2x21g3yaw+sG2qn16SnUd3IlBsQyvWxLMGHmCmXRMecPjGRSZ/1LmZB4rjQm68og== } diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index a828e525..1997f910 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2106,6 +2106,7 @@ dependencies = [ "tauri-build", "tauri-plugin-single-instance", "tauri-plugin-sql", + "tauri-plugin-store", ] [[package]] @@ -4052,6 +4053,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tauri-plugin-store" +version = "0.1.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#7acf865ffbb82c6d00334926a4527dd5ff3163a4" +dependencies = [ + "log", + "serde", + "serde_json", + "tauri", + "thiserror", +] + [[package]] name = "tauri-runtime" version = "0.12.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 73b23254..dae1156d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,6 +18,7 @@ serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.2", features = ["clipboard-read-text", "clipboard-write-text", "dialog-open", "fs-read-dir", "fs-read-file", "http-all", "http-multipart", "os-all", "shell-open", "system-tray", "updater", "window-close", "window-start-dragging"] } tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } +tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } sqlx-cli = {version = "0.6.3", default-features = false, features = ["sqlite"] } [dependencies.tauri-plugin-sql] diff --git a/src/components/accountProvider.tsx b/src/components/accountProvider.tsx new file mode 100644 index 00000000..2320a1bd --- /dev/null +++ b/src/components/accountProvider.tsx @@ -0,0 +1,14 @@ +import { createContext } from 'react'; + +export const AccountContext = createContext({}); + +let activeAccount: any = null; + +if (typeof window !== 'undefined') { + const { getActiveAccount } = await import('@utils/storage'); + activeAccount = await getActiveAccount(); +} + +export default function AccountProvider({ children }: { children: React.ReactNode }) { + return {children}; +} diff --git a/src/components/channels/createChannelModal.tsx b/src/components/channels/createChannelModal.tsx index 052ad704..e0911b65 100644 --- a/src/components/channels/createChannelModal.tsx +++ b/src/components/channels/createChannelModal.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { AvatarUploader } from '@components/avatarUploader'; import { RelayContext } from '@components/relaysProvider'; @@ -8,7 +9,6 @@ import { dateToUnix } from '@utils/getDate'; import { createChannel } from '@utils/storage'; import * as Dialog from '@radix-ui/react-dialog'; -import useLocalStorage from '@rehooks/local-storage'; import { Cancel, Plus } from 'iconoir-react'; import { useSetAtom } from 'jotai'; import { getEventHash, signEvent } from 'nostr-tools'; @@ -18,8 +18,9 @@ import { navigate } from 'vite-plugin-ssr/client/router'; export const CreateChannelModal = () => { const pool: any = useContext(RelayContext); + const activeAccount: any = useContext(AccountContext); + const [open, setOpen] = useState(false); - const [activeAccount]: any = useLocalStorage('account', {}); const [image, setImage] = useState(DEFAULT_AVATAR); const [loading, setLoading] = useState(false); diff --git a/src/components/channels/messages/hideMessageButton.tsx b/src/components/channels/messages/hideMessageButton.tsx index 365eb779..3af35fc1 100644 --- a/src/components/channels/messages/hideMessageButton.tsx +++ b/src/components/channels/messages/hideMessageButton.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { RelayContext } from '@components/relaysProvider'; import { MESSAGE_RELAYS } from '@stores/constants'; @@ -6,14 +7,13 @@ import { dateToUnix } from '@utils/getDate'; import * as AlertDialog from '@radix-ui/react-alert-dialog'; import * as Tooltip from '@radix-ui/react-tooltip'; -import useLocalStorage from '@rehooks/local-storage'; import { EyeClose } from 'iconoir-react'; import { getEventHash, signEvent } from 'nostr-tools'; import { useCallback, useContext } from 'react'; export const HideMessageButton = ({ id }: { id: string }) => { const pool: any = useContext(RelayContext); - const [activeAccount]: any = useLocalStorage('account', {}); + const activeAccount: any = useContext(AccountContext); const hideMessage = useCallback(() => { const event: any = { @@ -28,7 +28,7 @@ export const HideMessageButton = ({ id }: { id: string }) => { // publish note pool.publish(event, MESSAGE_RELAYS); - }, [id, activeAccount.privkey, activeAccount.pubkey, pool, MESSAGE_RELAYS]); + }, [id, pool, MESSAGE_RELAYS]); return ( diff --git a/src/components/channels/messages/muteButton.tsx b/src/components/channels/messages/muteButton.tsx index 90ffef4d..467e3926 100644 --- a/src/components/channels/messages/muteButton.tsx +++ b/src/components/channels/messages/muteButton.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { RelayContext } from '@components/relaysProvider'; import { MESSAGE_RELAYS } from '@stores/constants'; @@ -6,14 +7,13 @@ import { dateToUnix } from '@utils/getDate'; import * as AlertDialog from '@radix-ui/react-alert-dialog'; import * as Tooltip from '@radix-ui/react-tooltip'; -import useLocalStorage from '@rehooks/local-storage'; import { MicMute } from 'iconoir-react'; import { getEventHash, signEvent } from 'nostr-tools'; import { useContext } from 'react'; export const MuteButton = ({ pubkey }: { pubkey: string }) => { const pool: any = useContext(RelayContext); - const [activeAccount]: any = useLocalStorage('account', {}); + const activeAccount: any = useContext(AccountContext); const muteUser = () => { const event: any = { diff --git a/src/components/chats/chatList.tsx b/src/components/chats/chatList.tsx index 532af110..3f0eac6a 100644 --- a/src/components/chats/chatList.tsx +++ b/src/components/chats/chatList.tsx @@ -1,23 +1,23 @@ +import { AccountContext } from '@components/accountProvider'; import { ChatListItem } from '@components/chats/chatListItem'; import { ChatModal } from '@components/chats/chatModal'; import { DEFAULT_AVATAR } from '@stores/constants'; -import useLocalStorage from '@rehooks/local-storage'; +import { useContext } from 'react'; let list: any = []; if (typeof window !== 'undefined') { - const { getChats } = await import('@utils/storage'); - const getAccount = window.localStorage.getItem('account'); - const account = getAccount ? JSON.parse(getAccount) : null; + const { getChats, getActiveAccount } = await import('@utils/storage'); + const activeAccount = await getActiveAccount(); - list = await getChats(account.id); + list = await getChats(activeAccount.id); } export default function ChatList() { - const [activeAccount]: any = useLocalStorage('account', {}); - const profile = activeAccount.metadata ? JSON.parse(activeAccount.metadata) : null; + const activeAccount: any = useContext(AccountContext); + const profile = activeAccount ? JSON.parse(activeAccount.metadata) : null; return (
@@ -38,7 +38,7 @@ export default function ChatList() {
- {list.map((item) => ( + {list.map((item: { id: string; pubkey: string }) => ( ))} diff --git a/src/components/chats/messageList.tsx b/src/components/chats/messageList.tsx index b2dd8a57..e9813de0 100644 --- a/src/components/chats/messageList.tsx +++ b/src/components/chats/messageList.tsx @@ -1,26 +1,26 @@ +import { AccountContext } from '@components/accountProvider'; import MessageListItem from '@components/chats/messageListItem'; import { sortedChatMessagesAtom } from '@stores/chat'; -import useLocalStorage from '@rehooks/local-storage'; import { useAtomValue } from 'jotai'; -import { useCallback, useRef } from 'react'; +import { useCallback, useContext, useRef } from 'react'; import Skeleton from 'react-loading-skeleton'; import { Virtuoso } from 'react-virtuoso'; export default function MessageList() { + const activeAccount: any = useContext(AccountContext); + const virtuosoRef = useRef(null); const data = useAtomValue(sortedChatMessagesAtom); - const [activeAccount]: any = useLocalStorage('account', {}); - const itemContent: any = useCallback( (index: string | number) => { return ( ); }, - [activeAccount.privkey, activeAccount.pubkey, data] + [data] ); const computeItemKey = useCallback( diff --git a/src/components/eventCollector.tsx b/src/components/eventCollector.tsx index 41d85a7c..aca585c7 100644 --- a/src/components/eventCollector.tsx +++ b/src/components/eventCollector.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { NetworkStatusIndicator } from '@components/networkStatusIndicator'; import { RelayContext } from '@components/relaysProvider'; @@ -8,16 +9,15 @@ import { dateToUnix } from '@utils/getDate'; import { createChannel, createChat, createNote, updateAccount } from '@utils/storage'; import { getParentID, nip02ToArray } from '@utils/transform'; -import useLocalStorage from '@rehooks/local-storage'; import { useSetAtom } from 'jotai'; import { useCallback, useContext, useEffect, useRef } from 'react'; export default function EventCollector() { const pool: any = useContext(RelayContext); - const [activeAccount]: any = useLocalStorage('account', null); + const activeAccount: any = useContext(AccountContext); const setHasNewerNote = useSetAtom(hasNewerNoteAtom); - const follows = JSON.parse(activeAccount.follows); + const follows = activeAccount ? JSON.parse(activeAccount.follows) : []; const now = useRef(new Date()); @@ -104,7 +104,7 @@ export default function EventCollector() { return () => { unsubscribe(); }; - }, [activeAccount.pubkey, activeAccount.id, follows, pool, setHasNewerNote]); + }, [follows, pool, setHasNewerNote]); useEffect(() => { let ignore = false; @@ -116,7 +116,7 @@ export default function EventCollector() { return () => { ignore = true; }; - }, [setHasNewerNote, subscribe]); + }, [subscribe]); return ; } diff --git a/src/components/form/base.tsx b/src/components/form/base.tsx index db311131..66f24f2e 100644 --- a/src/components/form/base.tsx +++ b/src/components/form/base.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { ImagePicker } from '@components/form/imagePicker'; import { RelayContext } from '@components/relaysProvider'; @@ -6,7 +7,6 @@ import { noteContentAtom } from '@stores/note'; import { dateToUnix } from '@utils/getDate'; -import useLocalStorage from '@rehooks/local-storage'; import { useAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; import { getEventHash, signEvent } from 'nostr-tools'; @@ -14,12 +14,11 @@ import { useContext } from 'react'; export default function FormBase() { const pool: any = useContext(RelayContext); + const activeAccount: any = useContext(AccountContext); const [value, setValue] = useAtom(noteContentAtom); const resetValue = useResetAtom(noteContentAtom); - const [activeAccount]: any = useLocalStorage('account', {}); - const submitEvent = () => { const event: any = { content: value, diff --git a/src/components/form/channel.tsx b/src/components/form/channel.tsx index cc18e675..54fc86d0 100644 --- a/src/components/form/channel.tsx +++ b/src/components/form/channel.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { ImagePicker } from '@components/form/imagePicker'; import { RelayContext } from '@components/relaysProvider'; import { UserMini } from '@components/user/mini'; @@ -7,7 +8,6 @@ import { MESSAGE_RELAYS } from '@stores/constants'; import { dateToUnix } from '@utils/getDate'; -import useLocalStorage from '@rehooks/local-storage'; import { Cancel } from 'iconoir-react'; import { useAtom, useAtomValue } from 'jotai'; import { useResetAtom } from 'jotai/utils'; @@ -16,7 +16,7 @@ import { useCallback, useContext } from 'react'; export const FormChannel = ({ eventId }: { eventId: string | string[] }) => { const pool: any = useContext(RelayContext); - const [activeAccount]: any = useLocalStorage('account', {}); + const activeAccount: any = useContext(AccountContext); const [value, setValue] = useAtom(channelContentAtom); const resetValue = useResetAtom(channelContentAtom); diff --git a/src/components/form/chat.tsx b/src/components/form/chat.tsx index 8c8ca9c3..230312da 100644 --- a/src/components/form/chat.tsx +++ b/src/components/form/chat.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { ImagePicker } from '@components/form/imagePicker'; import { RelayContext } from '@components/relaysProvider'; @@ -6,7 +7,6 @@ import { MESSAGE_RELAYS } from '@stores/constants'; import { dateToUnix } from '@utils/getDate'; -import useLocalStorage from '@rehooks/local-storage'; import { useAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; import { getEventHash, nip04, signEvent } from 'nostr-tools'; @@ -14,7 +14,7 @@ import { useCallback, useContext } from 'react'; export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) { const pool: any = useContext(RelayContext); - const [activeAccount]: any = useLocalStorage('account', {}); + const activeAccount: any = useContext(AccountContext); const [value, setValue] = useAtom(chatContentAtom); const resetValue = useResetAtom(chatContentAtom); @@ -44,7 +44,7 @@ export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) resetValue(); }) .catch(console.error); - }, [encryptMessage, activeAccount.privkey, activeAccount.pubkey, receiverPubkey, resetValue, pool]); + }, [encryptMessage, receiverPubkey, resetValue, pool]); const handleEnterPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { diff --git a/src/components/form/comment.tsx b/src/components/form/comment.tsx index 2729ed11..fcd36363 100644 --- a/src/components/form/comment.tsx +++ b/src/components/form/comment.tsx @@ -1,17 +1,17 @@ +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 { getEventHash, signEvent } from 'nostr-tools'; import { useContext, useState } from 'react'; export default function FormComment({ eventID }: { eventID: any }) { const pool: any = useContext(RelayContext); + const activeAccount: any = useContext(AccountContext); - const [activeAccount]: any = useLocalStorage('account', {}); const [value, setValue] = useState(''); const profile = JSON.parse(activeAccount.metadata); diff --git a/src/components/multiAccounts/index.tsx b/src/components/multiAccounts/index.tsx index ae531dbf..08e39fd8 100644 --- a/src/components/multiAccounts/index.tsx +++ b/src/components/multiAccounts/index.tsx @@ -1,3 +1,4 @@ +import { AccountContext } from '@components/accountProvider'; import { ActiveAccount } from '@components/multiAccounts/activeAccount'; import { InactiveAccount } from '@components/multiAccounts/inactiveAccount'; @@ -5,9 +6,8 @@ import { APP_VERSION } from '@stores/constants'; import LumeSymbol from '@assets/icons/Lume'; -import useLocalStorage from '@rehooks/local-storage'; import { Plus } from 'iconoir-react'; -import { useCallback } from 'react'; +import { useCallback, useContext } from 'react'; let accounts: any = []; @@ -17,18 +17,15 @@ if (typeof window !== 'undefined') { } export default function MultiAccounts() { - const [activeAccount]: any = useLocalStorage('account', {}); + const activeAccount: any = useContext(AccountContext); - const renderAccount = useCallback( - (account: { pubkey: string }) => { - if (account.pubkey === activeAccount.pubkey) { - return ; - } else { - return ; - } - }, - [activeAccount.pubkey] - ); + const renderAccount = useCallback((account: { pubkey: string }) => { + if (account.pubkey === activeAccount.pubkey) { + return ; + } else { + return ; + } + }, []); return (
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();