Connect, collaborate, and grow with a community of TypeScript developers
Challenge collapsible sidebar (#787)
Amanuel Odichoe2e660a44d0f
diff --git a/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/ChallengePlaygroundWrapper.tsx b/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/ChallengePlaygroundWrapper.tsxnew file mode 100644index 00000000..0915022a--- /dev/null+++ b/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/ChallengePlaygroundWrapper.tsx@@ -0,0 +1,17 @@+'use client';++import { type ReactNode } from 'react';+import { ChallengeLayoutWrapper } from '../../challenge/_components/challenge-layout-wrapper';+import { useChallengePlaygroundStore } from './challenge-playground-store';+import type { ChallengeRouteData } from '../../challenge/[id]/getChallengeRouteData';++interface Props {+ children: ReactNode;+}++export function ChallengePlaygroundWrapper({ children }: Props) {+ const { values } = useChallengePlaygroundStore();++ const challenge = values.challenge as ChallengeRouteData;+ return <ChallengeLayoutWrapper challenge={challenge}>{children}</ChallengeLayoutWrapper>;+}diff --git a/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/layout.tsx b/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/layout.tsxindex 39f5975c..9563cb0c 100644--- a/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/layout.tsx+++ b/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/layout.tsx@@ -1,19 +1,18 @@import { ForceRenderUntilClient } from '@repo/ui/components/force-render-until-client';-import { ChallengeLayout } from '../../challenge/_components/challenge-layout';-import { LeftWrapper } from './left-wrapper';-import { Wrapper } from './wrapper';import { getServerAuthSession } from '@repo/auth/server';import { isAdminOrModerator } from '~/utils/auth-guards';import { redirect } from 'next/navigation';+import { ChallengePlaygroundWrapper } from './ChallengePlaygroundWrapper';export default async function LayoutData({ children }: { children: React.ReactNode }) {const session = await getServerAuthSession();if (!isAdminOrModerator(session)) {return redirect('/');}+return (<ForceRenderUntilClient>- <ChallengeLayout left={<LeftWrapper>{children}</LeftWrapper>} right={<Wrapper />} />+ <ChallengePlaygroundWrapper>{children}</ChallengePlaygroundWrapper></ForceRenderUntilClient>);}diff --git a/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/left-wrapper.tsx b/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/left-wrapper.tsxdeleted file mode 100644index 8372c497..00000000--- a/apps/web/src/app/[locale]/(playgrounds)/challenge-playground/left-wrapper.tsx+++ /dev/null@@ -1,11 +0,0 @@-'use client';--import { type ReactNode } from 'react';--interface Props {- children: ReactNode;-}--export function LeftWrapper({ children }: Props) {- return <div className="flex h-full w-full flex-col">{children}</div>;-}diff --git a/apps/web/src/app/[locale]/challenge/[id]/layout.tsx b/apps/web/src/app/[locale]/challenge/[id]/layout.tsxindex cc96d4dc..b0e4f354 100644--- a/apps/web/src/app/[locale]/challenge/[id]/layout.tsx+++ b/apps/web/src/app/[locale]/challenge/[id]/layout.tsx@@ -1,8 +1,6 @@import { getServerAuthSession } from '@repo/auth/server';-import { ChallengeLayout } from '../_components/challenge-layout';+import { ChallengeLayoutWrapper } from '../_components/challenge-layout-wrapper';import { getChallengeRouteData } from './getChallengeRouteData';-import { LeftWrapper } from './left-wrapper';-import { Wrapper } from './wrapper';import { ForceRenderUntilClient } from '@repo/ui/components/force-render-until-client';export default async function LayoutData({@@ -17,10 +15,7 @@ export default async function LayoutData({return (<ForceRenderUntilClient>- <ChallengeLayout- left={<LeftWrapper challengeId={challenge.id}>{children}</LeftWrapper>}- right={<Wrapper challenge={challenge} />}- />+ <ChallengeLayoutWrapper challenge={challenge}>{children}</ChallengeLayoutWrapper></ForceRenderUntilClient>);}diff --git a/apps/web/src/app/[locale]/challenge/[id]/left-wrapper.tsx b/apps/web/src/app/[locale]/challenge/[id]/left-wrapper.tsxindex d5101cf6..8cbde561 100644--- a/apps/web/src/app/[locale]/challenge/[id]/left-wrapper.tsx+++ b/apps/web/src/app/[locale]/challenge/[id]/left-wrapper.tsx@@ -1,18 +1,72 @@'use client';import { Tabs, TabsContent, TabsList, TabsTrigger } from '@repo/ui/components/tabs';+import { FlaskConical, History, Text } from '@repo/ui/icons';import { usePathname, useRouter } from 'next/navigation';-import { useMemo, type ReactNode } from 'react';+import { useMemo, type ReactNode, useRef, useState, useEffect } from 'react';type Tab = 'description' | 'solutions' | 'submissions';interface Props {children: ReactNode;challengeId: number;+ expandPanel: () => void;+ isDesktop: boolean;}-export function LeftWrapper({ challengeId, children }: Props) {+export function LeftWrapper({ challengeId, children, expandPanel, isDesktop }: Props) {const pathname = usePathname();const router = useRouter();+ const [isCollapsed, setIsCollapsed] = useState(false);++ const isCollapsedRef = useRef(isCollapsed);+ const isDesktopRef = useRef(isDesktop);+ isDesktopRef.current = isDesktop;+ isCollapsedRef.current = isCollapsed;++ const tabsRef = useRef<HTMLDivElement | null>(null);+ const tabsListRef = useRef<HTMLDivElement | null>(null);+ const tabsContentRef = useRef<HTMLDivElement | null>(null);++ useEffect(() => {+ const tabsElement = tabsRef.current;+ const tabsContentElement = tabsContentRef.current;+ const tabsListElement = tabsListRef.current;++ if (!tabsElement || !tabsContentElement || !tabsListElement) return;++ const handleResize = (entries: ResizeObserverEntry[]) => {+ for (const entry of entries) {+ if (entry.target !== tabsElement) continue;+ const isDesktopCollapsed = isDesktopRef.current && entry.contentRect.width <= 60;+ const isMobileCollapsed = !isDesktopRef.current && entry.contentRect.height <= 41;++ if (isDesktopCollapsed || isMobileCollapsed) {+ setIsCollapsed(true);+ tabsContentElement.style.display = 'none';+ if (isDesktopRef.current && !isCollapsedRef.current) {+ tabsListElement.style.display = 'none';+ }+ } else {+ tabsContentElement.style.display = '';+ setIsCollapsed(false);+ }+ }+ };++ const resizeObserver = new ResizeObserver(handleResize);+ resizeObserver.observe(tabsElement);++ return () => {+ resizeObserver.unobserve(tabsElement);+ };+ }, []);++ useEffect(() => {+ const tabsListElement = tabsListRef.current;+ if (tabsListElement) {+ tabsListElement.style.display = '';+ }+ }, [isCollapsed]);const selectedTab: Tab = useMemo(() => {const splitPath = pathname.split('/');@@ -28,31 +82,55 @@ export function LeftWrapper({ challengeId, children }: Props) {}, [pathname]);return (- <Tabs className="flex h-full w-full flex-col overflow-hidden" defaultValue={selectedTab}>- <TabsList className="bg-background/90 dark:bg-muted/90 sticky top-0 z-10 grid h-auto w-full grid-cols-3 rounded-none rounded-tl-2xl rounded-tr-xl border-b border-zinc-300 backdrop-blur-sm dark:border-zinc-700">+ <Tabs+ ref={tabsRef}+ className="flex h-full w-full flex-col overflow-hidden"+ defaultValue={selectedTab}+ >+ <TabsList+ className={`bg-background/90 dark:bg-muted/90 sticky top-0 z-10 grid h-auto w-full rounded-none rounded-tl-2xl rounded-tr-xl border-b border-zinc-300 backdrop-blur-sm dark:border-zinc-700 ${+ isCollapsed ? (isDesktop ? 'grid-rows-3 gap-2' : 'grid-cols-3') : 'grid-cols-3'+ }`}+ ref={tabsListRef}+ ><TabsTrigger- className="rounded-md rounded-tl-xl duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- onClick={() => router.push(`/challenge/${challengeId}`)}+ className={`rounded-md rounded-tl-xl duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700 ${+ isCollapsed ? (isDesktop ? 'rounded-r-lg py-4' : 'rounded-bl-xl') : ''+ }`}+ onClick={() => {+ router.push(`/challenge/${challengeId}`);+ expandPanel();+ }}value="description">- Description+ {isCollapsed && isDesktop ? <Text className="h-4 w-4" /> : 'Description'}</TabsTrigger><TabsTrigger- className="rounded-md duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- onClick={() => router.push(`/challenge/${challengeId}/solutions`)}+ className={`rounded-md duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700 ${+ isCollapsed && isDesktop ? 'py-4' : ''+ }`}+ onClick={() => {+ router.push(`/challenge/${challengeId}/solutions`);+ expandPanel();+ }}value="solutions">- Solutions+ {isCollapsed && isDesktop ? <FlaskConical className="h-4 w-4" /> : 'Solutions'}</TabsTrigger><TabsTrigger- className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- onClick={() => router.push(`/challenge/${challengeId}/submissions`)}+ className={`rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700 ${+ isCollapsed ? (isDesktop ? 'rounded-md py-4' : 'rounded-br-xl') : ''+ }`}+ onClick={() => {+ router.push(`/challenge/${challengeId}/submissions`);+ expandPanel();+ }}value="submissions">- Submissions+ {isCollapsed && isDesktop ? <History className="h-4 w-4" /> : 'Submissions'}</TabsTrigger></TabsList>- <TabsContent className="mt-0 h-[calc(100%_-_41px)]" value={selectedTab}>+ <TabsContent className="mt-0 h-[calc(100%_-_41px)]" value={selectedTab} ref={tabsContentRef}>{children}</TabsContent></Tabs>diff --git a/apps/web/src/app/[locale]/challenge/_components/challenge-layout-wrapper.tsx b/apps/web/src/app/[locale]/challenge/_components/challenge-layout-wrapper.tsxnew file mode 100644index 00000000..b881429e--- /dev/null+++ b/apps/web/src/app/[locale]/challenge/_components/challenge-layout-wrapper.tsx@@ -0,0 +1,43 @@+'use client';+import { useState, type ReactNode } from 'react';+import type { ChallengeRouteData } from '../[id]/getChallengeRouteData';+import { ChallengeLayout, MOBILE_BREAKPOINT } from './challenge-layout';+import usePanelAdjustments from './usePanelAdjustments';+import { LeftWrapper } from '../[id]/left-wrapper';+import { Wrapper } from '../[id]/wrapper';++export function ChallengeLayoutWrapper({+ challenge,+ children,+}: {+ challenge: ChallengeRouteData;+ children: ReactNode;+}) {+ const [isDesktop, setIsDesktop] = useState(window.innerWidth > MOBILE_BREAKPOINT);+ const LEFT_PANEL_BREAKPOINT = isDesktop ? 500 : 318;+ const DEFAULT_DESKTOP_WIDTH_PX = `${LEFT_PANEL_BREAKPOINT}px`;+ const { leftSide, adjustPanelSize, expandPanel, collapsePanel, isLeftPanelCollapsed } =+ usePanelAdjustments(DEFAULT_DESKTOP_WIDTH_PX, LEFT_PANEL_BREAKPOINT, isDesktop);++ const props = {+ setIsDesktop,+ isDesktop,+ leftSide,+ adjustPanelSize,+ expandPanel,+ collapsePanel,+ isLeftPanelCollapsed,+ };++ return (+ <ChallengeLayout+ left={+ <LeftWrapper challengeId={challenge.id} expandPanel={expandPanel} isDesktop={isDesktop}>+ {children}+ </LeftWrapper>+ }+ right={<Wrapper challenge={challenge} />}+ {...props}+ />+ );+}diff --git a/apps/web/src/app/[locale]/challenge/_components/challenge-layout.tsx b/apps/web/src/app/[locale]/challenge/_components/challenge-layout.tsxindex 1b3ac17f..bcf6d98a 100644--- a/apps/web/src/app/[locale]/challenge/_components/challenge-layout.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/challenge-layout.tsx@@ -1,10 +1,9 @@'use client';-import { useEffect, useRef, type ReactNode, useState, useCallback } from 'react';+import { useEffect, useRef, type ReactNode, useState, type MutableRefObject, useMemo } from 'react';import { create } from 'zustand';import { persist } from 'zustand/middleware';import { useFullscreenSettingsStore } from './fullscreen';-import usePanelAdjustments from './usePanelAdjustments';import { getEventDeltas } from '@repo/monaco/utils';export const DEFAULT_SETTINGS = {@@ -33,30 +32,74 @@ export const useLayoutSettingsStore = create<State>()(export interface ChallengeLayoutProps {left: ReactNode;right: ReactNode;+ setIsDesktop: (bool: boolean) => void;+ isDesktop: boolean;+ leftSide: MutableRefObject<HTMLDivElement | null>;+ collapsePanel: () => void;+ expandPanel: () => void;+ adjustPanelSize: (divideByW: number, divideByH: number, newDimensionValue: number) => void;+ isLeftPanelCollapsed: () => boolean;}-const MOBILE_BREAKPOINT = 1025;--export function ChallengeLayout({ left, right }: ChallengeLayoutProps) {+export const MOBILE_BREAKPOINT = 1025;+export const COLLAPSED_DESKTOP_WIDTH = 60;+export const COLLAPSED_MOBILE_HEIGHT = 41;++export function ChallengeLayout({+ left,+ right,+ setIsDesktop,+ isDesktop,+ leftSide,+ adjustPanelSize,+ collapsePanel,+ expandPanel,+ isLeftPanelCollapsed,+}: ChallengeLayoutProps) {const parent = useRef<HTMLDivElement>(null);const resizer = useRef<HTMLDivElement>(null);const rightSide = useRef<HTMLDivElement>(null);const { settings, updateSettings } = useLayoutSettingsStore();const { fssettings, updateFSSettings } = useFullscreenSettingsStore();- const [isDesktop, setIsDesktop] = useState(window.innerWidth > MOBILE_BREAKPOINT);const [isCollapsed, setIsCollapsed] = useState(false);const LEFT_PANEL_BREAKPOINT = isDesktop ? 500 : 318;const COLLAPSE_BREAKPOINT = isCollapsed ? 50 : 300;const DEFAULT_DESKTOP_WIDTH_PX = `${LEFT_PANEL_BREAKPOINT}px`;- const leftStyle = isDesktop- ? { width: settings.width, minWidth: LEFT_PANEL_BREAKPOINT }- : { height: settings.height, minHeight: LEFT_PANEL_BREAKPOINT };+ const isPanelCollapsed = useMemo(() => {+ const height = parseFloat(settings.height);+ const width = parseFloat(settings.width);++ return height <= COLLAPSED_MOBILE_HEIGHT || width <= COLLAPSED_DESKTOP_WIDTH;+ }, [settings.height, settings.width]);- const { leftSide, adjustPanelSize, expandPanel, collapsePanel, isLeftPanelCollapsed } =- usePanelAdjustments(DEFAULT_DESKTOP_WIDTH_PX, LEFT_PANEL_BREAKPOINT, isDesktop);+ const leftStyle = useMemo(() => {+ const leftStyleIfDesktopCollapsed = {+ width: `${COLLAPSED_DESKTOP_WIDTH}px`,+ minWidth: `${COLLAPSED_DESKTOP_WIDTH}px`,+ };+ const leftStyleIfMobileCollapsed = {+ height: `${COLLAPSED_MOBILE_HEIGHT}px`,+ minHeight: `${COLLAPSED_MOBILE_HEIGHT}px`,+ };++ if (isDesktop) {+ return isPanelCollapsed+ ? leftStyleIfDesktopCollapsed+ : {+ width: settings.width,+ minWidth: `${LEFT_PANEL_BREAKPOINT}px`,+ };+ }+ return isPanelCollapsed+ ? leftStyleIfMobileCollapsed+ : {+ height: settings.height,+ minHeight: `${LEFT_PANEL_BREAKPOINT}px`,+ };+ }, [isDesktop, isPanelCollapsed, settings.height, settings.width, LEFT_PANEL_BREAKPOINT]);useEffect(() => {const ref = resizer.current;@@ -156,18 +199,29 @@ export function ChallengeLayout({ left, right }: ChallengeLayoutProps) {// handle window resizeconst resizeHandler = () => {- setIsDesktop(window.innerWidth > MOBILE_BREAKPOINT);+ const isDesktop = window.innerWidth > MOBILE_BREAKPOINT;+ setIsDesktop(isDesktop);- if (isDesktop) {- leftRef.style.width = settings.width;- leftRef.style.height = 'auto';- } else {- leftRef.style.height = settings.height;- leftRef.style.width = 'auto';+ const leftRef = leftSide.current;+ if (!leftRef) return;++ const height = parseFloat(leftRef.style.height);+ const width = parseFloat(leftRef.style.width);++ if (height <= COLLAPSED_MOBILE_HEIGHT || width <= COLLAPSED_DESKTOP_WIDTH) {+ if (isDesktop) {+ leftRef.style.width = `${COLLAPSED_DESKTOP_WIDTH}px`;+ updateSettings({ width: `${COLLAPSED_DESKTOP_WIDTH}px`, height: '300px' });+ } else {+ leftRef.style.height = `${COLLAPSED_MOBILE_HEIGHT}px`;+ updateSettings({ width: '500px', height: `${COLLAPSED_MOBILE_HEIGHT}px` });+ }}};const handleResizerDoubleClick = () => {+ setIsCollapsed(isLeftPanelCollapsed());+if (!leftSide.current || !rightSide.current) return;const currentSize = isDesktop@@ -176,19 +230,13 @@ export function ChallengeLayout({ left, right }: ChallengeLayoutProps) {if (currentSize < LEFT_PANEL_BREAKPOINT) {expandPanel();- if (isDesktop) {- leftSide.current.style.width = DEFAULT_DESKTOP_WIDTH_PX;- } else {- leftSide.current.style.height = settings.height;- }} else {collapsePanel();- if (isDesktop) {- leftSide.current.style.width = '0px';- } else {- leftSide.current.style.height = '0px';- }}++ isDesktop+ ? updateSettings({ width: `${leftRef.offsetWidth}px`, height: settings.height })+ : updateSettings({ width: settings.width, height: `${leftRef.offsetHeight}px` });};window.addEventListener('resize', resizeHandler);@@ -211,6 +259,7 @@ export function ChallengeLayout({ left, right }: ChallengeLayoutProps) {expandPanel,isCollapsed,isDesktop,+ setIsDesktop,isLeftPanelCollapsed,leftSide,settings,@@ -224,7 +273,7 @@ export function ChallengeLayout({ left, right }: ChallengeLayoutProps) {style={{ height: fssettings.isFullscreen ? '100vh' : 'calc(100vh - 3.5rem)' }}><div- className="min-h-[318px] w-full overflow-hidden rounded-l-2xl rounded-r-xl border border-zinc-300 bg-white dark:border-zinc-700 dark:bg-zinc-800"+ className="w-full overflow-hidden rounded-l-2xl rounded-r-xl border border-zinc-300 bg-white dark:border-zinc-700 dark:bg-zinc-800"ref={leftSide}style={{ ...leftStyle }}>diff --git a/apps/web/src/app/[locale]/challenge/_components/usePanelAdjustments.ts b/apps/web/src/app/[locale]/challenge/_components/usePanelAdjustments.tsindex 055a1268..8879ca3c 100644--- a/apps/web/src/app/[locale]/challenge/_components/usePanelAdjustments.ts+++ b/apps/web/src/app/[locale]/challenge/_components/usePanelAdjustments.ts@@ -1,4 +1,5 @@import { useCallback, useRef } from 'react';+import { COLLAPSED_DESKTOP_WIDTH, COLLAPSED_MOBILE_HEIGHT } from './challenge-layout';const usePanelAdjustments = (DEFAULT_DESKTOP_WIDTH_PX: string,@@ -23,13 +24,12 @@ const usePanelAdjustments = (const collapsePanel = useCallback(() => {if (!leftSide.current) return;if (isDesktop) {- leftSide.current.style.width = '0px';- leftSide.current.style.minWidth = '0px';+ leftSide.current.style.width = `${COLLAPSED_DESKTOP_WIDTH}px`;+ leftSide.current.style.minWidth = `${COLLAPSED_DESKTOP_WIDTH}px`;} else {- leftSide.current.style.height = '0px';- leftSide.current.style.minHeight = '0px';+ leftSide.current.style.height = `${COLLAPSED_MOBILE_HEIGHT}px`;+ leftSide.current.style.minHeight = `${COLLAPSED_MOBILE_HEIGHT}px`;}- leftSide.current.style.opacity = '0%';}, [isDesktop]);const isLeftPanelCollapsed = useCallback(() => {diff --git a/packages/ui/src/icons.ts b/packages/ui/src/icons.tsindex 43b95e0e..71e5b872 100644--- a/packages/ui/src/icons.ts+++ b/packages/ui/src/icons.ts@@ -62,4 +62,7 @@ export {X,XCircle,Trash,+ Text,+ FlaskConical,+ History,} from 'lucide-react';