mirror of
https://github.com/overleaf/overleaf.git
synced 2024-09-16 02:52:31 -04:00
677ec173ed
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
47 lines
1.4 KiB
TypeScript
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
|
|
}
|