Skip to content

Commit

Permalink
feat(collaboration): get collaborators regardless; only collaborate o…
Browse files Browse the repository at this point in the history
…n an active tab
  • Loading branch information
benforshey committed Sep 20, 2024
1 parent 1fbc884 commit 97c6f5c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
26 changes: 26 additions & 0 deletions packages/app/collaboration/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ export async function updateCollaborators(
return [error, null]
}
}

export async function getCollaborators(
documentTitle: string,
modelName: string
): Promise<ErrorData<Collaborator[]>> {
try {
const response = await fetch(
`/meditor/api/collaboration/models/${encodeURIComponent(
modelName
)}/documents/${encodeURIComponent(documentTitle)}`,
{ method: 'GET' }
)

if (!response.ok) {
const { error }: APIError = await response.json()

throw new HttpException(ErrorCode.InternalServerError, error)
}

const collaborators = await response.json()

return [null, collaborators]
} catch (error) {
return [error, null]
}
}
10 changes: 5 additions & 5 deletions packages/app/collaboration/lib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { User } from 'auth/types'
import type { Collaborator } from './types'

export function filterActiveUser(
collaborators: Collaborator[] = [],
activeUser: User
) {
return collaborators.filter(collaborator => collaborator.uid !== activeUser.uid)
export function filterActiveUser(collaborators: Collaborator[], activeUser: User) {
return (
collaborators?.filter(collaborator => collaborator.uid !== activeUser.uid) ??
[]
)
}
49 changes: 33 additions & 16 deletions packages/app/pages/[modelName]/[documentTitle]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { adaptUserToCollaborator } from 'collaboration/adapters'
import { updateCollaborators } from 'collaboration/http'
import { getCollaborators, updateCollaborators } from 'collaboration/http'
import { filterActiveUser } from 'collaboration/lib'
import { getDocumentCollaborators } from 'collaboration/service'
import type { Collaborator } from 'collaboration/types'
import cloneDeep from 'lodash.clonedeep'
import type { NextPageContext } from 'next'
import { useRouter } from 'next/router'
import { useContext, useEffect, useState } from 'react'
import type { HttpException } from 'utils/errors'
import { getLoggedInUser } from '../../../auth/user'
import { getCommentsForDocument } from '../../../comments/service'
import type { DocumentComment } from '../../../comments/types'
Expand Down Expand Up @@ -98,12 +99,15 @@ const EditDocumentPage = ({
let intervalId: null | ReturnType<typeof globalThis.setInterval>
let consecutiveErrorCount = 0

if (userActivation) {
handleCollaborators()
intervalId = globalThis.setInterval(handleCollaborators, 3000)
}
handleCollaborators()
intervalId = globalThis.setInterval(handleCollaborators, 3000)

async function handleCollaborators() {
// No need to trigger a request if the document is not visible.
if (globalThis.document.hidden) {
return
}

// No need to keep triggering a re-render / sending a network request if collaboration isn't working.
if (consecutiveErrorCount > 5) {
console.warn(
Expand All @@ -112,18 +116,31 @@ const EditDocumentPage = ({
return globalThis.clearInterval(intervalId)
}

const collaborator = adaptUserToCollaborator(
user,
privileges,
userActivation.hasBeenActive,
userActivation.isActive
)
let collaborators: Collaborator[]
let error: Error | HttpException

const [error, collaborators] = await updateCollaborators(
collaborator,
documentTitle,
modelName
)
if (userActivation) {
const collaborator = adaptUserToCollaborator(
user,
privileges,
userActivation.hasBeenActive,
userActivation.isActive
)

const [updateError, collaboratorsWithUser] =
await updateCollaborators(collaborator, documentTitle, modelName)

collaborators = collaboratorsWithUser
error = updateError
} else {
const [getError, collaboratorsWithoutUser] = await getCollaborators(
documentTitle,
modelName
)

collaborators = collaboratorsWithoutUser
error = getError
}

consecutiveErrorCount = error ? consecutiveErrorCount + 1 : 0

Expand Down

0 comments on commit 97c6f5c

Please sign in to comment.