Просмотр исходного кода

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
M Fahru 3 лет назад
Родитель
Сommit
f85d17f894

+ 3 - 7
services/web/frontend/js/features/editor-left-menu/context/project-settings-context.tsx

@@ -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({
-    onListen: () => setIgnoreUpdates(true),
-  })
+  useProjectWideSettingsSocketListener()
 
 
   const value: ProjectSettingsContextValue = useMemo(
   const value: ProjectSettingsContextValue = useMemo(
     () => ({
     () => ({

+ 19 - 27
services/web/frontend/js/features/editor-left-menu/hooks/use-project-wide-settings-socket-listener.tsx

@@ -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 = {
-  onListen: () => void
-}
-
-export default function useProjectWideSettingsSocketListener({
-  onListen,
-}: UseProjectWideSettingsSocketListener) {
+export default function useProjectWideSettingsSocketListener() {
   const ide = useIdeContext()
   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(
   const setCompiler = useCallback(
     (compiler: ProjectCompiler) => {
     (compiler: ProjectCompiler) => {
-      onListen()
-      setCompilerScope(compiler)
+      if (projectScope) {
+        setProjectScope({ ...projectScope, compiler })
+      }
     },
     },
-    [setCompilerScope, onListen]
+    [projectScope, setProjectScope]
   )
   )
 
 
   const setImageName = useCallback(
   const setImageName = useCallback(
     (imageName: string) => {
     (imageName: string) => {
-      onListen()
-      setImageNameScope(imageName)
+      if (projectScope) {
+        setProjectScope({ ...projectScope, imageName })
+      }
     },
     },
-    [setImageNameScope, onListen]
+    [projectScope, setProjectScope]
   )
   )
 
 
   const setSpellCheckLanguage = useCallback(
   const setSpellCheckLanguage = useCallback(
     (spellCheckLanguage: string) => {
     (spellCheckLanguage: string) => {
-      onListen()
-      setSpellCheckLanguageScope(spellCheckLanguage)
+      if (projectScope) {
+        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 =
-      compilerScope && imageNameScope && spellCheckLanguageScope
+    const dataAvailable = !!projectScope
 
 
     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,
   ])
   ])
 }
 }

+ 7 - 13
services/web/frontend/js/features/editor-left-menu/hooks/use-set-project-wide-settings.tsx

@@ -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 = {
-  ignoreUpdates: boolean
-}
-
-export default function useSetProjectWideSettings({
-  ignoreUpdates,
-}: UseSetProjectWideSettings) {
+export default function 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 setSpellCheckLanguage = useSetSpellCheckLanguage({ ignoreUpdates })
+  const setRootDocId = useSetRootDocId()
+  const setSpellCheckLanguage = useSetSpellCheckLanguage()
 
 
   return {
   return {
     compiler: project?.compiler,
     compiler: project?.compiler,

+ 2 - 13
services/web/frontend/js/features/editor-left-menu/hooks/use-set-root-doc-id.tsx

@@ -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 = {
-  ignoreUpdates: boolean
-}
-
-export default function useSetRootDocId({ ignoreUpdates }: UseSetRootDocId) {
+export default function 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,
-      ignoreUpdates,
-    ]
+    [permissionsLevel, projectId, rootDocIdScope, setRootDocIdScope]
   )
   )
   return setRootDocId
   return setRootDocId
 }
 }

+ 3 - 16
services/web/frontend/js/features/editor-left-menu/hooks/use-set-spell-check-language.tsx

@@ -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 = {
-  ignoreUpdates: boolean
-}
-
-export default function useSetSpellCheckLanguage({
-  ignoreUpdates,
-}: UseSetSpellCheckLanguage) {
+export default function 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,
-      ignoreUpdates,
-    ]
+    [projectId, setSpellCheckLanguageScope, spellCheckLanguageScope]
   )
   )
 
 
   return setSpellCheckLanguage
   return setSpellCheckLanguage