Skip to content

Commit

Permalink
Fix 4484: Updated the cache mechanism on localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
mkbeefcake committed Oct 7, 2023
1 parent 3198d09 commit 4de58ad
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/ui/src/forum/components/category/LatestPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { MemberInfo } from '@/memberships/components'
import { CategoryItemFieldProps } from './CategoryListItem'

export const LatestPost = memo(({ categoryId }: CategoryItemFieldProps) => {
const { post, thread } = useCategoryLatestPost(categoryId)
const { post } = useCategoryLatestPost(categoryId)

if (!post) return <TextMedium>-</TextMedium>

return (
<PostInfoStyles>
<LatestPostLink to={`${generatePath(ForumRoutes.thread, { id: thread?.id ?? '' })}?post=${post.id}`}>
<LatestPostLink to={`${generatePath(ForumRoutes.thread, { id: post.threadId })}?post=${post.id}`}>
Re: {post.text.slice(0, 100)}
</LatestPostLink>
<TextInlineExtraSmall as="div" lighter>
Expand Down
6 changes: 2 additions & 4 deletions packages/ui/src/forum/hooks/useCategoryLatestPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { useMemo } from 'react'
import { ForumThreadOrderByInput } from '@/common/api/queries'
import { useLocalStorage } from '@/common/hooks/useLocalStorage'
import { useGetForumThreadsQuery } from '@/forum/queries'
import { asForumThread } from '@/forum/types'

import { ForumPostsCache } from './useForumLatestPosts'

export const useCategoryLatestPost = (category_eq: string) => {
const { data: threadData, loading: loadingThreads } = useGetForumThreadsQuery({
const { loading: loadingThreads } = useGetForumThreadsQuery({
variables: {
where: { category: { id_eq: category_eq } },
orderBy: ForumThreadOrderByInput.UpdatedAtDesc,
Expand All @@ -33,12 +32,11 @@ export const useCategoryLatestPost = (category_eq: string) => {
// fetchPolicy: 'cache-first',
// })

const [postsCache] = useLocalStorage<ForumPostsCache>()
const [postsCache] = useLocalStorage<ForumPostsCache>('LatestForumPosts')
return useMemo(
() => ({
isLoading: /*loadingPosts || */ loadingThreads,
post: postsCache?.posts.find((post) => post.categoryId === category_eq),
thread: threadData?.forumThreads.length ? asForumThread(threadData?.forumThreads[0]) : undefined,
}),
[postsCache, category_eq]
)
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/forum/hooks/useForumLatestPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export type ForumPostsCache = {
expires: number
posts: ForumPost[]
}
const orderBy = [ForumPostOrderByInput.UpdatedAtDesc]
const variables = { where: { isVisible_eq: true }, orderBy: [ForumPostOrderByInput.UpdatedAtDesc], limit: 500 }

export const useForumLatestPosts = () => {
const [cache, setCache] = useLocalStorage<ForumPostsCache>()
const [cache, setCache] = useLocalStorage<ForumPostsCache>('LatestForumPosts')

const isCached = cache?.expires ? Date.now() < cache?.expires : false
const { data } = useGetForumPostsQuery({
variables: { where: { isVisible_eq: true }, orderBy, limit: 500 },
variables: variables,
skip: isCached,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/forum/hooks/useThreadLatestPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useLocalStorage } from '@/common/hooks/useLocalStorage'
import { ForumPostsCache } from './useForumLatestPosts'

export const useThreadLatestPost = (threadId: string) => {
const [postsCache] = useLocalStorage<ForumPostsCache>()
const [postsCache] = useLocalStorage<ForumPostsCache>('LatestForumPosts')

return useMemo(() => ({ post: postsCache?.posts.find((post) => post.threadId === threadId) }), [postsCache, threadId])
}

0 comments on commit 4de58ad

Please sign in to comment.