import React, { useCallback, useEffect, useState } from 'react'; import { AvailableTab, LearnerAccessProps, TabKey } from './Types/types'; import LoadWaitlist from './LoadWaitlistedContent'; import LoadArchivedContent from './LoadArchivedContent'; import LoadBookmarks from './LoadBookmarks'; import LoadUserLearning from './LoadUserLearning'; import LoadCertificates from './LoadCertificates'; import { GlobalTypes, LoadingDots, useContentGroupsQuery } from '@thoughtindustries/content'; import LearnerAccessContext from './Context/context'; import { getAvailableTabs } from './Utilities/utilities'; import { useTranslation } from 'react-i18next'; import DropDownClosed from '../Assets/DropDownClosed'; import DropDownOpen from '../Assets/DropDownOpen'; const localizedTabLabelMapping: { [key in TabKey]: string } = { [TabKey.Current]: 'dashboard.current', [TabKey.Events]: 'dashboard.events', [TabKey.LearningPath]: 'learning-path', [TabKey.Completed]: 'dashboard.completed', [TabKey.Archived]: 'dashboard.archived', [TabKey.Bookmark]: 'dashboard.bookmark', [TabKey.Certificate]: 'dashboard.certificates', [TabKey.Waitlist]: 'dashboard.waitlisted' }; const LearnerAccess = ({ displayExpiredCertificateInformation, query, userHasManagerInterfaceAccess, companyEnableExternalCertificateUploads, companyHasWaitlistingFeature }: LearnerAccessProps): JSX.Element => { const [activeTabKey, setActiveTabKey] = useState(undefined); const [availableTabs, setAvailableTabs] = useState([]); // Initialize button state based on SSR-safe default (desktop view) // Will be updated in useEffect on client const [button, setButton] = useState(false); const [dropDownActive, setDropDownActive] = useState(false); const { data, loading, error, refetch: refetchContentGroups } = useContentGroupsQuery({ variables: { query, includeExpiredCertificates: displayExpiredCertificateInformation } }); // Handle available tabs update in useEffect to avoid state updates during render useEffect(() => { if (data?.UserContentGroups) { const newAvailableTabs = getAvailableTabs( data.UserContentGroups || [], userHasManagerInterfaceAccess, companyEnableExternalCertificateUploads, companyHasWaitlistingFeature ); // reset active tab key if not included in available tabs if (activeTabKey && !newAvailableTabs.find(({ key }) => key === activeTabKey)) { setActiveTabKey(undefined); } else if (!activeTabKey && newAvailableTabs.length) { setActiveTabKey(newAvailableTabs[0].key); } setAvailableTabs(newAvailableTabs); } }, [ data, activeTabKey, userHasManagerInterfaceAccess, companyEnableExternalCertificateUploads, companyHasWaitlistingFeature ]); // update state to display button only on mobile const handleResize = () => { if (typeof window !== 'undefined') { setButton(window.innerWidth < 640); } }; useEffect(() => { // Only run on client side after hydration if (typeof window !== 'undefined') { // Don't set initial state here to avoid hydration mismatch // The component will use the default state (false) for both SSR and initial client render window.addEventListener('resize', handleResize); // Only update state after a small delay to ensure hydration is complete const timer = setTimeout(() => { handleResize(); }, 0); return () => { clearTimeout(timer); window.removeEventListener('resize', handleResize); }; } }, []); const { t } = useTranslation(); const resetActiveTab = useCallback( () => setActiveTabKey(availableTabs.length ? availableTabs[0].key : undefined), [availableTabs] ); if (loading) { return ; } if (error) { return <>{error.message}; } const handleTabSelection = (currentTabKey: TabKey) => { setActiveTabKey(currentTabKey); }; const TabButton = () => { return ( <> {dropDownActive && (
{availableTabs.map(({ key }, index) => { const activeTabClassName = key === activeTabKey ? 'font-bold underline' : ''; return ( <> {/* dropdown menu -> only visible on smaller screens */}
); })}
)} ); }; const dashboardAccessTabs = ( <> {/* dropdown button */} {button ? ( ) : (
    {availableTabs.map(({ key }, index) => { const activeTabClassName = key === activeTabKey ? 'py-3 font-semibold border-b border-blue-500' : ''; return ( <>
  • ); })}
)} ); // tab container const tabContent = () => { switch (activeTabKey) { case TabKey.Current: return ( ); case TabKey.Events: return ( ); case TabKey.LearningPath: return ; case TabKey.Completed: return ( ); case TabKey.Archived: return ; case TabKey.Bookmark: return ; case TabKey.Waitlist: return ; case TabKey.Certificate: return ( ); default: return <>; } }; return ( {/* title */}
Activity
{dashboardAccessTabs} {!button &&
} {tabContent()}
); }; LearnerAccess.displayName = 'LearnerAccessWidget'; export default LearnerAccess;