Connect, collaborate, and grow with a community of TypeScript developers
fix: comments ui (#1113)
Fahad30b9cb54edfd
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/layout.tsx b/apps/web/src/app/[locale]/challenge/[slug]/layout.tsxindex ba4e3597..6d1b483b 100644--- a/apps/web/src/app/[locale]/challenge/[slug]/layout.tsx+++ b/apps/web/src/app/[locale]/challenge/[slug]/layout.tsx@@ -4,6 +4,7 @@ import { auth } from '@repo/auth/server';import { ForceRenderUntilClient } from '@repo/ui/components/force-render-until-client';import { getChallengeRouteData } from './getChallengeRouteData';+import { TrackVisibiltyProvider } from './use-track-visibility.hook';export default async function LayoutData({children,@@ -17,9 +18,11 @@ export default async function LayoutData({return (<ForceRenderUntilClient>- <ChallengeLayoutWrapper challenge={challenge} track={track}>- {children}- </ChallengeLayoutWrapper>+ <TrackVisibiltyProvider>+ <ChallengeLayoutWrapper challenge={challenge} track={track}>+ {children}+ </ChallengeLayoutWrapper>+ </TrackVisibiltyProvider></ForceRenderUntilClient>);}diff --git a/apps/web/src/app/[locale]/challenge/[slug]/left-wrapper.tsx b/apps/web/src/app/[locale]/challenge/[slug]/left-wrapper.tsxindex 013befd5..f346cd46 100644--- a/apps/web/src/app/[locale]/challenge/[slug]/left-wrapper.tsx+++ b/apps/web/src/app/[locale]/challenge/[slug]/left-wrapper.tsx@@ -11,6 +11,7 @@ import { cn } from '@repo/ui/cn';import { FlaskConical, History, Text } from '@repo/ui/icons';import type { ChallengeRouteData } from './getChallengeRouteData';+import { useTrackNavigationVisiblity } from './use-track-visibility.hook';type Tab = 'description' | 'solutions' | 'submissions';interface Props {@@ -98,6 +99,11 @@ export function LeftWrapper({ children, challenge, track, expandPanel, isDesktopisTrackFeatureEnabled && hasEnrolledTrackForChallenge && (!isCollapsed || isDesktop);const isIconOnly = isCollapsed && isDesktop;+ const { setIsTrackTitleVisible } = useTrackNavigationVisiblity();++ useEffect(() => {+ setIsTrackTitleVisible(Boolean(isTrackVisible));+ }, [isTrackVisible, setIsTrackTitleVisible]);return (<div className="flex h-full w-full flex-col">diff --git a/apps/web/src/app/[locale]/challenge/[slug]/use-track-visibility.hook.tsx b/apps/web/src/app/[locale]/challenge/[slug]/use-track-visibility.hook.tsxnew file mode 100644index 00000000..22c0d998--- /dev/null+++ b/apps/web/src/app/[locale]/challenge/[slug]/use-track-visibility.hook.tsx@@ -0,0 +1,27 @@+'use client';+import { createContext, useState, useContext, type PropsWithChildren } from 'react';++interface TrackVisibilityContextType {+ isTrackTitleVisible: boolean;+ setIsTrackTitleVisible: React.Dispatch<React.SetStateAction<boolean>>;+}++export const TrackVisibilityContext = createContext({} as TrackVisibilityContextType);++export const TrackVisibiltyProvider = ({ children }: PropsWithChildren) => {+ const [isTrackTitleVisible, setIsTrackTitleVisible] = useState<boolean>(false);++ return (+ <TrackVisibilityContext.Provider value={{ isTrackTitleVisible, setIsTrackTitleVisible }}>+ {children}+ </TrackVisibilityContext.Provider>+ );+};++export const useTrackNavigationVisiblity = () => {+ const context = useContext(TrackVisibilityContext);+ if (context === undefined) {+ throw new Error('use useTrackVisiblity hook within TrackVisibiltyProvider');+ }+ return context;+};diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/index.tsxindex 7dffe09e..b6b15293 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx@@ -13,6 +13,7 @@ import { Pagination } from '../pagination';import { SortSelect } from '../sort-select';import { useComments } from './comments.hooks';import { sortKeys } from './comments.constants';+import { useTrackNavigationVisiblity } from '../../[slug]/use-track-visibility.hook';interface Props {preselectedCommentMetadata?: PreselectedCommentMetadata;@@ -23,6 +24,7 @@ interface Props {// million-ignoreexport function Comments({ preselectedCommentMetadata, rootId, type, expanded = false }: Props) {+ const { isTrackTitleVisible } = useTrackNavigationVisiblity();const [showComments, setShowComments] = useState(expanded);const commentContainerRef = useRef<HTMLDivElement>(null);const {@@ -62,6 +64,7 @@ export function Comments({ preselectedCommentMetadata, rootId, type, expanded =className="flex w-full items-center justify-between gap-2 p-3 font-medium text-neutral-500 duration-300 hover:text-neutral-700 focus:outline-none dark:hover:text-zinc-300"onClick={() => {setShowComments(!showComments);+ commentContainerRef.current?.scroll({ top: 0 });}}><div className="flex items-center gap-2">@@ -79,9 +82,11 @@ export function Comments({ preselectedCommentMetadata, rootId, type, expanded =className={clsx('custom-scrollable-element flex flex-col overscroll-contain duration-300',{- 'h-64 pb-4 md:h-[calc(100vh_-_164px)]': showComments,'h-0 overflow-y-hidden': !showComments,- 'overflow-y-auto': showComments && (data?.comments.length ?? 0) > 0,+ 'h-64 pb-4 lg:h-[calc(100vh_-_164px)]': showComments && !isTrackTitleVisible, // no title+ 'h-[calc(256px_-_46px)] pb-4 lg:h-[calc(100vh_-_210px)]':+ showComments && isTrackTitleVisible, // with title+ 'overflow-y-auto ': showComments && (data?.comments.length ?? 0) > 0,},)}ref={commentContainerRef}