Skip to content

Commit

Permalink
refactor: remove rq data store
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jun 23, 2023
1 parent 474dd8b commit 12a0600
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import { createContext, useContext, useEffect, useMemo, useState } from 'react'

import { useAggregationQuery } from '~/hooks/data/use-aggregation'
import { cloneDeep } from '~/lib/_'
import { useAggregationSelector } from '~/providers/root/aggregation-data-provider'

import { headerMenuConfig as baseHeaderMenuConfig } from '../config'

Expand All @@ -14,17 +14,22 @@ const HeaderMenuConfigContext = createContext({

export const useHeaderConfig = () => useContext(HeaderMenuConfigContext)
export const HeaderDataConfigureProvider: Component = ({ children }) => {
const { data } = useAggregationQuery()
const pageMeta = useAggregationSelector(
(aggregationData) => aggregationData.pageMeta,
)
const categories = useAggregationSelector(
(aggregationData) => aggregationData.categories,
)
const [headerMenuConfig, setHeaderMenuConfig] = useState(baseHeaderMenuConfig)

useEffect(() => {
if (!data) return
if (!pageMeta) return
const nextMenuConfig = cloneDeep(baseHeaderMenuConfig)
if (data.pageMeta) {
if (pageMeta) {
const homeIndex = nextMenuConfig.findIndex((item) => item.type === 'Home')
if (homeIndex !== -1) {
nextMenuConfig[homeIndex].subMenu = []
for (const page of data.pageMeta) {
for (const page of pageMeta) {
nextMenuConfig[homeIndex].subMenu!.push({
path: page.slug,
title: page.title,
Expand All @@ -33,11 +38,11 @@ export const HeaderDataConfigureProvider: Component = ({ children }) => {
}
}

if (data.categories?.length) {
if (categories?.length) {
const postIndex = nextMenuConfig.findIndex((item) => item.type === 'Post')
if (postIndex !== -1) {
nextMenuConfig[postIndex].subMenu = []
for (const category of data.categories) {
for (const category of categories) {
nextMenuConfig[postIndex].subMenu!.push({
path: `/categories/${category.slug}`,
title: category.name,
Expand All @@ -47,7 +52,7 @@ export const HeaderDataConfigureProvider: Component = ({ children }) => {
}

setHeaderMenuConfig(nextMenuConfig)
}, [data])
}, [categories, pageMeta])

return (
<HeaderMenuConfigContext.Provider
Expand Down
4 changes: 1 addition & 3 deletions src/components/widgets/note/NoteActionAside.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
useCurrentNoteDataSelector,
} from '~/providers/note/CurrentNoteDataProvider'
import { useCurrentNoteId } from '~/providers/note/CurrentNoteIdProvider'
import { useAggregationData } from '~/providers/root/aggregation-data-provider'
import { isLikedBefore, setLikeId } from '~/utils/cookie'
import { clsxm } from '~/utils/helper'
import { apiClient } from '~/utils/request'
Expand Down Expand Up @@ -110,13 +109,12 @@ const ShareButton = () => {
const hasShare = 'share' in navigator
const isClient = useIsClient()

const aggregation = useAggregationData()
if (!isClient) return null

if (!hasShare) {
return null
}
if (!aggregation) return null

return (
<MotionButtonBase
aria-label="Share This Note Button"
Expand Down
5 changes: 2 additions & 3 deletions src/components/widgets/post/PostActionAside.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
setCurrentPostData,
useCurrentPostDataSelector,
} from '~/providers/post/CurrentPostDataProvider'
import { useAggregationData } from '~/providers/root/aggregation-data-provider'
import { isLikedBefore, setLikeId } from '~/utils/cookie'
import { clsxm } from '~/utils/helper'
import { apiClient } from '~/utils/request'
Expand Down Expand Up @@ -110,14 +109,14 @@ const ShareButton = () => {
const isClient = useIsClient()
void useCurrentNoteId()
const note = getCurrentNoteData()?.data
const aggregation = useAggregationData()

if (!isClient) return null
if (!note) return null

if (!hasShare) {
return null
}
if (!aggregation) return null

return (
<MotionButtonBase
aria-label="Share this post"
Expand Down
9 changes: 3 additions & 6 deletions src/components/widgets/subscribe/SubscribeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StyledButton } from '~/components/ui/button'
import { Input } from '~/components/ui/input/Input'
import { useStateToRef } from '~/hooks/common/use-state-ref'
import { toast } from '~/lib/toast'
import { useAggregationData } from '~/providers/root/aggregation-data-provider'
import { useAggregationSelector } from '~/providers/root/aggregation-data-provider'
import { apiClient } from '~/utils/request'

import { useSubscribeStatusQuery } from './hooks'
Expand Down Expand Up @@ -94,11 +94,8 @@ export const SubscribeModal: FC<SubscribeModalProps> = ({
dispatch({ type: 'reset' })
onConfirm()
}
const aggregation = useAggregationData()
if (!aggregation) return null
const {
seo: { title },
} = aggregation
const title = useAggregationSelector((data) => data.seo.title)

return (
<form action="#" onSubmit={handleSubList} className="flex flex-col gap-5">
<p className="text-gray-1 text-sm">
Expand Down
14 changes: 0 additions & 14 deletions src/hooks/data/use-aggregation.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/providers/root/aggregation-data-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { FC, PropsWithChildren } from 'react'

import { fetchAppUrl } from '~/atoms'
import { login } from '~/atoms/owner'
import { useBeforeMounted } from '~/hooks/common/use-before-mounted'
import { jotaiStore } from '~/lib/store'

export const aggregationDataAtom = atom<null | AggregateRoot>(null)
Expand All @@ -17,6 +18,11 @@ export const AggregationProvider: FC<
aggregationData: AggregateRoot
}>
> = ({ children, aggregationData }) => {
useBeforeMounted(() => {
if (!aggregationData) return
jotaiStore.set(aggregationDataAtom, aggregationData)
})

useEffect(() => {
if (!aggregationData) return
jotaiStore.set(aggregationDataAtom, aggregationData)
Expand All @@ -40,11 +46,6 @@ export const AggregationProvider: FC<
return children
}

/**
* Not recommended to use
*/
export const useAggregationData = () => useAtomValue(aggregationDataAtom)

export const useAggregationSelector = <T,>(
selector: (atomValue: AggregateRoot) => T,
deps: any[] = [],
Expand Down

0 comments on commit 12a0600

Please sign in to comment.