From 7efe8f5cc3778a8ca6aba2b525963f19916d6722 Mon Sep 17 00:00:00 2001 From: sundasnoreen12 <72802712+sundasnoreen12@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:59:53 +0500 Subject: [PATCH] chore: make sidebar less intrusive for mobile (#1423) * chore: make sidebar less intrusive for mobile * fix: fixed first time view issue * refactor: refactor code --- .../new-sidebar/SidebarContextProvider.jsx | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/courseware/course/new-sidebar/SidebarContextProvider.jsx b/src/courseware/course/new-sidebar/SidebarContextProvider.jsx index 0af0ea2b95..b673c1a422 100644 --- a/src/courseware/course/new-sidebar/SidebarContextProvider.jsx +++ b/src/courseware/course/new-sidebar/SidebarContextProvider.jsx @@ -18,11 +18,20 @@ const SidebarProvider = ({ unitId, children, }) => { + const { verifiedMode } = useModel('courseHomeMeta', courseId); + const topic = useModel('discussionTopics', unitId); const shouldDisplayFullScreen = useWindowSize().width < breakpoints.large.minWidth; const shouldDisplaySidebarOpen = useWindowSize().width > breakpoints.medium.minWidth; const query = new URLSearchParams(window.location.search); - const initialSidebar = (shouldDisplaySidebarOpen || query.get('sidebar') === 'true') - ? SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID : null; + const isInitiallySidebarOpen = shouldDisplaySidebarOpen || query.get('sidebar') === 'true'; + const sidebarKey = `sidebar.${courseId}`; + + let initialSidebar = shouldDisplayFullScreen && sidebarKey in localStorage ? getLocalStorage(sidebarKey) + : SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID; + + if (!shouldDisplayFullScreen && isInitiallySidebarOpen) { + initialSidebar = SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID; + } const [currentSidebar, setCurrentSidebar] = useState(initialSidebar); const [notificationStatus, setNotificationStatus] = useState(getLocalStorage(`notificationStatus.${courseId}`)); const [hideDiscussionbar, setHideDiscussionbar] = useState(false); @@ -30,8 +39,6 @@ const SidebarProvider = ({ const [upgradeNotificationCurrentState, setUpgradeNotificationCurrentState] = useState( getLocalStorage(`upgradeNotificationCurrentState.${courseId}`), ); - const topic = useModel('discussionTopics', unitId); - const { verifiedMode } = useModel('courseHomeMeta', courseId); const isDiscussionbarAvailable = (topic?.id && topic?.enabledInContext) || false; const isNotificationbarAvailable = !isEmpty(verifiedMode); @@ -43,7 +50,9 @@ const SidebarProvider = ({ useEffect(() => { setHideDiscussionbar(!isDiscussionbarAvailable); setHideNotificationbar(!isNotificationbarAvailable); - setCurrentSidebar(SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID); + if (initialSidebar && currentSidebar !== initialSidebar) { + setCurrentSidebar(SIDEBARS.DISCUSSIONS_NOTIFICATIONS.ID); + } }, [unitId, topic]); useEffect(() => { @@ -52,16 +61,35 @@ const SidebarProvider = ({ } }, [hideDiscussionbar, hideNotificationbar]); + const handleWidgetToggle = useCallback((widgetId, sidebarId) => { + setHideDiscussionbar(prevWidgetId => (widgetId === WIDGETS.DISCUSSIONS ? true : prevWidgetId)); + setHideNotificationbar(prevWidgetId => (widgetId === WIDGETS.NOTIFICATIONS ? true : prevWidgetId)); + setLocalStorage(sidebarKey, sidebarId); + }, []); + + const handleSidebarToggle = useCallback((sidebarId) => { + setCurrentSidebar(prevSidebar => (sidebarId === prevSidebar ? null : sidebarId)); + setHideDiscussionbar(!isDiscussionbarAvailable); + setHideNotificationbar(!isNotificationbarAvailable); + setLocalStorage(sidebarKey, sidebarId === currentSidebar ? null : sidebarId); + }, [currentSidebar, isDiscussionbarAvailable, isNotificationbarAvailable]); + + const clearSidebarKeyIfWidgetsUnavailable = useCallback((widgetId) => { + if ((!isNotificationbarAvailable && widgetId === WIDGETS.DISCUSSIONS) + || (!isDiscussionbarAvailable && widgetId === WIDGETS.NOTIFICATIONS)) { + setLocalStorage(sidebarKey, null); + } + }, [isDiscussionbarAvailable, isNotificationbarAvailable]); + const toggleSidebar = useCallback((sidebarId = null, widgetId = null) => { if (widgetId) { - setHideDiscussionbar(prevWidgetId => (widgetId === WIDGETS.DISCUSSIONS ? true : prevWidgetId)); - setHideNotificationbar(prevWidgetId => (widgetId === WIDGETS.NOTIFICATIONS ? true : prevWidgetId)); + handleWidgetToggle(widgetId, sidebarId); } else { - setCurrentSidebar(prevSidebar => (sidebarId === prevSidebar ? null : sidebarId)); - setHideDiscussionbar(!isDiscussionbarAvailable); - setHideNotificationbar(!isNotificationbarAvailable); + handleSidebarToggle(sidebarId); } - }, [isDiscussionbarAvailable, isNotificationbarAvailable]); + + clearSidebarKeyIfWidgetsUnavailable(widgetId); + }, [handleWidgetToggle, handleSidebarToggle, clearSidebarKeyIfWidgetsUnavailable]); const contextValue = useMemo(() => ({ toggleSidebar,