Skip to content

Commit

Permalink
fix(re-render): caused by complex array in mobx hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mydearxym committed Mar 6, 2024
1 parent 28ca7ad commit 90d1990
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 91 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"mobx": "^6.12.0",
"mobx-react": "^9.1.0",
"mobx-react-lite": "^4.0.5",
"mobx-state-tree": "^5.4.0",
"mobx-state-tree": "^5.4.1",
"nanoid": "^3.1.12",
"next": "^14.1.1",
"next-compose-plugins": "^2.2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/app/Landing/ArticlesIntroTabs/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react'
import { FC, memo } from 'react'

import type { TThread } from '@/spec'
import { THREAD } from '@/constant/thread'
Expand Down Expand Up @@ -31,4 +31,4 @@ const Content: FC<TProps> = ({ tab }) => {
)
}

export default Content
export default memo(Content)
13 changes: 4 additions & 9 deletions src/app/Landing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,14 @@ const LandingPage: FC = () => {
<TechStacks />
</Suspense>

<DesktopOnly>
<Divider top={80} bottom={80} />
</DesktopOnly>
<MobileOnly>
<Divider top={50} bottom={50} />
</MobileOnly>
<Divider top={80} bottom={80} />
<UsersWall wallpaper={wallpaper} />
<DesktopOnly>
<Divider top={60} bottom={80} />
</DesktopOnly>
<Divider top={60} bottom={80} />

<FAQWrapper>
<FaqList layout={DOC_FAQ_LAYOUT.FLAT} large sections={faqs} />
</FAQWrapper>

<Divider top={60} bottom={80} />

<Footer />
Expand Down
12 changes: 5 additions & 7 deletions src/app/[community]/(articles)/about/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { FC } from 'react'
'use client'

const Loading: FC = () => {
return (
<div>
<h2>about loading</h2>
</div>
)
import LavaLampLoading from '@/widgets/Loading/LavaLampLoading'

const Loading = () => {
return <LavaLampLoading top={80} />
}

export default Loading
10 changes: 5 additions & 5 deletions src/app/[community]/(articles)/changelog/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import LavaLampLoading from '@/widgets/Loading/LavaLampLoading'

const Loading = () => {
return (
<div>
<h2>changelogs loading</h2>
</div>
)
return <LavaLampLoading top={80} />
}

export default Loading
10 changes: 5 additions & 5 deletions src/app/[community]/(articles)/doc/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import LavaLampLoading from '@/widgets/Loading/LavaLampLoading'

const Loading = () => {
return (
<div>
<h2>doc loading</h2>
</div>
)
return <LavaLampLoading top={80} />
}

export default Loading
10 changes: 5 additions & 5 deletions src/app/[community]/(articles)/kanban/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import LavaLampLoading from '@/widgets/Loading/LavaLampLoading'

const Loading = () => {
return (
<div>
<h2>kanban loading</h2>
</div>
)
return <LavaLampLoading top={80} />
}

export default Loading
10 changes: 5 additions & 5 deletions src/app/[community]/(articles)/post/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import LavaLampLoading from '@/widgets/Loading/LavaLampLoading'

const Loading = () => {
return (
<div>
<h2>post loading</h2>
</div>
)
return <LavaLampLoading top={80} />
}

export default Loading
8 changes: 4 additions & 4 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export const metadata: Metadata = {

export default function Layout({ children }: { children: React.ReactNode }) {
// const token = cookies().get('jwtToken')?.value || null
const token = null
// const token = null
// const theme = cookies().get('theme')?.value === 'night' ? 'night' : 'day'
const theme = 'day'
// const theme = 'day'

return (
<html lang="en">
<body>
<StyledComponentsRegistry>
<GraphQLProvider token={token}>
<RootStoreProvider token={token} theme={theme}>
<GraphQLProvider>
<RootStoreProvider>
<GlobalLayout>{children}</GlobalLayout>
</RootStoreProvider>
</GraphQLProvider>
Expand Down
6 changes: 5 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client'

import { memo } from 'react'

import Landing from './Landing'

export default function Page() {
const Page = () => {
return <Landing />
}

export default memo(Page)
7 changes: 3 additions & 4 deletions src/app/providers/GraphQLProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import { UrqlProvider, ssrExchange, cacheExchange, fetchExchange, createClient }
import { GRAPHQL_ENDPOINT } from '@/config'

type TProps = {
token: null | string
children: ReactNode
}

const GraphQLProvider: FC<TProps> = ({ children, token }) => {
const GraphQLProvider: FC<TProps> = ({ children }) => {
const [client, ssr] = useMemo(() => {
const ssr = ssrExchange()
const client = createClient({
Expand All @@ -25,13 +24,13 @@ const GraphQLProvider: FC<TProps> = ({ children, token }) => {
fetchOptions: () => ({
headers: {
special: 'Special header value',
authorization: `Bearer ${token || ''}`,
authorization: '',
},
}),
})

return [client, ssr]
}, [token])
}, [])

return (
<UrqlProvider client={client} ssr={ssr}>
Expand Down
17 changes: 3 additions & 14 deletions src/app/providers/RootStoreProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { FC, ReactNode, memo } from 'react'
import { Provider } from 'mobx-react'
import { enableStaticRendering } from 'mobx-react-lite'

import type { TThemeName } from '@/spec'
import { useStore } from '@/stores/init'

import {
useMetric,
useSession,
useCommunity,
useTags,
usePost,
Expand All @@ -27,19 +25,15 @@ import {
enableStaticRendering(typeof window === 'undefined')

type TProps = {
token: null | string
theme: TThemeName
children: ReactNode
}

const RootStoreWrapper: FC<TProps> = ({ children, token, theme }) => {
const userHasLogin = !!token
const RootStoreWrapper: FC<TProps> = ({ children }) => {
const userHasLogin = false

const metric = useMetric()
const activeThread = useThreadParam()
// console.log('## activeThread: ', activeThread)

const { sesstion } = useSession()
const { community } = useCommunity(userHasLogin)

const { pagedPosts } = usePagedPosts(userHasLogin)
Expand All @@ -55,7 +49,6 @@ const RootStoreWrapper: FC<TProps> = ({ children, token, theme }) => {

const store = useStore({
metric,
...sesstion,
articles: {
pagedPosts,
pagedChangelogs,
Expand All @@ -72,12 +65,8 @@ const RootStoreWrapper: FC<TProps> = ({ children, token, theme }) => {
changelog,
activeThread,
},
wallpaperEditor: wallpaper,
dashboardThread: dashboard,

theme: {
curTheme: theme,
},
wallpaperEditor: wallpaper,
})

console.log('## root store provider')
Expand Down
44 changes: 44 additions & 0 deletions src/app/queries/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,47 @@ export const parseDashboard = (community: TCommunity, pathname: string): TParseD
...parseDashboardThread(pathname),
}
}

export const parseDashboardbackup = (community: TCommunity, pathname: string): TParseDashboard => {
// NOTE: if the backend is not ready, return default config
// @ts-ignore
if (!community) return {}

const { dashboard, moderators } = community
const {
enable,
nameAlias,
socialLinks,
faqs,
seo,
layout,
rss,
baseInfo,
headerLinks,
footerLinks,
mediaReports,
} = dashboard

const fieldsObj = removeEmptyValuesFromObject({
enable,
nameAlias: parseDashboardAlias(nameAlias),
socialLinks,
faqSections: faqs,
...baseInfo,
...seo,
...layout,
...rss,
headerLinks,
footerLinks,
moderators,
mediaReports,
})

return {
...fieldsObj,
initSettings: {
...fieldsObj,
},
...parseDashboardThread(pathname),
}
}
4 changes: 3 additions & 1 deletion src/app/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ export const useDashboard = (community: TCommunity): TParseDashboard => {
const pathname = usePathname()

// @ts-ignore
return !isStaticQuery ? parseDashboard(community, pathname) : {}
if (isStaticQuery) return {}

return parseDashboard(community, pathname)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/containers/thread/DashboardThread/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
TChangeMode,
TSocialItem,
TMediaReport,
TLinkItem,
} from '@/spec'

import {
Expand Down Expand Up @@ -132,6 +133,19 @@ const DashboardThread = T.model('DashboardThread', {

return toJS(root.viewing.community)
},

get nameAliasData(): TNameAlias[] {
return toJS(self.nameAlias)
},

get headerLinksData(): TLinkItem[] {
return toJS(self.headerLinks)
},

get footerLinksData(): TLinkItem[] {
return toJS(self.footerLinks)
},

get overviewData(): TOverview {
return toJS(self.overview)
},
Expand Down
8 changes: 3 additions & 5 deletions src/hooks/useFooterLinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react'
import { useContext, useMemo } from 'react'
import { MobXProviderContext } from 'mobx-react'

import type { TLinkItem, TFooterLayout } from '@/spec'
Expand All @@ -14,13 +14,11 @@ type TFooterLinks = {
const useFooterLinks = (): TFooterLinks => {
const { store } = useContext(MobXProviderContext)

if (store === null) {
throw new Error('Store cannot be null, please add a context provider')
}
const footerlinks = useMemo(() => store.dashboardThread.footerLinksData, [])

return {
layout: store.dashboardThread.footerLayout,
links: store.dashboardThread.footerLinks,
links: footerlinks,
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useHeaderLinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react'
import { useContext, useMemo } from 'react'
import { find } from 'ramda'
import { MobXProviderContext } from 'mobx-react'

Expand Down Expand Up @@ -48,10 +48,11 @@ const useHeaderLinks = (): THeaderLinks => {
}

const customLinks = isModerator ? [...headerLinksRow, aboutLink, dashboardLink] : headerLinksRow
const headerlinks = useMemo(() => store.dashboardThread.headerLinksData, [])

return {
layout: store.dashboardThread.headerLayout,
links: store.dashboardThread.headerLinks,
links: headerlinks,
customLinks,
}
}
Expand Down
Loading

0 comments on commit 90d1990

Please sign in to comment.