From 648b0fc4e8e23c38e65758b0c3505af4fd0a2182 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Wed, 3 May 2023 16:29:39 +0700 Subject: [PATCH] remove tricky codes --- src/app/channel/components/blacklist.tsx | 2 +- src/app/channel/components/mutedItem.tsx | 2 +- src/app/channel/pages/index.page.tsx | 32 +++++++++++++----------- src/auth/layout.tsx | 12 +++++---- src/shared/appHeader.tsx | 12 +++++---- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/app/channel/components/blacklist.tsx b/src/app/channel/components/blacklist.tsx index ad6689bb..8fbc3dcd 100644 --- a/src/app/channel/components/blacklist.tsx +++ b/src/app/channel/components/blacklist.tsx @@ -37,7 +37,7 @@ export default function ChannelBlackList({ blacklist }: { blacklist: any }) {

-
+
{blacklist.map((item: any) => ( ))} diff --git a/src/app/channel/components/mutedItem.tsx b/src/app/channel/components/mutedItem.tsx index 8bffcd62..52c092f9 100644 --- a/src/app/channel/components/mutedItem.tsx +++ b/src/app/channel/components/mutedItem.tsx @@ -49,7 +49,7 @@ export default function MutedItem({ data }: { data: any }) {
- {user?.display_name || user?.name} + {user?.display_name || user?.name || 'Pleb'} {shortenKey(data.content)}
diff --git a/src/app/channel/pages/index.page.tsx b/src/app/channel/pages/index.page.tsx index f050c30a..2de477b0 100644 --- a/src/app/channel/pages/index.page.tsx +++ b/src/app/channel/pages/index.page.tsx @@ -9,24 +9,26 @@ import { READONLY_RELAYS } from '@lume/stores/constants'; import { dateToUnix, hoursAgo } from '@lume/utils/getDate'; import { useActiveAccount } from '@lume/utils/hooks/useActiveAccount'; import { usePageContext } from '@lume/utils/hooks/usePageContext'; +import { getActiveBlacklist, getBlacklist } from '@lume/utils/storage'; import { arrayObjToPureArr } from '@lume/utils/transform'; import { useSetAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; import { Suspense, lazy, useContext, useEffect, useRef } from 'react'; +import useSWR from 'swr'; import useSWRSubscription from 'swr/subscription'; -let mutedList: any = []; -let activeMutedList: any = []; -let activeHidedList: any = []; +const fetchMuted = async ([, id]) => { + const res = await getBlacklist(id, 44); + const array = arrayObjToPureArr(res); + return { original: res, array: array }; +}; -if (typeof window !== 'undefined') { - const { getBlacklist, getActiveBlacklist, getActiveAccount } = await import('@lume/utils/storage'); - const activeAccount = await getActiveAccount(); - activeHidedList = await getActiveBlacklist(activeAccount.id, 43); - activeMutedList = await getActiveBlacklist(activeAccount.id, 44); - mutedList = await getBlacklist(activeAccount.id, 44); -} +const fetchHided = async ([, id]) => { + const res = await getActiveBlacklist(id, 43); + const array = arrayObjToPureArr(res); + return array; +}; const ChannelMessageList = lazy(() => import('@lume/app/channel/components/messageList')); @@ -39,16 +41,16 @@ export function Page() { const channelPubkey = searchParams.pubkey; const { account, isLoading, isError } = useActiveAccount(); + const { data: muted } = useSWR(!isLoading && !isError && account ? ['muted', account.id] : null, fetchMuted); + const { data: hided } = useSWR(!isLoading && !isError && account ? ['hided', account.id] : null, fetchHided); const setChannelMessages = useSetAtom(channelMessagesAtom); const resetChannelMessages = useResetAtom(channelMessagesAtom); const resetChannelReply = useResetAtom(channelReplyAtom); const now = useRef(new Date()); - const hided = arrayObjToPureArr(activeHidedList); - const muted = arrayObjToPureArr(activeMutedList); - useSWRSubscription(channelID ? ['channel', channelID] : null, ([, key], {}: any) => { + useSWRSubscription(channelID && muted && hided ? ['channel', channelID] : null, ([, key], {}: any) => { // subscribe to channel const unsubscribe = pool.subscribe( [ @@ -67,7 +69,7 @@ export function Page() { } else { message['hide'] = false; } - if (!muted.includes(event.pubkey)) { + if (!muted.array.includes(event.pubkey)) { setChannelMessages((prev) => [...prev, message]); } } @@ -101,7 +103,7 @@ export function Page() {
- + {!muted ? <> : } {!isLoading && !isError && account ? ( account.pubkey === channelPubkey && ) : ( diff --git a/src/auth/layout.tsx b/src/auth/layout.tsx index 20e0d93c..16cce9a5 100644 --- a/src/auth/layout.tsx +++ b/src/auth/layout.tsx @@ -1,14 +1,16 @@ import ArrowLeftIcon from '@lume/shared/icons/arrowLeft'; import ArrowRightIcon from '@lume/shared/icons/arrowRight'; -let platformName = 'darwin'; +import useSWR from 'swr'; -if (typeof window !== 'undefined') { +const fetcher = async () => { const { platform } = await import('@tauri-apps/api/os'); - platformName = await platform(); -} + return await platform(); +}; export function LayoutOnboarding({ children }: { children: React.ReactNode }) { + const { data: platform } = useSWR('platform', fetcher); + const goBack = () => { window.history.back(); }; @@ -25,7 +27,7 @@ export function LayoutOnboarding({ children }: { children: React.ReactNode }) { className="relative h-11 shrink-0 border border-zinc-100 bg-white dark:border-zinc-900 dark:bg-black" >
-
+