Skip to content

Commit

Permalink
refactor: set apollo client ssrMode only on server, use client polling
Browse files Browse the repository at this point in the history
- replace manual refetch with interval with apollo client build in
  pollInterval
  • Loading branch information
Danijel Bjelajac committed Nov 6, 2023
1 parent 84752e4 commit 46714c3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
36 changes: 1 addition & 35 deletions src/hooks/useSessionActivities/useSessionActivities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
useGetHostedSessionActivitiesQuery,
GetHostedSessionActivitiesDocument,
GetHostedSessionActivitiesQuery,
ActivityStatus,
} from './types'
import { captureException } from '@sentry/nextjs'

Expand All @@ -30,6 +29,7 @@ export const useSessionActivities = (): UsePathwayActivitiesHook => {
const client = useApolloClient()
const { data, error, loading, refetch } = useGetHostedSessionActivitiesQuery({
variables,
pollInterval: POLLING_DELAY,
onError: (error) => {
captureException(error, {
contexts: {
Expand Down Expand Up @@ -68,40 +68,6 @@ export const useSessionActivities = (): UsePathwayActivitiesHook => {
data?.hostedSessionActivities.activities as Activity[]
) ?? []

// temporary solution to refetch query when subscription does not work
useEffect(() => {
let refetchQueryInterval: NodeJS.Timer | undefined

/**
* Note: refetch() activities can return error if we continuously
* call it again and again.
*/

/**
* Only try to refetch activities if there are none found.
* That means we would only risk showing an error message
* when a user is waiting for activities to load, not when
* they are actively interacting with one.
*/

const activeActivities = activities.filter(
({ status }) => status === ActivityStatus.Active
)

if (activeActivities.length === 0) {
refetchQueryInterval = setInterval(() => {
refetch()
}, POLLING_DELAY)
}

if (activeActivities.length !== 0 && !isNil(refetchQueryInterval)) {
clearInterval(refetchQueryInterval)
}

// clear interval on component unmount
return () => clearInterval(refetchQueryInterval)
}, [activities])

useEffect(() => {
if (!isNil(onActivityCreated.data)) {
const {
Expand Down
2 changes: 1 addition & 1 deletion src/services/graphql/apollo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const createClient = ({
const link = wsLink ? split(isSubscription, wsLink, defaultLink) : defaultLink

return new ApolloClient({
ssrMode: true,
ssrMode: typeof window === 'undefined',
connectToDevTools: process.env.NODE_ENV !== 'production',
cache: new InMemoryCache(cacheConfig),
link,
Expand Down

0 comments on commit 46714c3

Please sign in to comment.