mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Remove disabling update while listening on socket (ignoreUpdates state) since the logic is more complicated that was originally thought and it will be tested/revisited on a different PR
GitOrigin-RevId: 70cee2f0fc565aaebfb3ead2dd1a58fc5371c816
This commit is contained in:
parent
677ec173ed
commit
f85d17f894
5 changed files with 34 additions and 76 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, useContext, useMemo, useState } from 'react'
|
import { createContext, useContext, useMemo } from 'react'
|
||||||
import type { PropsWithChildren } from 'react'
|
import type { PropsWithChildren } from 'react'
|
||||||
import type {
|
import type {
|
||||||
FontFamily,
|
FontFamily,
|
||||||
|
@ -52,8 +52,6 @@ export const ProjectSettingsContext = createContext<
|
||||||
export function ProjectSettingsProvider({
|
export function ProjectSettingsProvider({
|
||||||
children,
|
children,
|
||||||
}: PropsWithChildren<Record<string, never>>) {
|
}: PropsWithChildren<Record<string, never>>) {
|
||||||
const [ignoreUpdates, setIgnoreUpdates] = useState(false)
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
compiler,
|
compiler,
|
||||||
setCompiler,
|
setCompiler,
|
||||||
|
@ -63,7 +61,7 @@ export function ProjectSettingsProvider({
|
||||||
setRootDocId,
|
setRootDocId,
|
||||||
spellCheckLanguage,
|
spellCheckLanguage,
|
||||||
setSpellCheckLanguage,
|
setSpellCheckLanguage,
|
||||||
} = useSetProjectWideSettings({ ignoreUpdates })
|
} = useSetProjectWideSettings()
|
||||||
|
|
||||||
const {
|
const {
|
||||||
autoComplete,
|
autoComplete,
|
||||||
|
@ -88,9 +86,7 @@ export function ProjectSettingsProvider({
|
||||||
setPdfViewer,
|
setPdfViewer,
|
||||||
} = useUserWideSettings()
|
} = useUserWideSettings()
|
||||||
|
|
||||||
useProjectWideSettingsSocketListener({
|
useProjectWideSettingsSocketListener()
|
||||||
onListen: () => setIgnoreUpdates(true),
|
|
||||||
})
|
|
||||||
|
|
||||||
const value: ProjectSettingsContextValue = useMemo(
|
const value: ProjectSettingsContextValue = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
|
|
|
@ -2,51 +2,45 @@ import { useCallback, useEffect } from 'react'
|
||||||
import { ProjectCompiler } from '../../../../../types/project-settings'
|
import { ProjectCompiler } from '../../../../../types/project-settings'
|
||||||
import { useIdeContext } from '../../../shared/context/ide-context'
|
import { useIdeContext } from '../../../shared/context/ide-context'
|
||||||
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
||||||
|
import type { ProjectSettingsScope } from '../utils/api'
|
||||||
|
|
||||||
type UseProjectWideSettingsSocketListener = {
|
export default function useProjectWideSettingsSocketListener() {
|
||||||
onListen: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useProjectWideSettingsSocketListener({
|
|
||||||
onListen,
|
|
||||||
}: UseProjectWideSettingsSocketListener) {
|
|
||||||
const ide = useIdeContext()
|
const ide = useIdeContext()
|
||||||
|
|
||||||
const [compilerScope, setCompilerScope] =
|
const [projectScope, setProjectScope] = useScopeValue<
|
||||||
useScopeValue<ProjectCompiler>('project.compiler')
|
ProjectSettingsScope | undefined
|
||||||
const [imageNameScope, setImageNameScope] =
|
>('project', true)
|
||||||
useScopeValue<string>('project.imageName')
|
|
||||||
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
|
|
||||||
useScopeValue<string>('project.spellCheckLanguage')
|
|
||||||
|
|
||||||
const setCompiler = useCallback(
|
const setCompiler = useCallback(
|
||||||
(compiler: ProjectCompiler) => {
|
(compiler: ProjectCompiler) => {
|
||||||
onListen()
|
if (projectScope) {
|
||||||
setCompilerScope(compiler)
|
setProjectScope({ ...projectScope, compiler })
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[setCompilerScope, onListen]
|
[projectScope, setProjectScope]
|
||||||
)
|
)
|
||||||
|
|
||||||
const setImageName = useCallback(
|
const setImageName = useCallback(
|
||||||
(imageName: string) => {
|
(imageName: string) => {
|
||||||
onListen()
|
if (projectScope) {
|
||||||
setImageNameScope(imageName)
|
setProjectScope({ ...projectScope, imageName })
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[setImageNameScope, onListen]
|
[projectScope, setProjectScope]
|
||||||
)
|
)
|
||||||
|
|
||||||
const setSpellCheckLanguage = useCallback(
|
const setSpellCheckLanguage = useCallback(
|
||||||
(spellCheckLanguage: string) => {
|
(spellCheckLanguage: string) => {
|
||||||
onListen()
|
if (projectScope) {
|
||||||
setSpellCheckLanguageScope(spellCheckLanguage)
|
setProjectScope({ ...projectScope, spellCheckLanguage })
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[setSpellCheckLanguageScope, onListen]
|
[projectScope, setProjectScope]
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// data is not available on initial mounting
|
// data is not available on initial mounting
|
||||||
const dataAvailable =
|
const dataAvailable = !!projectScope
|
||||||
compilerScope && imageNameScope && spellCheckLanguageScope
|
|
||||||
|
|
||||||
if (dataAvailable && ide?.socket) {
|
if (dataAvailable && ide?.socket) {
|
||||||
ide.socket.on('compilerUpdated', setCompiler)
|
ide.socket.on('compilerUpdated', setCompiler)
|
||||||
|
@ -63,11 +57,9 @@ export default function useProjectWideSettingsSocketListener({
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
ide?.socket,
|
ide?.socket,
|
||||||
compilerScope,
|
projectScope,
|
||||||
setCompiler,
|
setCompiler,
|
||||||
imageNameScope,
|
|
||||||
setImageName,
|
setImageName,
|
||||||
spellCheckLanguageScope,
|
|
||||||
setSpellCheckLanguage,
|
setSpellCheckLanguage,
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,7 @@ import { ProjectSettingsScope, saveProjectSettings } from '../utils/api'
|
||||||
import useSetRootDocId from './use-set-root-doc-id'
|
import useSetRootDocId from './use-set-root-doc-id'
|
||||||
import useSetSpellCheckLanguage from './use-set-spell-check-language'
|
import useSetSpellCheckLanguage from './use-set-spell-check-language'
|
||||||
|
|
||||||
type UseSetProjectWideSettings = {
|
export default function useSetProjectWideSettings() {
|
||||||
ignoreUpdates: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useSetProjectWideSettings({
|
|
||||||
ignoreUpdates,
|
|
||||||
}: UseSetProjectWideSettings) {
|
|
||||||
// The value will be undefined on mount
|
// The value will be undefined on mount
|
||||||
const [project, setProject] = useScopeValue<ProjectSettingsScope | undefined>(
|
const [project, setProject] = useScopeValue<ProjectSettingsScope | undefined>(
|
||||||
'project',
|
'project',
|
||||||
|
@ -22,30 +16,30 @@ export default function useSetProjectWideSettings({
|
||||||
|
|
||||||
const setCompiler = useCallback(
|
const setCompiler = useCallback(
|
||||||
(compiler: ProjectCompiler) => {
|
(compiler: ProjectCompiler) => {
|
||||||
const allowUpdate = !ignoreUpdates && project?.compiler
|
const allowUpdate = project?.compiler
|
||||||
|
|
||||||
if (allowUpdate) {
|
if (allowUpdate) {
|
||||||
setProject({ ...project, compiler })
|
setProject({ ...project, compiler })
|
||||||
saveProjectSettings({ projectId, compiler })
|
saveProjectSettings({ projectId, compiler })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[projectId, project, setProject, ignoreUpdates]
|
[projectId, project, setProject]
|
||||||
)
|
)
|
||||||
|
|
||||||
const setImageName = useCallback(
|
const setImageName = useCallback(
|
||||||
(imageName: string) => {
|
(imageName: string) => {
|
||||||
const allowUpdate = !ignoreUpdates && project?.imageName
|
const allowUpdate = project?.imageName
|
||||||
|
|
||||||
if (allowUpdate) {
|
if (allowUpdate) {
|
||||||
setProject({ ...project, imageName })
|
setProject({ ...project, imageName })
|
||||||
saveProjectSettings({ projectId, imageName })
|
saveProjectSettings({ projectId, imageName })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[projectId, project, setProject, ignoreUpdates]
|
[projectId, project, setProject]
|
||||||
)
|
)
|
||||||
|
|
||||||
const setRootDocId = useSetRootDocId({ ignoreUpdates })
|
const setRootDocId = useSetRootDocId()
|
||||||
const setSpellCheckLanguage = useSetSpellCheckLanguage({ ignoreUpdates })
|
const setSpellCheckLanguage = useSetSpellCheckLanguage()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
compiler: project?.compiler,
|
compiler: project?.compiler,
|
||||||
|
|
|
@ -4,11 +4,7 @@ import { useProjectContext } from '../../../shared/context/project-context'
|
||||||
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
||||||
import { saveProjectSettings } from '../utils/api'
|
import { saveProjectSettings } from '../utils/api'
|
||||||
|
|
||||||
type UseSetRootDocId = {
|
export default function useSetRootDocId() {
|
||||||
ignoreUpdates: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
|
|
||||||
const [rootDocIdScope, setRootDocIdScope] =
|
const [rootDocIdScope, setRootDocIdScope] =
|
||||||
useScopeValue<string>('project.rootDoc_id')
|
useScopeValue<string>('project.rootDoc_id')
|
||||||
const { permissionsLevel } = useEditorContext()
|
const { permissionsLevel } = useEditorContext()
|
||||||
|
@ -17,7 +13,6 @@ export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
|
||||||
const setRootDocId = useCallback(
|
const setRootDocId = useCallback(
|
||||||
async (rootDocId: string) => {
|
async (rootDocId: string) => {
|
||||||
const allowUpdate =
|
const allowUpdate =
|
||||||
!ignoreUpdates &&
|
|
||||||
typeof rootDocIdScope !== 'undefined' &&
|
typeof rootDocIdScope !== 'undefined' &&
|
||||||
permissionsLevel !== 'readOnly' &&
|
permissionsLevel !== 'readOnly' &&
|
||||||
rootDocIdScope !== rootDocId
|
rootDocIdScope !== rootDocId
|
||||||
|
@ -31,13 +26,7 @@ export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[permissionsLevel, projectId, rootDocIdScope, setRootDocIdScope]
|
||||||
permissionsLevel,
|
|
||||||
projectId,
|
|
||||||
rootDocIdScope,
|
|
||||||
setRootDocIdScope,
|
|
||||||
ignoreUpdates,
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
return setRootDocId
|
return setRootDocId
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,7 @@ import { useProjectContext } from '../../../shared/context/project-context'
|
||||||
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
||||||
import { saveProjectSettings, saveUserSettings } from '../utils/api'
|
import { saveProjectSettings, saveUserSettings } from '../utils/api'
|
||||||
|
|
||||||
type UseSetSpellCheckLanguage = {
|
export default function useSetSpellCheckLanguage() {
|
||||||
ignoreUpdates: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useSetSpellCheckLanguage({
|
|
||||||
ignoreUpdates,
|
|
||||||
}: UseSetSpellCheckLanguage) {
|
|
||||||
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
|
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
|
||||||
useScopeValue<string>('project.spellCheckLanguage')
|
useScopeValue<string>('project.spellCheckLanguage')
|
||||||
const { _id: projectId } = useProjectContext()
|
const { _id: projectId } = useProjectContext()
|
||||||
|
@ -18,9 +12,7 @@ export default function useSetSpellCheckLanguage({
|
||||||
const setSpellCheckLanguage = useCallback(
|
const setSpellCheckLanguage = useCallback(
|
||||||
(spellCheckLanguage: string) => {
|
(spellCheckLanguage: string) => {
|
||||||
const allowUpdate =
|
const allowUpdate =
|
||||||
!ignoreUpdates &&
|
spellCheckLanguage && spellCheckLanguage !== spellCheckLanguageScope
|
||||||
spellCheckLanguage &&
|
|
||||||
spellCheckLanguage !== spellCheckLanguageScope
|
|
||||||
|
|
||||||
if (allowUpdate) {
|
if (allowUpdate) {
|
||||||
sendMB('setting-changed', {
|
sendMB('setting-changed', {
|
||||||
|
@ -35,12 +27,7 @@ export default function useSetSpellCheckLanguage({
|
||||||
saveUserSettings({ spellCheckLanguage })
|
saveUserSettings({ spellCheckLanguage })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[projectId, setSpellCheckLanguageScope, spellCheckLanguageScope]
|
||||||
projectId,
|
|
||||||
setSpellCheckLanguageScope,
|
|
||||||
spellCheckLanguageScope,
|
|
||||||
ignoreUpdates,
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return setSpellCheckLanguage
|
return setSpellCheckLanguage
|
||||||
|
|
Loading…
Reference in a new issue