-
Notifications
You must be signed in to change notification settings - Fork 24
Menu side bar for Challenges #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phipsae
wants to merge
6
commits into
main
Choose a base branch
from
menu-side-bar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6638e79
add sidebar
phipsae b5dd48f
different placement of burger
phipsae c406df9
fix rinats comments
6419746
fix menu button alignment
phipsae 32d1b9b
move menu content all the way up
phipsae 797b14f
Merge branch 'main' into menu-side-bar
carletex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
packages/nextjs/app/challenge/[challengeId]/_components/ChallengeSidebar.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useState } from "react"; | ||
| import { XMarkIcon } from "@heroicons/react/24/outline"; | ||
| import { useSidebar } from "~~/contexts/SidebarContext"; | ||
|
|
||
| export type Heading = { | ||
| id: string; | ||
| text: string; | ||
| }; | ||
|
|
||
| type ChallengeSidebarProps = { | ||
| headings: Heading[]; | ||
| }; | ||
|
|
||
| export function ChallengeSidebar({ headings }: ChallengeSidebarProps) { | ||
| const [activeId, setActiveId] = useState<string>(""); | ||
| const sidebar = useSidebar(); | ||
| const isOpen = sidebar?.isOpen ?? false; | ||
|
|
||
| const setIsOpen = (open: boolean) => { | ||
| sidebar?.setIsOpen(open); | ||
| }; | ||
|
|
||
| // Register sidebar on mount, unregister on unmount | ||
| useEffect(() => { | ||
| sidebar?.setHasSidebar(true); | ||
| return () => { | ||
| sidebar?.setHasSidebar(false); | ||
| }; | ||
| }, [sidebar]); | ||
|
|
||
| useEffect(() => { | ||
| const observer = new IntersectionObserver( | ||
| entries => { | ||
| const visibleEntries = entries.filter(entry => entry.isIntersecting); | ||
| if (visibleEntries.length > 0) { | ||
| // Sort by their position in the document and take the topmost one | ||
| const topEntry = visibleEntries.reduce((prev, curr) => { | ||
| return prev.boundingClientRect.top < curr.boundingClientRect.top ? prev : curr; | ||
| }); | ||
| setActiveId(topEntry.target.id); | ||
| } | ||
| }, | ||
| { | ||
| rootMargin: "-80px 0px -70% 0px", | ||
| threshold: 0, | ||
| }, | ||
| ); | ||
|
|
||
| headings.forEach(heading => { | ||
| const element = document.getElementById(heading.id); | ||
| if (element) { | ||
| observer.observe(element); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| observer.disconnect(); | ||
| }; | ||
| }, [headings]); | ||
|
|
||
| const handleClick = (id: string) => { | ||
| const element = document.getElementById(id); | ||
| if (element) { | ||
| element.scrollIntoView({ behavior: "smooth" }); | ||
| setActiveId(id); | ||
| setIsOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| if (headings.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {/* Mobile overlay */} | ||
| {isOpen && <div className="lg:hidden fixed inset-0 bg-black/50 z-40" onClick={() => setIsOpen(false)} />} | ||
|
|
||
| {/* Sidebar */} | ||
| <nav | ||
| className={` | ||
| fixed left-0 top-0 h-full w-72 pt-4 z-40 | ||
| bg-base-100 border-r border-base-300 overflow-y-auto | ||
| transition-transform duration-300 ease-in-out | ||
| lg:sticky lg:top-0 lg:h-screen lg:w-64 lg:shrink-0 lg:translate-x-0 lg:border-r-0 lg:bg-transparent | ||
| ${isOpen ? "translate-x-0" : "-translate-x-full"} | ||
| `} | ||
| > | ||
| {/* Close button for mobile */} | ||
| <button | ||
| onClick={() => setIsOpen(false)} | ||
| className="lg:hidden absolute top-4 right-4 btn btn-circle btn-sm btn-ghost" | ||
| aria-label="Close navigation menu" | ||
| > | ||
| <XMarkIcon className="w-5 h-5" /> | ||
| </button> | ||
|
|
||
| <div className="p-4"> | ||
| <h3 className="font-semibold text-sm uppercase tracking-wider text-base-content/60 mb-4">On this page</h3> | ||
| <ul className="space-y-1"> | ||
| {headings.map(heading => ( | ||
| <li key={heading.id}> | ||
| <button | ||
| onClick={() => handleClick(heading.id)} | ||
| className={` | ||
| block w-full text-left px-3 py-2 text-sm rounded-lg transition-colors border-l-2 | ||
| hover:bg-primary/20 hover:text-primary | ||
| ${ | ||
| activeId === heading.id | ||
| ? "bg-primary/10 text-primary border-primary" | ||
| : "text-base-content/70 border-transparent" | ||
| } | ||
| `} | ||
| > | ||
| {heading.text} | ||
| </button> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| </nav> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import { createElement } from "react"; | ||
| import type { ComponentPropsWithoutRef } from "react"; | ||
| import type { ComponentPropsWithoutRef, ReactNode } from "react"; | ||
| import { notFound } from "next/navigation"; | ||
| import { ChallengeHeader } from "./_components/ChallengeHeader"; | ||
| import { ChallengeSidebar, Heading } from "./_components/ChallengeSidebar"; | ||
| import { ConnectAndRegisterBanner } from "./_components/ConnectAndRegisterBanner"; | ||
| import { SubmitChallengeButton } from "./_components/SubmitChallengeButton"; | ||
| import { MDXRemote } from "next-mdx-remote/rsc"; | ||
|
|
@@ -18,6 +19,27 @@ import { fetchGithubChallengeReadme, parseGithubUrl, splitChallengeReadme } from | |
| import { CHALLENGE_METADATA } from "~~/utils/challenges"; | ||
| import { getMetadata } from "~~/utils/scaffold-eth/getMetadata"; | ||
|
|
||
| function generateHeadingId(text: string): string { | ||
| return text | ||
| .toLowerCase() | ||
| .replace(/[\u{1F300}-\u{1F9FF}]/gu, "") | ||
| .replace(/[^\w\s-]/g, "") | ||
| .trim() | ||
| .replace(/\s+/g, "-"); | ||
| } | ||
|
|
||
| function extractHeadings(markdown: string): Heading[] { | ||
| const h2Regex = /^##\s+(.+)$/gm; | ||
| const headings: Heading[] = []; | ||
| let match; | ||
| while ((match = h2Regex.exec(markdown)) !== null) { | ||
| const text = match[1]; | ||
| const id = generateHeadingId(text); | ||
| headings.push({ id, text }); | ||
| } | ||
| return headings; | ||
| } | ||
|
|
||
|
Comment on lines
+22
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can go into |
||
| export async function generateStaticParams() { | ||
| const challenges = await getAllChallenges(); | ||
|
|
||
|
|
@@ -58,90 +80,107 @@ export default async function ChallengePage(props: { params: Promise<{ challenge | |
| const { headerImageMdx, restMdx } = splitChallengeReadme(challengeReadme); | ||
| const { owner, repo, branch } = parseGithubUrl(challenge.github); | ||
|
|
||
| // Extract headings for the sidebar navigation | ||
| const headings = extractHeadings(restMdx); | ||
|
|
||
| // Custom h2 component that adds IDs for anchor navigation | ||
| const createH2WithId = ({ children, ...props }: { children?: ReactNode }) => { | ||
| const text = String(children); | ||
| const id = generateHeadingId(text); | ||
| return createElement("h2", { ...props, id, style: { scrollMarginTop: "80px" } }, children); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col items-center py-8 px-5 xl:p-12 relative max-w-[100vw]"> | ||
| {challengeReadme ? ( | ||
| <> | ||
| <div className="prose dark:prose-invert max-w-fit break-words lg:max-w-[850px]"> | ||
| <MDXRemote | ||
| source={headerImageMdx} | ||
| options={{ | ||
| mdxOptions: { | ||
| rehypePlugins: [rehypeRaw], | ||
| remarkPlugins: [remarkGfm], | ||
| format: "md", | ||
| }, | ||
| }} | ||
| /> | ||
| </div> | ||
| <ChallengeHeader | ||
| skills={staticMetadata?.skills} | ||
| skillLevel={staticMetadata?.skillLevel} | ||
| timeToComplete={staticMetadata?.timeToComplete} | ||
| helpfulLinks={staticMetadata?.helpfulLinks} | ||
| completedByCount={countOfCompletedChallenge} | ||
| /> | ||
| <div className="prose dark:prose-invert max-w-fit break-words lg:max-w-[850px]"> | ||
| <MDXRemote | ||
| source={restMdx} | ||
| components={{ | ||
| a: (props: ComponentPropsWithoutRef<"a">) => | ||
| createElement("a", { ...props, target: "_blank", rel: "noopener" }), | ||
| }} | ||
| options={{ | ||
| mdxOptions: { | ||
| rehypePlugins: [rehypeRaw], | ||
| remarkPlugins: [remarkGfm], | ||
| format: "md", | ||
| }, | ||
| }} | ||
| /> | ||
| </div> | ||
| <div className="flex relative max-w-[100vw]"> | ||
| {/* Sidebar Navigation */} | ||
| <ChallengeSidebar headings={headings} /> | ||
|
|
||
| <a | ||
| href={`https://github.com/${owner}/${repo}/tree/${branch}`} | ||
| className="block mt-2" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| <button className="btn btn-outline btn-sm sm:btn-md"> | ||
| <span className="text-xs sm:text-sm">View on GitHub</span> | ||
| <ArrowTopRightOnSquareIcon className="w-3 h-3 sm:w-4 sm:h-4" /> | ||
| </button> | ||
| </a> | ||
| {guides && guides.length > 0 && ( | ||
| <div className="max-w-[850px] w-full mx-auto"> | ||
| <div className="mt-16 mb-4 font-semibold text-left">Related guides</div> | ||
| <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mt-2 mb-2"> | ||
| {guides.map(guide => ( | ||
| <div key={guide.url} className="p-4 border rounded bg-base-300"> | ||
| <a href={guide.url} className="text-primary underline font-semibold"> | ||
| {guide.title} | ||
| </a> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| {/* Main Content */} | ||
| <div className="flex-1 flex flex-col items-center py-8 px-5 xl:p-12"> | ||
| {challengeReadme ? ( | ||
| <> | ||
| <div className="prose dark:prose-invert max-w-fit break-words lg:max-w-[850px]"> | ||
| <MDXRemote | ||
| source={headerImageMdx} | ||
| options={{ | ||
| mdxOptions: { | ||
| rehypePlugins: [rehypeRaw], | ||
| remarkPlugins: [remarkGfm], | ||
| format: "md", | ||
| }, | ||
| }} | ||
| /> | ||
| </div> | ||
| <ChallengeHeader | ||
| skills={staticMetadata?.skills} | ||
| skillLevel={staticMetadata?.skillLevel} | ||
| timeToComplete={staticMetadata?.timeToComplete} | ||
| helpfulLinks={staticMetadata?.helpfulLinks} | ||
| completedByCount={countOfCompletedChallenge} | ||
| /> | ||
| <div className="prose dark:prose-invert max-w-fit break-words lg:max-w-[850px]"> | ||
| <MDXRemote | ||
| source={restMdx} | ||
| components={{ | ||
| a: (props: ComponentPropsWithoutRef<"a">) => | ||
| createElement("a", { ...props, target: "_blank", rel: "noopener" }), | ||
| h2: createH2WithId, | ||
| }} | ||
| options={{ | ||
| mdxOptions: { | ||
| rehypePlugins: [rehypeRaw], | ||
| remarkPlugins: [remarkGfm], | ||
| format: "md", | ||
| }, | ||
| }} | ||
| /> | ||
| </div> | ||
| )} | ||
| </> | ||
| ) : ( | ||
| <div>Failed to load challenge content</div> | ||
| )} | ||
| {challenge.autograding && ( | ||
| <> | ||
| <ConnectAndRegisterBanner /> | ||
| <SubmitChallengeButton challengeId={challenge.id} /> | ||
| </> | ||
| )} | ||
| {challenge.externalLink && ( | ||
| <div className="fixed bottom-8 inset-x-0 mx-auto w-fit"> | ||
| <button className="btn btn-sm sm:btn-md btn-primary text-secondary px-3 sm:px-4 mt-2 text-xs sm:text-sm"> | ||
| <a href={challenge.externalLink.link} target="_blank" rel="noopener noreferrer"> | ||
| {challenge.externalLink.claim} | ||
|
|
||
| <a | ||
| href={`https://github.com/${owner}/${repo}/tree/${branch}`} | ||
| className="block mt-2" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| <button className="btn btn-outline btn-sm sm:btn-md"> | ||
| <span className="text-xs sm:text-sm">View on GitHub</span> | ||
| <ArrowTopRightOnSquareIcon className="w-3 h-3 sm:w-4 sm:h-4" /> | ||
| </button> | ||
| </a> | ||
| </button> | ||
| </div> | ||
| )} | ||
| {guides && guides.length > 0 && ( | ||
| <div className="max-w-[850px] w-full mx-auto"> | ||
| <div className="mt-16 mb-4 font-semibold text-left">Related guides</div> | ||
| <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mt-2 mb-2"> | ||
| {guides.map(guide => ( | ||
| <div key={guide.url} className="p-4 border rounded bg-base-300"> | ||
| <a href={guide.url} className="text-primary underline font-semibold"> | ||
| {guide.title} | ||
| </a> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| )} | ||
| </> | ||
| ) : ( | ||
| <div>Failed to load challenge content</div> | ||
| )} | ||
| {challenge.autograding && ( | ||
| <> | ||
| <ConnectAndRegisterBanner /> | ||
| <SubmitChallengeButton challengeId={challenge.id} /> | ||
| </> | ||
| )} | ||
| {challenge.externalLink && ( | ||
| <div className="fixed bottom-8 inset-x-0 mx-auto w-fit"> | ||
| <button className="btn btn-sm sm:btn-md btn-primary text-secondary px-3 sm:px-4 mt-2 text-xs sm:text-sm"> | ||
| <a href={challenge.externalLink.link} target="_blank" rel="noopener noreferrer"> | ||
| {challenge.externalLink.claim} | ||
| </a> | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove all of these comments from the PR. At least the ones that don't contribute anything to the understanding of the code.