Skip to content

Commit

Permalink
chore: make sidebar less intrusive for mobile (#1423)
Browse files Browse the repository at this point in the history
* chore: make sidebar less intrusive for mobile

* fix: fixed first time view issue

* refactor: refactor code
  • Loading branch information
sundasnoreen12 authored Jul 24, 2024
1 parent 263fe6d commit 7efe8f5
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/courseware/course/new-sidebar/SidebarContextProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@ 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);
const [hideNotificationbar, setHideNotificationbar] = useState(false);
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);

Expand All @@ -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(() => {
Expand All @@ -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,
Expand Down

0 comments on commit 7efe8f5

Please sign in to comment.