-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathChallenges.tsx
161 lines (149 loc) · 4.4 KB
/
Challenges.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import {Children, useRef, useEffect, useState} from 'react';
import * as React from 'react';
import cn from 'classnames';
import {H2} from 'components/MDX/Heading';
import {H4} from 'components/MDX/Heading';
import {Challenge} from './Challenge';
import {Navigation} from './Navigation';
import {useRouter} from 'next/router';
interface ChallengesProps {
children: React.ReactElement[];
isRecipes?: boolean;
titleText?: string;
titleId?: string;
noTitle?: boolean;
}
export interface ChallengeContents {
id: string;
name: string;
order: number;
content: React.ReactNode;
solution: React.ReactNode;
hint?: React.ReactNode;
}
const parseChallengeContents = (
children: React.ReactElement[]
): ChallengeContents[] => {
const contents: ChallengeContents[] = [];
if (!children) {
return contents;
}
let challenge: Partial<ChallengeContents> = {};
let content: React.ReactElement[] = [];
Children.forEach(children, (child) => {
const {props, type} = child;
switch ((type as any).mdxName) {
case 'Solution': {
challenge.solution = child;
challenge.content = content;
contents.push(challenge as ChallengeContents);
challenge = {};
content = [];
break;
}
case 'Hint': {
challenge.hint = child;
break;
}
case 'h4': {
challenge.order = contents.length + 1;
challenge.name = props.children;
challenge.id = props.id;
break;
}
default: {
content.push(child);
}
}
});
return contents;
};
enum QueuedScroll {
INIT = 'init',
NEXT = 'next',
}
export function Challenges({
children,
isRecipes,
noTitle,
titleText = isRecipes ? 'Try out some examples' : 'कुछ चुनौतियाँ आज़माएँ',
titleId = isRecipes ? 'examples' : 'challenges',
}: ChallengesProps) {
const challenges = parseChallengeContents(children);
const totalChallenges = challenges.length;
const scrollAnchorRef = useRef<HTMLDivElement>(null);
const queuedScrollRef = useRef<undefined | QueuedScroll>(QueuedScroll.INIT);
const [activeIndex, setActiveIndex] = useState(0);
const currentChallenge = challenges[activeIndex];
const {asPath} = useRouter();
useEffect(() => {
if (queuedScrollRef.current === QueuedScroll.INIT) {
const initIndex = challenges.findIndex(
(challenge) => challenge.id === asPath.split('#')[1]
);
if (initIndex === -1) {
queuedScrollRef.current = undefined;
} else if (initIndex !== activeIndex) {
setActiveIndex(initIndex);
}
}
if (queuedScrollRef.current) {
scrollAnchorRef.current!.scrollIntoView({
block: 'start',
...(queuedScrollRef.current === QueuedScroll.NEXT && {
behavior: 'smooth',
}),
});
queuedScrollRef.current = undefined;
}
}, [activeIndex, asPath, challenges]);
const handleChallengeChange = (index: number) => {
setActiveIndex(index);
};
const Heading = isRecipes ? H4 : H2;
return (
<div className="max-w-7xl mx-auto py-4 w-full">
<div
className={cn(
'border-gray-10 bg-card dark:bg-card-dark shadow-inner rounded-none -mx-5 sm:mx-auto sm:rounded-2xl'
)}>
<div ref={scrollAnchorRef} className="py-2 px-5 sm:px-8 pb-0 md:pb-0">
{!noTitle && (
<Heading
id={titleId}
className={cn(
'mb-2 leading-10 relative',
isRecipes
? 'text-xl text-purple-50 dark:text-purple-30'
: 'text-3xl text-link'
)}>
{titleText}
</Heading>
)}
{totalChallenges > 1 && (
<Navigation
currentChallenge={currentChallenge}
challenges={challenges}
handleChange={handleChallengeChange}
isRecipes={isRecipes}
/>
)}
</div>
<Challenge
key={currentChallenge.id}
isRecipes={isRecipes}
currentChallenge={currentChallenge}
totalChallenges={totalChallenges}
hasNextChallenge={activeIndex < totalChallenges - 1}
handleClickNextChallenge={() => {
setActiveIndex((i) => i + 1);
queuedScrollRef.current = QueuedScroll.NEXT;
}}
/>
</div>
</div>
);
}