From b38a97adf329c78b4a99ff564c4d4c0ab86bc7ef Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Mon, 17 May 2021 09:05:02 -0400 Subject: [PATCH] Studio admin panel --- src/views/studio/studio-admin-panel.jsx | 96 +++++++++++++++++++ src/views/studio/studio.jsx | 2 + src/views/studio/studio.scss | 29 ++++++ .../components/studio-admin-panel.test.jsx | 72 ++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 src/views/studio/studio-admin-panel.jsx create mode 100644 test/unit/components/studio-admin-panel.test.jsx diff --git a/src/views/studio/studio-admin-panel.jsx b/src/views/studio/studio-admin-panel.jsx new file mode 100644 index 000000000..b2358bd2d --- /dev/null +++ b/src/views/studio/studio-admin-panel.jsx @@ -0,0 +1,96 @@ +/* eslint-disable react/jsx-no-bind */ +import React, {useState, useEffect} from 'react'; +import {connect} from 'react-redux'; +import classNames from 'classnames'; + + +import {selectIsAdmin} from '../../redux/session.js'; +import {selectStudioId} from '../../redux/studio.js'; + +import AdminPanel from '../../components/adminpanel/adminpanel.jsx'; + +const adminPanelOpenKey = 'adminPanelToggled_studios'; +const adminPanelOpenClass = 'mod-view-admin-panel-open'; + +/** + * Propagate the admin panel openness to localStorage and set a class name + * on the #view element. + * @param {boolean} value - whether the admin panel is now open. + */ +const storeAdminPanelOpen = value => { + try { + localStorage.setItem(adminPanelOpenKey, value ? 'open' : 'closed'); + } catch (e) { + // eslint-disable-next-line no-console + console.error('Could not set adminPanelToggled_studios in local storage', e); + } + try { + document.querySelector('#view').classList + .toggle(adminPanelOpenClass, value); + } catch (e) { + // eslint-disable-next-line no-console + console.error('Could not set admin-panel-open class on #view'); + } +}; + +/** + * Get the previous stored value of admin panel openness from localStorage. + * @returns {boolean} - whether the admin panel should be open. + */ +const getAdminPanelOpen = () => { + try { + return localStorage.getItem(adminPanelOpenKey) === 'open'; + } catch (_) { + return false; + } +}; + +const StudioAdminPanel = ({studioId, showAdminPanel}) => { + const [adminPanelOpen, setAdminPanelOpen] = useState(getAdminPanelOpen()); + + useEffect(() => { + storeAdminPanelOpen(adminPanelOpen); + }, [adminPanelOpen]); + + useEffect(() => { + if (!showAdminPanel) return; + const handleMessage = e => { + if (e.data === 'closePanel') setAdminPanelOpen(false); + }; + window.addEventListener('message', handleMessage); + return () => { + window.removeEventListener('message', handleMessage); + }; + }, [showAdminPanel]); + + return showAdminPanel && ( + setAdminPanelOpen(true)} + > +