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:
M Fahru 2023-01-03 10:04:34 -07:00 committed by Copybot
parent 677ec173ed
commit f85d17f894
5 changed files with 34 additions and 76 deletions

View file

@ -1,4 +1,4 @@
import { createContext, useContext, useMemo, useState } from 'react'
import { createContext, useContext, useMemo } from 'react'
import type { PropsWithChildren } from 'react'
import type {
FontFamily,
@ -52,8 +52,6 @@ export const ProjectSettingsContext = createContext<
export function ProjectSettingsProvider({
children,
}: PropsWithChildren<Record<string, never>>) {
const [ignoreUpdates, setIgnoreUpdates] = useState(false)
const {
compiler,
setCompiler,
@ -63,7 +61,7 @@ export function ProjectSettingsProvider({
setRootDocId,
spellCheckLanguage,
setSpellCheckLanguage,
} = useSetProjectWideSettings({ ignoreUpdates })
} = useSetProjectWideSettings()
const {
autoComplete,
@ -88,9 +86,7 @@ export function ProjectSettingsProvider({
setPdfViewer,
} = useUserWideSettings()
useProjectWideSettingsSocketListener({
onListen: () => setIgnoreUpdates(true),
})
useProjectWideSettingsSocketListener()
const value: ProjectSettingsContextValue = useMemo(
() => ({

View file

@ -2,51 +2,45 @@ import { useCallback, useEffect } from 'react'
import { ProjectCompiler } from '../../../../../types/project-settings'
import { useIdeContext } from '../../../shared/context/ide-context'
import useScopeValue from '../../../shared/hooks/use-scope-value'
import type { ProjectSettingsScope } from '../utils/api'
type UseProjectWideSettingsSocketListener = {
onListen: () => void
}
export default function useProjectWideSettingsSocketListener({
onListen,
}: UseProjectWideSettingsSocketListener) {
export default function useProjectWideSettingsSocketListener() {
const ide = useIdeContext()
const [compilerScope, setCompilerScope] =
useScopeValue<ProjectCompiler>('project.compiler')
const [imageNameScope, setImageNameScope] =
useScopeValue<string>('project.imageName')
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
useScopeValue<string>('project.spellCheckLanguage')
const [projectScope, setProjectScope] = useScopeValue<
ProjectSettingsScope | undefined
>('project', true)
const setCompiler = useCallback(
(compiler: ProjectCompiler) => {
onListen()
setCompilerScope(compiler)
if (projectScope) {
setProjectScope({ ...projectScope, compiler })
}
},
[setCompilerScope, onListen]
[projectScope, setProjectScope]
)
const setImageName = useCallback(
(imageName: string) => {
onListen()
setImageNameScope(imageName)
if (projectScope) {
setProjectScope({ ...projectScope, imageName })
}
},
[setImageNameScope, onListen]
[projectScope, setProjectScope]
)
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: string) => {
onListen()
setSpellCheckLanguageScope(spellCheckLanguage)
if (projectScope) {
setProjectScope({ ...projectScope, spellCheckLanguage })
}
},
[setSpellCheckLanguageScope, onListen]
[projectScope, setProjectScope]
)
useEffect(() => {
// data is not available on initial mounting
const dataAvailable =
compilerScope && imageNameScope && spellCheckLanguageScope
const dataAvailable = !!projectScope
if (dataAvailable && ide?.socket) {
ide.socket.on('compilerUpdated', setCompiler)
@ -63,11 +57,9 @@ export default function useProjectWideSettingsSocketListener({
}
}, [
ide?.socket,
compilerScope,
projectScope,
setCompiler,
imageNameScope,
setImageName,
spellCheckLanguageScope,
setSpellCheckLanguage,
])
}

View file

@ -6,13 +6,7 @@ import { ProjectSettingsScope, saveProjectSettings } from '../utils/api'
import useSetRootDocId from './use-set-root-doc-id'
import useSetSpellCheckLanguage from './use-set-spell-check-language'
type UseSetProjectWideSettings = {
ignoreUpdates: boolean
}
export default function useSetProjectWideSettings({
ignoreUpdates,
}: UseSetProjectWideSettings) {
export default function useSetProjectWideSettings() {
// The value will be undefined on mount
const [project, setProject] = useScopeValue<ProjectSettingsScope | undefined>(
'project',
@ -22,30 +16,30 @@ export default function useSetProjectWideSettings({
const setCompiler = useCallback(
(compiler: ProjectCompiler) => {
const allowUpdate = !ignoreUpdates && project?.compiler
const allowUpdate = project?.compiler
if (allowUpdate) {
setProject({ ...project, compiler })
saveProjectSettings({ projectId, compiler })
}
},
[projectId, project, setProject, ignoreUpdates]
[projectId, project, setProject]
)
const setImageName = useCallback(
(imageName: string) => {
const allowUpdate = !ignoreUpdates && project?.imageName
const allowUpdate = project?.imageName
if (allowUpdate) {
setProject({ ...project, imageName })
saveProjectSettings({ projectId, imageName })
}
},
[projectId, project, setProject, ignoreUpdates]
[projectId, project, setProject]
)
const setRootDocId = useSetRootDocId({ ignoreUpdates })
const setSpellCheckLanguage = useSetSpellCheckLanguage({ ignoreUpdates })
const setRootDocId = useSetRootDocId()
const setSpellCheckLanguage = useSetSpellCheckLanguage()
return {
compiler: project?.compiler,

View file

@ -4,11 +4,7 @@ import { useProjectContext } from '../../../shared/context/project-context'
import useScopeValue from '../../../shared/hooks/use-scope-value'
import { saveProjectSettings } from '../utils/api'
type UseSetRootDocId = {
ignoreUpdates: boolean
}
export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
export default function useSetRootDocId() {
const [rootDocIdScope, setRootDocIdScope] =
useScopeValue<string>('project.rootDoc_id')
const { permissionsLevel } = useEditorContext()
@ -17,7 +13,6 @@ export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
const setRootDocId = useCallback(
async (rootDocId: string) => {
const allowUpdate =
!ignoreUpdates &&
typeof rootDocIdScope !== 'undefined' &&
permissionsLevel !== 'readOnly' &&
rootDocIdScope !== rootDocId
@ -31,13 +26,7 @@ export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
}
}
},
[
permissionsLevel,
projectId,
rootDocIdScope,
setRootDocIdScope,
ignoreUpdates,
]
[permissionsLevel, projectId, rootDocIdScope, setRootDocIdScope]
)
return setRootDocId
}

View file

@ -4,13 +4,7 @@ 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) {
export default function useSetSpellCheckLanguage() {
const [spellCheckLanguageScope, setSpellCheckLanguageScope] =
useScopeValue<string>('project.spellCheckLanguage')
const { _id: projectId } = useProjectContext()
@ -18,9 +12,7 @@ export default function useSetSpellCheckLanguage({
const setSpellCheckLanguage = useCallback(
(spellCheckLanguage: string) => {
const allowUpdate =
!ignoreUpdates &&
spellCheckLanguage &&
spellCheckLanguage !== spellCheckLanguageScope
spellCheckLanguage && spellCheckLanguage !== spellCheckLanguageScope
if (allowUpdate) {
sendMB('setting-changed', {
@ -35,12 +27,7 @@ export default function useSetSpellCheckLanguage({
saveUserSettings({ spellCheckLanguage })
}
},
[
projectId,
setSpellCheckLanguageScope,
spellCheckLanguageScope,
ignoreUpdates,
]
[projectId, setSpellCheckLanguageScope, spellCheckLanguageScope]
)
return setSpellCheckLanguage