Explorar el Código

manage isDeviceSmallerThanMd with SWR

Yuki Takei hace 4 años
padre
commit
c05cb73151

+ 10 - 69
packages/app/src/client/services/NavigationContainer.js

@@ -24,18 +24,10 @@ export default class NavigationContainer extends Container {
     this.state = {
     this.state = {
       editorMode: 'view',
       editorMode: 'view',
 
 
-      isDeviceSmallerThanMd: null,
-      preferDrawerModeByUser: localStorage.preferDrawerModeByUser === 'true',
-      preferDrawerModeOnEditByUser: // default: true
-        localStorage.preferDrawerModeOnEditByUser == null || localStorage.preferDrawerModeOnEditByUser === 'true',
-      isDrawerMode: null,
-      isDrawerOpened: false,
-
       isScrollTop: true,
       isScrollTop: true,
     };
     };
 
 
     this.setEditorMode = this.setEditorMode.bind(this);
     this.setEditorMode = this.setEditorMode.bind(this);
-    this.initDeviceSize();
     this.initScrollEvent();
     this.initScrollEvent();
   }
   }
 
 
@@ -50,26 +42,6 @@ export default class NavigationContainer extends Container {
     return this.appContainer.getContainer('PageContainer');
     return this.appContainer.getContainer('PageContainer');
   }
   }
 
 
