overleaf/services/web/frontend/js/features/editor-left-menu/hooks/use-set-spell-check-language.tsx
M Fahru 677ec173ed Disable updating project-wide settings while socket is currently listening to update project-wide settings in a project.
This may happen if the project is being used by multiple people, and we want to avoid race condition on the update since it's possible for multiple people to update setting value at the same time.

GitOrigin-RevId: cdad6a6456e2d9e4ef1812ebfd6f6ef59f23747f
2023-01-11 09:08:06 +00:00

47 lines
1.4 KiB
TypeScript

import { useCallback } from 'react'
import { sendMB } from '../../../infrastructure/event-tracking'
import { useProjectContext } from '../../../shared/context/project-context'
import useScopeValue from '../../../shared/hooks/use-scope-value'
import { saveProjectSettings, saveUserSettings } from '../utils/api'
type UseSetSpellCheckLanguage = {
ignoreUpdates: boolean
}
export default function useSetSpellCheckLanguage({
ignoreUpdates,
}: UseSetSpellCheckLanguage) {
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
useScopeValue<string>('project.spellCheckLanguage')
const { _id: projectId } = useProjectContext()
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: string) => {
const allowUpdate =
!ignoreUpdates &&
spellCheckLanguage &&
spellCheckLanguage !== spellCheckLanguageScope
if (allowUpdate) {
sendMB('setting-changed', {
changedSetting: 'spellCheckLanguage',
changedSettingVal: spellCheckLanguage,
})
setSpellCheckLanguageScope(spellCheckLanguage)
// save to both project setting and user setting
saveProjectSettings({ projectId, spellCheckLanguage })
saveUserSettings({ spellCheckLanguage })
}
},
[
projectId,
setSpellCheckLanguageScope,
spellCheckLanguageScope,
ignoreUpdates,
]
)
return setSpellCheckLanguage
}