diff --git a/src/app/listener.middleware.js b/src/app/listener.middleware.js new file mode 100644 index 00000000..688e7535 --- /dev/null +++ b/src/app/listener.middleware.js @@ -0,0 +1,39 @@ +import { createListenerMiddleware } from "@reduxjs/toolkit"; +const getCacheKey = (action = "") => { + const [type] = action.split("/"); + const keys = [ + "visitMark", + "channels", + "contacts", + "userMessage", + "channelMessage", + // "pendingMessage", + ]; + const [tmp = ""] = keys.filter((k) => k == type); + return tmp; +}; + +// Create the middleware instance and methods +const listenerMiddleware = createListenerMiddleware(); + +// Add one or more listener entries that look for specific actions. +// They may contain any sync or async logic, similar to thunks. +listenerMiddleware.startListening({ + predicate: (action, currentState, previousState) => { + const { type = "" } = action; + + return !!getCacheKey(type); + // console.log("listener predicate", action, currentState, previousState); + // return true; + }, + effect: async (action, listenerApi) => { + const { type = "" } = action; + const key = getCacheKey(type); + // console.log("listener effect", key, listenerApi.getState(), window.CACHE); + if (key && window.CACHE) { + await window.CACHE.setItem(key, listenerApi.getState()[key]); + } + }, +}); + +export default listenerMiddleware; diff --git a/src/app/store.js b/src/app/store.js index 563918a2..e78e38dc 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -1,9 +1,6 @@ -import { - configureStore, - combineReducers, - createListenerMiddleware, -} from "@reduxjs/toolkit"; +import { configureStore, combineReducers } from "@reduxjs/toolkit"; import { setupListeners } from "@reduxjs/toolkit/query"; +import listenerMiddleware from "./listener.middleware"; import authDataReducer from "./slices/auth.data"; import visitMarkReducer from "./slices/visit.mark"; import uiReducer from "./slices/ui"; @@ -17,19 +14,7 @@ import { contactApi } from "./services/contact"; import { channelApi } from "./services/channel"; import { messageApi } from "./services/message"; import { serverApi } from "./services/server"; -const getCacheKey = (action = "") => { - const [type] = action.split("/"); - const keys = [ - "visitMark", - "channels", - "contacts", - "userMessage", - "channelMessage", - // "pendingMessage", - ]; - const [tmp = ""] = keys.filter((k) => k == type); - return tmp; -}; + const reducer = combineReducers({ ui: uiReducer, visitMark: visitMarkReducer, @@ -45,28 +30,7 @@ const reducer = combineReducers({ [channelApi.reducerPath]: channelApi.reducer, [serverApi.reducerPath]: serverApi.reducer, }); -// Create the middleware instance and methods -const listenerMiddleware = createListenerMiddleware(); -// Add one or more listener entries that look for specific actions. -// They may contain any sync or async logic, similar to thunks. -listenerMiddleware.startListening({ - predicate: (action, currentState, previousState) => { - const { type = "" } = action; - - return !!getCacheKey(type); - // console.log("listener predicate", action, currentState, previousState); - // return true; - }, - effect: async (action, listenerApi) => { - const { type = "" } = action; - const key = getCacheKey(type); - // console.log("listener effect", key, listenerApi.getState(), window.CACHE); - if (key && window.CACHE) { - await window.CACHE.setItem(key, listenerApi.getState()[key]); - } - }, -}); const store = configureStore({ reducer, middleware: (getDefaultMiddleware) => diff --git a/src/common/component/Message/index.js b/src/common/component/Message/index.js index 72b01486..2afeba94 100644 --- a/src/common/component/Message/index.js +++ b/src/common/component/Message/index.js @@ -24,7 +24,6 @@ function Message({ content_type = "text/plain", unread = false, pending, - removed = false, edited = false, likes = {}, }) { @@ -49,29 +48,27 @@ function Message({ const toggleEmojiPopover = () => { setEmojiPopVisible((prev) => !prev); }; - useEffect(() => { - if (!unread) { - avatarRef.current?.scrollIntoView(); - } - }, [unread]); + // useEffect(() => { + // if (!unread) { + // avatarRef.current?.scrollIntoView(); + // } + // }, [unread]); useEffect(() => { - if (inView) { - if (unread) { - const setMsgRead = gid ? setChannelMsgRead : setUserMsgRead; - disptach(setMsgRead({ id: gid || uid, mid })); - } + if (inView && unread) { + const setMsgRead = gid ? setChannelMsgRead : setUserMsgRead; + disptach(setMsgRead({ id: gid || uid, mid })); } }, [gid, mid, uid, unread, inView]); if (!contacts) return null; const currUser = contacts.find((c) => c.uid == fromUid) || {}; - return removed ? ( - "removed" - ) : ( + return ( { - if (!onboarding.current) { - onboarding.current = new MetaMaskOnboarding(); - } - }, []); - const getSignature = async (address, nonce) => { - console.log('get sn'); - const signature = await ethereum.request({ - method: 'personal_sign', - params: [nonce, address, 'hello from rustchat'] - }); - return signature; - }; - const handleMetamaskLogin = async () => { - if (MetaMaskOnboarding.isMetaMaskInstalled()) { - setRequesting(true); - const [address] = await ethereum.request({ - method: 'eth_requestAccounts' - }); - const { data: nonce, isSuccess } = await getNonce(address); - if (isSuccess) { - const signature = await getSignature(address, nonce); - login({ - public_address: address, - nonce, - signature, - type: 'metamask' + useEffect(() => { + if (!onboarding.current) { + onboarding.current = new MetaMaskOnboarding(); + } + }, []); + const getSignature = async (address, nonce) => { + console.log("get sn"); + const signature = await ethereum.request({ + method: "personal_sign", + params: [nonce, address, "hello from rustchat"], }); - setRequesting(false); - } - } else { - onboarding.current.startOnboarding(); - } - }; - return ( - - ); + return signature; + }; + const handleMetamaskLogin = async () => { + if (MetaMaskOnboarding.isMetaMaskInstalled()) { + setRequesting(true); + const [address] = await ethereum.request({ + method: "eth_requestAccounts", + }); + const { data: nonce, isSuccess } = await getNonce(address); + if (isSuccess) { + const signature = await getSignature(address, nonce); + login({ + public_address: address, + nonce, + signature, + type: "metamask", + }); + setRequesting(false); + } + } else { + onboarding.current.startOnboarding(); + } + }; + return ( + + ); }