Procházet zdrojové kódy

Move useScopeValue hook to shared hooks folder and wrap setValue in a function (#5386)

GitOrigin-RevId: b1a6a4e871af3b52fb3d100a83b479c834cb75ca
Alf Eaton před 4 roky
rodič
revize
845b2fbc04

+ 1 - 1
services/web/frontend/js/features/pdf-preview/components/pdf-js-viewer.js

@@ -5,7 +5,7 @@ import { Alert } from 'react-bootstrap'
 import PdfViewerControls from './pdf-viewer-controls'
 import { useProjectContext } from '../../../shared/context/project-context'
 import usePersistedState from '../../../shared/hooks/use-persisted-state'
-import useScopeValue from '../../../shared/context/util/scope-value-hook'
+import useScopeValue from '../../../shared/hooks/use-scope-value'
 import { buildHighlightElement } from '../util/highlights'
 import PDFJSWrapper from '../util/pdf-js-wrapper'
 import withErrorBoundary from '../../../infrastructure/error-boundary'

+ 1 - 1
services/web/frontend/js/features/pdf-preview/components/pdf-viewer.js

@@ -1,4 +1,4 @@
-import useScopeValue from '../../../shared/context/util/scope-value-hook'
+import useScopeValue from '../../../shared/hooks/use-scope-value'
 import { usePdfPreviewContext } from '../contexts/pdf-preview-context'
 import { lazy, memo, useEffect } from 'react'
 

+ 1 - 1
services/web/frontend/js/features/pdf-preview/contexts/pdf-preview-context.js

@@ -7,7 +7,7 @@ import {
   useState,
 } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from '../../../shared/context/util/scope-value-hook'
+import useScopeValue from '../../../shared/hooks/use-scope-value'
 import { useProjectContext } from '../../../shared/context/project-context'
 import usePersistedState from '../../../shared/hooks/use-persisted-state'
 import {

+ 1 - 1
services/web/frontend/js/shared/context/compile-context.js

@@ -1,6 +1,6 @@
 import { createContext, useContext, useMemo } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from './util/scope-value-hook'
+import useScopeValue from '../hooks/use-scope-value'
 
 export const CompileContext = createContext()
 

+ 1 - 1
services/web/frontend/js/shared/context/editor-context.js

@@ -6,7 +6,7 @@ import {
   useMemo,
 } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from './util/scope-value-hook'
+import useScopeValue from '../hooks/use-scope-value'
 import useBrowserWindow from '../hooks/use-browser-window'
 import { useIdeContext } from './ide-context'
 import { useProjectContext } from './project-context'

+ 1 - 1
services/web/frontend/js/shared/context/layout-context.js

@@ -1,6 +1,6 @@
 import { createContext, useContext, useCallback, useMemo } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from './util/scope-value-hook'
+import useScopeValue from '../hooks/use-scope-value'
 import { useIdeContext } from './ide-context'
 
 export const LayoutContext = createContext()

+ 1 - 1
services/web/frontend/js/shared/context/project-context.js

@@ -1,6 +1,6 @@
 import { createContext, useContext } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from './util/scope-value-hook'
+import useScopeValue from '../hooks/use-scope-value'
 
 const ProjectContext = createContext()
 

+ 1 - 1
services/web/frontend/js/shared/context/user-context.js

@@ -1,6 +1,6 @@
 import { createContext, useContext } from 'react'
 import PropTypes from 'prop-types'
-import useScopeValue from './util/scope-value-hook'
+import useScopeValue from '../hooks/use-scope-value'
 
 export const UserContext = createContext()
 

+ 6 - 2
services/web/frontend/js/shared/context/util/scope-value-hook.js → services/web/frontend/js/shared/hooks/use-scope-value.js

@@ -1,7 +1,7 @@
 import { useCallback, useEffect, useState } from 'react'
 import PropTypes from 'prop-types'
 import _ from 'lodash'
-import { useIdeContext } from '../ide-context'
+import { useIdeContext } from '../context/ide-context'
 
 /**
  * Binds a property in an Angular scope making it accessible in a React
@@ -23,7 +23,11 @@ export default function useScopeValue(path, deep = false) {
     return $scope.$watch(
       path,
       newValue => {
-        setValue(deep ? _.cloneDeep(newValue) : newValue)
+        setValue(() => {
+          // NOTE: this is deliberately wrapped in a function,
+          // to avoid calling setValue directly with a value that's a function
+          return deep ? _.cloneDeep(newValue) : newValue
+        })
       },
       deep
     )

+ 1 - 1
services/web/frontend/stories/pdf-js-viewer.stories.js

@@ -5,7 +5,7 @@ import { Button } from 'react-bootstrap'
 import { useCallback } from 'react'
 import { withContextRoot } from './utils/with-context-root'
 import { setupContext } from './fixtures/context'
-import useScopeValue from '../js/shared/context/util/scope-value-hook'
+import useScopeValue from '../js/shared/hooks/use-scope-value'
 
 setupContext()