-  initDeviceSize() {
-    const mdOrAvobeHandler = async(mql) => {
-      let isDeviceSmallerThanMd;
-
-      // sm -> md
-      if (mql.matches) {
-        isDeviceSmallerThanMd = false;
-      }
-      // md -> sm
-      else {
-        isDeviceSmallerThanMd = true;
-      }
-
-      this.setState({ isDeviceSmallerThanMd });
-      this.updateDrawerMode({ ...this.state, isDeviceSmallerThanMd }); // generate newest state object
-    };
-
-    this.appContainer.addBreakpointListener('md', mdOrAvobeHandler, true);
-  }
-
   initScrollEvent() {
   initScrollEvent() {
     window.addEventListener('scroll', () => {
     window.addEventListener('scroll', () => {
       const currentYOffset = window.pageYOffset;
       const currentYOffset = window.pageYOffset;
@@ -129,37 +101,6 @@ export default class NavigationContainer extends Container {
     this.updateDrawerMode({ ...this.state, editorMode }); // generate newest state object
     this.updateDrawerMode({ ...this.state, editorMode }); // generate newest state object
   }
   }
 
 
-  toggleDrawer() {
-    const { isDrawerOpened } = this.state;
-    this.setState({ isDrawerOpened: !isDrawerOpened });
-  }
-
-  /**
-   * Set Sidebar mode preference by user
-   * @param {boolean} preferDockMode
-   */
-  async setDrawerModePreference(bool) {
-    this.setState({ preferDrawerModeByUser: bool });
-    this.updateDrawerMode({ ...this.state, preferDrawerModeByUser: bool }); // generate newest state object
-
-    // store settings to localStorage
-    const { localStorage } = window;
-    localStorage.preferDrawerModeByUser = bool;
-  }
-
-  /**
-   * Set Sidebar mode preference by user
-   * @param {boolean} preferDockMode
-   */
-  async setDrawerModePreferenceOnEdit(bool) {
-    this.setState({ preferDrawerModeOnEditByUser: bool });
-    this.updateDrawerMode({ ...this.state, preferDrawerModeOnEditByUser: bool }); // generate newest state object
-
-    // store settings to localStorage
-    const { localStorage } = window;
-    localStorage.preferDrawerModeOnEditByUser = bool;
-  }
-
   /**
   /**
    * Update drawer related state by specified 'newState' object
    * Update drawer related state by specified 'newState' object
    * @param {object} newState A newest state object
    * @param {object} newState A newest state object
@@ -170,19 +111,19 @@ export default class NavigationContainer extends Container {
    *
    *
    * because updating state of unstated container will be delayed unless you use await
    * because updating state of unstated container will be delayed unless you use await
    */
    */
-  updateDrawerMode(newState) {
-    const {
-      editorMode, isDeviceSmallerThanMd, preferDrawerModeByUser, preferDrawerModeOnEditByUser,
-    } = newState;
+  // updateDrawerMode(newState) {
+  //   const {
+  //     editorMode, isDeviceSmallerThanMd, preferDrawerModeByUser, preferDrawerModeOnEditByUser,
+  //   } = newState;
 
 
-    // get preference on view or edit
-    const preferDrawerMode = editorMode !== 'view' ? preferDrawerModeOnEditByUser : preferDrawerModeByUser;
+  //   // get preference on view or edit
+  //   const preferDrawerMode = editorMode !== 'view' ? preferDrawerModeOnEditByUser : preferDrawerModeByUser;
 
 
-    const isDrawerMode = isDeviceSmallerThanMd || preferDrawerMode;
-    const isDrawerOpened = false; // close Drawer anyway
+  //   const isDrawerMode = isDeviceSmallerThanMd || preferDrawerMode;
+  //   const isDrawerOpened = false; // close Drawer anyway
 
 
-    this.setState({ isDrawerMode, isDrawerOpened });
-  }
+  //   this.setState({ isDrawerMode, isDrawerOpened });
+  // }
 
 
   /**
   /**
    * Function that implements the click event for realizing smooth scroll
    * Function that implements the click event for realizing smooth scroll

+ 5 - 6
packages/app/src/components/Navbar/GrowiNavbar.tsx

@@ -5,10 +5,9 @@ import { useTranslation } from 'react-i18next';
 
 
 import { UncontrolledTooltip } from 'reactstrap';
 import { UncontrolledTooltip } from 'reactstrap';
 
 
-import NavigationContainer from '~/client/services/NavigationContainer';
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
 import { IUser } from '~/interfaces/user';
 import { IUser } from '~/interfaces/user';
-import { usePageCreateModalOpened } from '~/stores/ui';
+import { useIsDeviceSmallerThanMd, usePageCreateModalOpened } from '~/stores/ui';
 
 
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import GrowiLogo from '../Icons/GrowiLogo';
 import GrowiLogo from '../Icons/GrowiLogo';
@@ -80,10 +79,11 @@ const Confidential: FC<ConfidentialProps> = memo((props: ConfidentialProps) => {
 
 
 const GrowiNavbar = (props) => {
 const GrowiNavbar = (props) => {
 
 
-  const { appContainer, navigationContainer } = props;
+  const { appContainer } = props;
   const { currentUser } = appContainer;
   const { currentUser } = appContainer;
   const { crowi, isSearchServiceConfigured } = appContainer.config;
   const { crowi, isSearchServiceConfigured } = appContainer.config;
-  const { isDeviceSmallerThanMd } = navigationContainer.state;
+
+  const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
 
 
   return (
   return (
     <>
     <>
@@ -118,12 +118,11 @@ const GrowiNavbar = (props) => {
 /**
 /**
  * Wrapper component for using unstated
  * Wrapper component for using unstated
  */
  */
-const GrowiNavbarWrapper = withUnstatedContainers(GrowiNavbar, [AppContainer, NavigationContainer]);
+const GrowiNavbarWrapper = withUnstatedContainers(GrowiNavbar, [AppContainer]);
 
 
 
 
 GrowiNavbar.propTypes = {
 GrowiNavbar.propTypes = {
   appContainer: PropTypes.instanceOf(AppContainer).isRequired,
   appContainer: PropTypes.instanceOf(AppContainer).isRequired,
-  navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
 };
 };
 
 
 export default GrowiNavbarWrapper;
 export default GrowiNavbarWrapper;

+ 4 - 3
packages/app/src/components/Navbar/GrowiNavbarBottom.jsx

@@ -2,7 +2,7 @@ import React from 'react';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
 import NavigationContainer from '~/client/services/NavigationContainer';
 import NavigationContainer from '~/client/services/NavigationContainer';
-import { usePageCreateModalOpened } from '~/stores/ui';
+import { usePageCreateModalOpened, useIsDeviceSmallerThanMd, useDrawerOpened } from '~/stores/ui';
 
 
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import GlobalSearch from './GlobalSearch';
 import GlobalSearch from './GlobalSearch';
@@ -12,8 +12,9 @@ const GrowiNavbarBottom = (props) => {
   const {
   const {
     navigationContainer,
     navigationContainer,
   } = props;
   } = props;
-  const { isDrawerOpened, isDeviceSmallerThanMd } = navigationContainer.state;
 
 
+  const { data: isDrawerOpened, mutate: mutateDrawerOpened } = useDrawerOpened();
+  const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
   const { mutate: mutatePageCreateModalOpened } = usePageCreateModalOpened();
   const { mutate: mutatePageCreateModalOpened } = usePageCreateModalOpened();
 
 
   const additionalClasses = ['grw-navbar-bottom'];
   const additionalClasses = ['grw-navbar-bottom'];
@@ -39,7 +40,7 @@ const GrowiNavbarBottom = (props) => {
             <a
             <a
               role="button"
               role="button"
               className="nav-link btn-lg"
               className="nav-link btn-lg"
-              onClick={() => navigationContainer.toggleDrawer()}
+              onClick={() => mutateDrawerOpened(true)}
             >
             >
               <i className="icon-menu"></i>
               <i className="icon-menu"></i>
             </a>
             </a>

+ 3 - 2
packages/app/src/components/Navbar/GrowiSubNavigation.jsx

@@ -11,7 +11,7 @@ import { withUnstatedContainers } from '../UnstatedUtils';
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
 import NavigationContainer from '~/client/services/NavigationContainer';
 import NavigationContainer from '~/client/services/NavigationContainer';
 import PageContainer from '~/client/services/PageContainer';
 import PageContainer from '~/client/services/PageContainer';
-import { useDrawerMode } from '~/stores/ui';
+import { useDrawerMode, useIsDeviceSmallerThanMd } from '~/stores/ui';
 
 
 import CopyDropdown from '../Page/CopyDropdown';
 import CopyDropdown from '../Page/CopyDropdown';
 import TagLabels from '../Page/TagLabels';
 import TagLabels from '../Page/TagLabels';
@@ -68,12 +68,13 @@ const PagePathNav = ({
 };
 };
 
 
 const GrowiSubNavigation = (props) => {
 const GrowiSubNavigation = (props) => {
+  const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
   const { data: isDrawerMode } = useDrawerMode();
   const { data: isDrawerMode } = useDrawerMode();
 
 
   const {
   const {
     appContainer, navigationContainer, pageContainer, isCompactMode,
     appContainer, navigationContainer, pageContainer, isCompactMode,
   } = props;
   } = props;
-  const { editorMode, isDeviceSmallerThanMd } = navigationContainer.state;
+  const { editorMode } = navigationContainer.state;
   const {
   const {
     pageId, path, createdAt, creator, updatedAt, revisionAuthor, isPageExist,
     pageId, path, createdAt, creator, updatedAt, revisionAuthor, isPageExist,
   } = pageContainer.state;
   } = pageContainer.state;

+ 6 - 4
packages/app/src/components/Navbar/PageEditorModeManager.jsx

@@ -3,8 +3,10 @@ import PropTypes from 'prop-types';
 import { withTranslation } from 'react-i18next';
 import { withTranslation } from 'react-i18next';
 import { UncontrolledTooltip } from 'reactstrap';
 import { UncontrolledTooltip } from 'reactstrap';
 
 
-import { withUnstatedContainers } from '../UnstatedUtils';
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
+import { useIsDeviceSmallerThanMd } from '~/stores/ui';
+
+import { withUnstatedContainers } from '../UnstatedUtils';
 
 
 /* eslint-disable react/prop-types */
 /* eslint-disable react/prop-types */
 const PageEditorModeButtonWrapper = React.memo(({
 const PageEditorModeButtonWrapper = React.memo(({
@@ -37,9 +39,11 @@ const PageEditorModeButtonWrapper = React.memo(({
 function PageEditorModeManager(props) {
 function PageEditorModeManager(props) {
   const {
   const {
     t, appContainer,
     t, appContainer,
-    editorMode, onPageEditorModeButtonClicked, isBtnDisabled, isDeviceSmallerThanMd,
+    editorMode, onPageEditorModeButtonClicked, isBtnDisabled,
   } = props;
   } = props;
 
 
+  const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
+
   const isAdmin = appContainer.isAdmin;
   const isAdmin = appContainer.isAdmin;
   const isHackmdEnabled = appContainer.config.env.HACKMD_URI != null;
   const isHackmdEnabled = appContainer.config.env.HACKMD_URI != null;
   const showHackmdBtn = isHackmdEnabled || isAdmin;
   const showHackmdBtn = isHackmdEnabled || isAdmin;
@@ -116,12 +120,10 @@ PageEditorModeManager.propTypes = {
   onPageEditorModeButtonClicked: PropTypes.func,
   onPageEditorModeButtonClicked: PropTypes.func,
   isBtnDisabled: PropTypes.bool,
   isBtnDisabled: PropTypes.bool,
   editorMode: PropTypes.string,
   editorMode: PropTypes.string,
-  isDeviceSmallerThanMd: PropTypes.bool,
 };
 };
 
 
 PageEditorModeManager.defaultProps = {
 PageEditorModeManager.defaultProps = {
   isBtnDisabled: false,
   isBtnDisabled: false,
-  isDeviceSmallerThanMd: false,
 };
 };
 
 
 /**
 /**

+ 11 - 2
packages/app/src/components/PageEditor/EditorNavbarBottom.jsx

@@ -6,6 +6,8 @@ import { Collapse, Button } from 'reactstrap';
 import NavigationContainer from '~/client/services/NavigationContainer';
 import NavigationContainer from '~/client/services/NavigationContainer';
 import EditorContainer from '~/client/services/EditorContainer';
 import EditorContainer from '~/client/services/EditorContainer';
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
+import { useDrawerOpened, useIsDeviceSmallerThanMd } from '~/stores/ui';
+
 import SlackNotification from '../SlackNotification';
 import SlackNotification from '../SlackNotification';
 import SlackLogo from '../SlackLogo';
 import SlackLogo from '../SlackLogo';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { withUnstatedContainers } from '../UnstatedUtils';
@@ -24,12 +26,19 @@ const EditorNavbarBottom = (props) => {
   const {
   const {
     navigationContainer,
     navigationContainer,
   } = props;
   } = props;
-  const { editorMode, isDeviceSmallerThanMd } = navigationContainer.state;
+  const { editorMode } = navigationContainer.state;
+
+  const { mutate: mutateDrawerOpened } = useDrawerOpened();
+  const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
 
 
   const additionalClasses = ['grw-editor-navbar-bottom'];
   const additionalClasses = ['grw-editor-navbar-bottom'];
 
 
   const renderDrawerButton = () => (
   const renderDrawerButton = () => (
-    <button type="button" className="btn btn-outline-secondary border-0" onClick={() => navigationContainer.toggleDrawer()}>
+    <button
+      type="button"
+      className="btn btn-outline-secondary border-0"
+      onClick={() => mutateDrawerOpened(true)}
+    >
       <i className="icon-menu"></i>
       <i className="icon-menu"></i>
     </button>
     </button>
   );
   );