Use in-memory store for home featured section data

This commit is contained in:
James Grugett 2022-10-12 21:15:40 -05:00
parent bc6fab399e
commit 4e5b78f4ee
3 changed files with 29 additions and 6 deletions

View File

@ -1,12 +1,17 @@
import { GlobalConfig } from 'common/globalConfig' import { GlobalConfig } from 'common/globalConfig'
import { useEffect, useState } from 'react' import { useEffect } from 'react'
import { listenForGlobalConfig } from 'web/lib/firebase/globalConfig' import { listenForGlobalConfig } from 'web/lib/firebase/globalConfig'
import { inMemoryStore, usePersistentState } from './use-persistent-state'
export const useGlobalConfig = () => { export const useGlobalConfig = () => {
const [globalConfig, setGlobalConfig] = useState<GlobalConfig | null>(null) const [globalConfig, setGlobalConfig] =
usePersistentState<GlobalConfig | null>(null, {
store: inMemoryStore(),
key: 'globalConfig',
})
useEffect(() => { useEffect(() => {
return listenForGlobalConfig(setGlobalConfig) return listenForGlobalConfig(setGlobalConfig)
}, []) }, [setGlobalConfig])
return globalConfig return globalConfig
} }

View File

@ -89,6 +89,17 @@ export const historyStore = <T>(prefix = '__manifold'): PersistentStore<T> => ({
}, },
}) })
const store: Record<string, any> = {}
export const inMemoryStore = <T>(): PersistentStore<T> => ({
get: (k: string) => {
return store[k]
},
set: (k: string, v: T | undefined) => {
store[k] = v
},
})
export const usePersistentState = <T>( export const usePersistentState = <T>(
initial: T, initial: T,
persist?: PersistenceOptions<T> persist?: PersistenceOptions<T>

View File

@ -1,4 +1,4 @@
import React, { ReactNode, useEffect, useState } from 'react' import React, { ReactNode, useEffect } from 'react'
import Router from 'next/router' import Router from 'next/router'
import { import {
AdjustmentsIcon, AdjustmentsIcon,
@ -63,6 +63,10 @@ import { useAllPosts } from 'web/hooks/use-post'
import { useGlobalConfig } from 'web/hooks/use-global-config' import { useGlobalConfig } from 'web/hooks/use-global-config'
import { useAdmin } from 'web/hooks/use-admin' import { useAdmin } from 'web/hooks/use-admin'
import { GlobalConfig } from 'common/globalConfig' import { GlobalConfig } from 'common/globalConfig'
import {
inMemoryStore,
usePersistentState,
} from 'web/hooks/use-persistent-state'
export default function Home() { export default function Home() {
const user = useUser() const user = useUser()
@ -105,7 +109,10 @@ export default function Home() {
groups?.map((g) => g.slug) groups?.map((g) => g.slug)
) )
const [pinned, setPinned] = useState<JSX.Element[] | null>(null) const [pinned, setPinned] = usePersistentState<JSX.Element[] | null>(null, {
store: inMemoryStore(),
key: 'home-pinned',
})
useEffect(() => { useEffect(() => {
const pinnedItems = globalConfig?.pinnedItems const pinnedItems = globalConfig?.pinnedItems
@ -139,7 +146,7 @@ export default function Home() {
} }
} }
getPinned() getPinned()
}, [globalConfig]) }, [globalConfig, setPinned])
const isLoading = const isLoading =
!user || !user ||