Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 4 additions & 69 deletions src/app/exercise/[id]/ExercisePageClient.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { use, useEffect, useRef, useState } from 'react';

Check warning on line 3 in src/app/exercise/[id]/ExercisePageClient.tsx

View workflow job for this annotation

GitHub Actions / Lint

'useRef' is defined but never used. Allowed unused vars must match /^_/u
import { usePathname, useRouter } from 'next/navigation';
import { useExerciseContext } from '@/state/ExerciseContext';
import { getExercise, getAllExercises } from '@/exercises/registry';
Expand Down Expand Up @@ -150,10 +150,6 @@
const pathname = usePathname();
const [isMobile, setIsMobile] = useState(false);
const [activeMobileTab, setActiveMobileTab] = useState<'source' | 'viz' | 'log' | 'misc'>('source');
const mobileShellRef = useRef<HTMLDivElement | null>(null);
const mobileWorkspacePanelRef = useRef<HTMLDivElement | null>(null);
const mobileDirectionsRef = useRef<HTMLDivElement | null>(null);
const mobileBottomDockRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (typeof window === 'undefined') return;
Expand Down Expand Up @@ -209,67 +205,6 @@
setActiveMobileTab('source');
}, [id]);

useEffect(() => {
if (!isMobile) {
mobileShellRef.current?.style.removeProperty('--mobile-workspace-panel-height');
return;
}

const shellElement = mobileShellRef.current;
const panelElement = mobileWorkspacePanelRef.current;
const directionsElement = mobileDirectionsRef.current;
const bottomDockElement = mobileBottomDockRef.current;

if (!shellElement || !panelElement || !directionsElement || !bottomDockElement) {
return;
}

let frameId = 0;

const syncPanelHeight = () => {
frameId = 0;
const shellRect = shellElement.getBoundingClientRect();
const panelRect = panelElement.getBoundingClientRect();
const bottomDockRect = bottomDockElement.getBoundingClientRect();
const shellStyles = window.getComputedStyle(shellElement);
const shellGap = parseFloat(shellStyles.rowGap || shellStyles.gap || '0') || 0;
const availableHeight = Math.floor(bottomDockRect.top - panelRect.top - shellGap);
const maxAvailableHeight = Math.floor(shellRect.bottom - panelRect.top);
const nextHeight = Math.max(0, Math.min(availableHeight, maxAvailableHeight));
shellElement.style.setProperty('--mobile-workspace-panel-height', `${nextHeight}px`);
};

const queueSyncPanelHeight = () => {
if (frameId !== 0) return;
frameId = window.requestAnimationFrame(syncPanelHeight);
};

queueSyncPanelHeight();

const observer = new ResizeObserver(() => {
queueSyncPanelHeight();
});

observer.observe(shellElement);
observer.observe(directionsElement);
observer.observe(bottomDockElement);

window.addEventListener('resize', queueSyncPanelHeight);
window.visualViewport?.addEventListener('resize', queueSyncPanelHeight);
window.visualViewport?.addEventListener('scroll', queueSyncPanelHeight);

return () => {
observer.disconnect();
window.removeEventListener('resize', queueSyncPanelHeight);
window.visualViewport?.removeEventListener('resize', queueSyncPanelHeight);
window.visualViewport?.removeEventListener('scroll', queueSyncPanelHeight);
if (frameId !== 0) {
window.cancelAnimationFrame(frameId);
}
shellElement.style.removeProperty('--mobile-workspace-panel-height');
};
}, [activeMobileTab, isMobile]);

useEffect(() => {
const mainElement = document.querySelector('#app-body > main');
const appElement = document.getElementById('app');
Expand Down Expand Up @@ -512,8 +447,8 @@

if (isMobile) {
return (
<div className="mobile-exercise-shell" ref={mobileShellRef}>
<div ref={mobileDirectionsRef}>
<div className="mobile-exercise-shell">
<div>
<ExerciseDirectionsPanel />
</div>

Expand Down Expand Up @@ -557,7 +492,7 @@
</button>
</div>

<div className="mobile-workspace-panel" ref={mobileWorkspacePanelRef}>
<div className="mobile-workspace-panel">
{activeMobileTab === 'source' && <SourcePanel showDescription={false} />}
{activeMobileTab === 'viz' && (
<ErrorBoundary>
Expand All @@ -576,7 +511,7 @@
</div>
</section>

<div className="mobile-bottom-dock" ref={mobileBottomDockRef}>
<div className="mobile-bottom-dock">
<ErrorBoundary>
<InputPanel showToolkit={false} />
</ErrorBoundary>
Expand Down
4 changes: 1 addition & 3 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -1021,10 +1021,8 @@ button.link-button:focus-visible,

.mobile-workspace-panel {
display: flex;
flex: 0 0 auto;
flex: 1 1 0%;
flex-direction: column;
height: var(--mobile-workspace-panel-height, auto);
max-height: var(--mobile-workspace-panel-height, none);
min-height: 0;
overflow: hidden;
}
Expand Down
Loading