Skip to content

Commit

Permalink
Fix Forum returns 404 for correct links (#4318)
Browse files Browse the repository at this point in the history
* fixes #4108 Forum returns 404 for correct links

* fixes #4318 handle errors for forum threads

* #4108 handle thread errors

* Add dot to error description

Co-authored-by: Theophile Sandoz <theophile.sandoz@gmail.com>

* Fix use `useRefetchQueries`

---------

Co-authored-by: ilya Smiyukha <ilya.smiyukha@touchcast.com>
Co-authored-by: Theophile Sandoz <theophile.sandoz@gmail.com>
  • Loading branch information
3 people authored Apr 21, 2023
1 parent e26105a commit bbdd6fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
22 changes: 11 additions & 11 deletions packages/ui/src/app/pages/Forum/ForumThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import { PageHeaderRow, PageHeaderWrapper } from '@/app/components/PageLayout'
import { BadgesRow, BadgeStatus } from '@/common/components/BadgeStatus'
import { BlockTime } from '@/common/components/BlockTime'
import { ButtonsGroup, CopyButtonTemplate } from '@/common/components/buttons'
import { EmptyPagePlaceholder } from '@/common/components/EmptyPagePlaceholder/EmptyPagePlaceholder'
import { LinkIcon } from '@/common/components/icons'
import { PinIcon } from '@/common/components/icons/PinIcon'
import { MainPanel, RowGapBlock } from '@/common/components/page/PageContent'
import { PreviousPage } from '@/common/components/page/PreviousPage'
import { Colors } from '@/common/constants'
import { useRefetchQueries } from '@/common/hooks/useRefetchQueries'
import { createType } from '@/common/model/createType'
import { MILLISECONDS_PER_BLOCK } from '@/common/model/formatters'
import { metadataToBytes } from '@/common/model/JoystreamNode'
import { getUrl } from '@/common/utils/getUrl'
import { PostList } from '@/forum/components/PostList/PostList'
Expand All @@ -30,12 +29,7 @@ import { ForumPageLayout } from './components/ForumPageLayout'

export const ForumThread = () => {
const { id } = useParams<{ id: string }>()
const { isLoading: isLoadingThread, thread } = useForumThread(id)
const isRefetched = useRefetchQueries({
interval: MILLISECONDS_PER_BLOCK,
include: ['GetForumThreads'],
})
const isLoading = isLoadingThread && !isRefetched
const { isLoading, thread, hasError } = useForumThread(id)
const { api } = useApi()
const { active } = useMyMemberships()

Expand All @@ -59,7 +53,7 @@ export const ForumThread = () => {
}

useEffect(() => {
if (!isLoading && !thread) {
if (!isLoading && !thread && !hasError) {
history.replace('/404')
}
}, [isLoading, thread])
Expand Down Expand Up @@ -107,8 +101,14 @@ export const ForumThread = () => {

const displayMain = () => (
<ThreadPanel ref={sideNeighborRef}>
<PostList threadId={id} isThreadActive={isThreadActive} isLoading={isLoading} />
{thread && isThreadActive && <NewThreadPost ref={newPostRef} getTransaction={getTransaction} />}
{hasError ? (
<EmptyPagePlaceholder title="Something went wrong fetching this thread." copy="" button={null} />
) : (
<>
<PostList threadId={id} isThreadActive={isThreadActive} isLoading={isLoading} />
{thread && isThreadActive && <NewThreadPost ref={newPostRef} getTransaction={getTransaction} />}
</>
)}
</ThreadPanel>
)

Expand Down
25 changes: 9 additions & 16 deletions packages/ui/src/common/hooks/useRefetchQueries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useApolloClient } from '@apollo/client'
import { ApolloQueryResult, OnQueryUpdated, useApolloClient } from '@apollo/client'
import { DependencyList, useEffect, useRef } from 'react'

interface RefetchOptions {
Expand All @@ -20,29 +20,22 @@ export const useRefetchQueries = (
const isRefetched = useRef(false)
const apolloClient = useApolloClient()
const couldRefetchNext = useRef(typeof after === 'undefined')
const onQueryUpdated: OnQueryUpdated<Promise<ApolloQueryResult<any>>> = (_, { complete }) => {
if (complete) {
isRefetched.current = true
}
return complete ?? false
}

useEffect(() => {
if (couldRefetchNext.current && when) {
if (interval) {
const handler = setInterval(() => {
apolloClient.refetchQueries({
include,
onQueryUpdated(_, { complete }) {
if (complete) {
isRefetched.current = true
}
return complete
},
})
isRefetched.current = true
}, interval)

const handler = setInterval(() => apolloClient.refetchQueries({ include, onQueryUpdated }), interval)
return () => {
clearInterval(handler)
}
} else {
apolloClient.refetchQueries({ include })
isRefetched.current = true
apolloClient.refetchQueries({ include, onQueryUpdated })
}
}
couldRefetchNext.current = after !== false
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/src/forum/components/PostList/PostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { RowGapBlock } from '@/common/components/page/PageContent'
import { Pagination } from '@/common/components/Pagination'
import { Colors } from '@/common/constants'
import { useLocation } from '@/common/hooks/useLocation'
import { useRefetchQueries } from '@/common/hooks/useRefetchQueries'
import { useRouteQuery } from '@/common/hooks/useRouteQuery'
import { MILLISECONDS_PER_BLOCK } from '@/common/model/formatters'
import { AnyKeys } from '@/common/types'
import { getUrl } from '@/common/utils/getUrl'
import { ForumRoutes } from '@/forum/constant'
Expand All @@ -29,8 +31,9 @@ export const PostList = ({ threadId, isThreadActive, isLoading, isDiscussion }:

const navigation = { post: query.get('post'), page: query.get('page') }
const { isLoading: isLoadingPosts, posts, page, pageCount } = useForumThreadPosts(threadId, navigation)
const isRefetched = useRefetchQueries({ interval: MILLISECONDS_PER_BLOCK, include: ['GetForumPosts'] }, [])

const isReady = useMemo(() => !(isLoading || isLoadingPosts), [posts, pageCount])
const isReady = useMemo(() => (!isLoading && !isLoadingPosts) || isRefetched, [posts, pageCount])
const setPage = useCallback(
(page: number) => history.replace({ pathname, search: page > 1 ? `page=${page}` : '' }),
[]
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/forum/hooks/useForumThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { useGetForumThreadQuery } from '@/forum/queries/__generated__/forum.gene
import { asForumThreadWithDetails } from '@/forum/types'

export const useForumThread = (threadId: string) => {
const { loading, data } = useGetForumThreadQuery({ variables: { where: { id: threadId } } })
const { loading, data, error } = useGetForumThreadQuery({ variables: { where: { id: threadId } } })

return {
isLoading: loading,
thread: data && data.thread ? asForumThreadWithDetails(data.thread) : null,
hasError: !!error,
}
}

2 comments on commit bbdd6fe

@vercel
Copy link

@vercel vercel bot commented on bbdd6fe Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on bbdd6fe Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pioneer-2 – ./

pioneer-2-joystream.vercel.app
pioneer-2.vercel.app
pioneer-2-git-dev-joystream.vercel.app

Please sign in to comment.