Merge pull request #16010 from overleaf/ae-socket-listeners-usecallback

[ide-react] Wrap socket listeners in useCallback

GitOrigin-RevId: 09692ac0026b6f94626eece4583ae320039a4ee9
This commit is contained in:
Alf Eaton 2023-12-05 09:57:59 +00:00 committed by Copybot
parent 3f98752986
commit df472b607e

View file

@ -9,6 +9,8 @@ import { useConnectionContext } from '@/features/ide-react/context/connection-co
import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context' import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
import { useModalsContext } from '@/features/ide-react/context/modals-context' import { useModalsContext } from '@/features/ide-react/context/modals-context'
import { debugConsole } from '@/utils/debugging' import { debugConsole } from '@/utils/debugging'
import { useCallback } from 'react'
import { PublicAccessLevel } from '../../../../../types/public-access-level'
function useSocketListeners() { function useSocketListeners() {
const { t } = useTranslation() const { t } = useTranslation()
@ -19,46 +21,64 @@ function useSocketListeners() {
const [, setProjectMembers] = useScopeValue('project.members') const [, setProjectMembers] = useScopeValue('project.members')
const [, setProjectInvites] = useScopeValue('project.invites') const [, setProjectInvites] = useScopeValue('project.invites')
useSocketListener(socket, 'project:access:revoked', () => { useSocketListener(
showGenericMessageModal( socket,
t('removed_from_project'), 'project:access:revoked',
t( useCallback(() => {
'you_have_been_removed_from_this_project_and_will_be_redirected_to_project_dashboard' showGenericMessageModal(
t('removed_from_project'),
t(
'you_have_been_removed_from_this_project_and_will_be_redirected_to_project_dashboard'
)
) )
}, [showGenericMessageModal, t])
)
useSocketListener(
socket,
'project:publicAccessLevel:changed',
useCallback(
(data: { newAccessLevel?: PublicAccessLevel }) => {
if (data.newAccessLevel) {
setPublicAccessLevel(data.newAccessLevel)
}
},
[setPublicAccessLevel]
) )
}) )
useSocketListener(socket, 'project:publicAccessLevel:changed', data => { useSocketListener(
if (data.newAccessLevel) { socket,
setPublicAccessLevel(data.newAccessLevel) 'project:membership:changed',
} useCallback(
}) (data: { members?: boolean; invites?: boolean }) => {
if (data.members) {
listProjectMembers(projectId)
.then(({ members }) => {
if (members) {
setProjectMembers(members)
}
})
.catch(err => {
debugConsole.error('Error fetching members for project', err)
})
}
useSocketListener(socket, 'project:membership:changed', data => { if (data.invites) {
if (data.members) { listProjectInvites(projectId)
listProjectMembers(projectId) .then(({ invites }) => {
.then(({ members }) => { if (invites) {
if (members) { setProjectInvites(invites)
setProjectMembers(members) }
} })
}) .catch(err => {
.catch(err => { debugConsole.error('Error fetching invites for project', err)
debugConsole.error('Error fetching members for project', err) })
}) }
} },
[projectId, setProjectInvites, setProjectMembers]
if (data.invites) { )
listProjectInvites(projectId) )
.then(({ invites }) => {
if (invites) {
setProjectInvites(invites)
}
})
.catch(err => {
debugConsole.error('Error fetching invites for project', err)
})
}
})
} }
export default useSocketListeners export default useSocketListeners