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
22 changes: 11 additions & 11 deletions beta/src/components/MDX/Challenges/Challenges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export function Challenges({children, isRecipes}: ChallengesProps) {
};

const currentChallenge = challenges.find(({id}) => id === activeChallenge);
if (currentChallenge === undefined) {
throw new TypeError('currentChallenge should always exist');
}
const nextChallenge = challenges.find(({order}) => {
if (!currentChallenge) {
return false;
}
return order === currentChallenge.order + 1;
});

Expand All @@ -121,7 +121,7 @@ export function Challenges({children, isRecipes}: ChallengesProps) {
</H2>
{challenges.length > 1 && (
<Navigation
activeChallenge={activeChallenge}
currentChallenge={currentChallenge}
challenges={challenges}
handleChange={handleChallengeChange}
isRecipes={isRecipes}
Expand All @@ -132,16 +132,16 @@ export function Challenges({children, isRecipes}: ChallengesProps) {
<div key={activeChallenge}>
<h3 className="text-xl text-primary dark:text-primary-dark mb-2">
<div className="font-bold block md:inline">
{isRecipes ? 'Recipe' : 'Challenge'} {currentChallenge?.order}{' '}
of {challenges.length}
{isRecipes ? 'Recipe' : 'Challenge'} {currentChallenge.order} of{' '}
{challenges.length}
<span className="text-primary dark:text-primary-dark">: </span>
</div>
{currentChallenge?.name}
{currentChallenge.name}
</h3>
<>{currentChallenge?.content}</>
<>{currentChallenge.content}</>
</div>
<div className="flex justify-between items-center mt-4">
{currentChallenge?.hint ? (
{currentChallenge.hint ? (
<div>
<Button className="mr-2" onClick={toggleHint} active={showHint}>
<IconHint className="mr-1.5" />{' '}
Expand Down Expand Up @@ -187,14 +187,14 @@ export function Challenges({children, isRecipes}: ChallengesProps) {
</Button>
)}
</div>
{showHint && currentChallenge?.hint}
{showHint && currentChallenge.hint}

{showSolution && (
<div className="mt-6">
<h3 className="text-2xl font-bold text-primary dark:text-primary-dark">
Solution
</h3>
{currentChallenge?.solution}
{currentChallenge.solution}
<div className="flex justify-between items-center mt-4">
<Button onClick={() => setShowSolution(false)}>
Close solution
Expand Down
13 changes: 5 additions & 8 deletions beta/src/components/MDX/Challenges/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const debounce = require('debounce');
export function Navigation({
challenges,
handleChange,
activeChallenge,
currentChallenge,
isRecipes,
}: {
challenges: ChallengeContents[];
handleChange: (id: string) => void;
activeChallenge: string;
currentChallenge: ChallengeContents;
isRecipes?: boolean;
}) {
const containerRef = React.useRef<HTMLDivElement>(null);
const challengesNavRef = React.useRef(
challenges.map(() => createRef<HTMLButtonElement>())
);
const [scrollPos, setScrollPos] = React.useState(0);
const scrollPos = currentChallenge.order - 1;
const canScrollLeft = scrollPos > 0;
const canScrollRight = scrollPos < challenges.length - 1;

Expand All @@ -37,7 +37,6 @@ export function Navigation({
containerRef.current.scrollLeft = currentNavRef.offsetLeft;
}
handleChange(challenges[scrollPos + 1].id);
setScrollPos(scrollPos + 1);
}
};

Expand All @@ -51,7 +50,6 @@ export function Navigation({
containerRef.current.scrollLeft = currentNavRef.offsetLeft;
}
handleChange(challenges[scrollPos - 1].id);
setScrollPos(scrollPos - 1);
}
};

Expand All @@ -64,7 +62,6 @@ export function Navigation({
containerRef.current.scrollLeft = currentNavRef?.offsetLeft || 0;
}
handleChange(id);
setScrollPos(selectedChallenge);
};

const handleResize = React.useCallback(() => {
Expand Down Expand Up @@ -94,10 +91,10 @@ export function Navigation({
className={cn(
'py-2 mr-4 text-base border-b-4 duration-100 ease-in transition whitespace-nowrap overflow-ellipsis',
isRecipes &&
activeChallenge === id &&
currentChallenge.id === id &&
'text-purple-50 border-purple-50 hover:text-purple-50 dark:text-purple-30 dark:border-purple-30 dark:hover:text-purple-30',
!isRecipes &&
activeChallenge === id &&
currentChallenge.id === id &&
'text-link border-link hover:text-link dark:text-link-dark dark:border-link-dark dark:hover:text-link-dark'
)}
onClick={() => handleSelectNav(id)}
Expand Down