-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
188 lines (170 loc) · 6.09 KB
/
index.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import dynamic from "next/dynamic"
import { useMemo, useState, useEffect } from "react"
import {
FiGithub,
BiRocket,
BiCoffeeTogo,
BsPencilSquare,
FaHome,
FaBug,
AiOutlineRead,
} from "../icons"
import { SpinnerDots } from "../spinner"
import { SquareButton } from "../button"
import Main from "../main"
import NotebookLayout from "../main/two-sections"
import { BigHeadNotice } from "../big-head-girl"
import { PrettyHeader, SimpleLink } from "../pretty-defaults"
import { DefaultErrorBoundary } from "../error-boundary"
import { Pagination, CallToActionUl } from "./presentational-components"
import DynamicMarkdownRender from "../markdown-render/dynamic"
const KOFI_URL = "https://ko-fi.com/minimithi"
const EPIC_NOTES_REPO_URL = "https://github.com/mithi/epic-react-exercises"
const issueUrl = message => `${EPIC_NOTES_REPO_URL}/issues/new?title=${message}`
const editUrl = editPath =>
`${EPIC_NOTES_REPO_URL}/edit/main/content/${editPath}/notes.md`
const solutionUrl = editPath => `${EPIC_NOTES_REPO_URL}/edit/main/content/${editPath}`
const BUTTONS_PROPS = {
deployedSite: {
"aria-label": "go to Epic React deployed site",
"title": "go to Epic React deployed site",
"children": <BiRocket />,
},
repository: {
"aria-label": "go to Epic React repository",
"title": "go to Epic React repository",
"children": <FiGithub />,
},
edit: {
"aria-label": "edit this page",
"title": "edit this page",
"children": <BsPencilSquare />,
},
issue: {
"aria-label": "suggest a better solution via an issue",
"title": "suggest a better solution via an issue",
"children": <FaBug />,
},
home: {
"aria-label": "go back to main page",
"title": "go back to main page",
"children": <FaHome />,
},
kofi: {
"aria-label": "buy me a coffee",
"title": "buy me a coffee",
"children": <BiCoffeeTogo />,
},
solution: {
"aria-label": "view complete solution",
"title": "view complete solution",
"children": <AiOutlineRead />,
},
}
const PageLayout = ({
properties,
pageId,
notesString,
numberOfPages,
hasApp,
topic,
section,
}) => {
const [resetCounter, setResetCounter] = useState(0)
const App = useMemo(
() =>
hasApp
? dynamic(() => import(`content/${topic}/${section}/${pageId}/app`), {
// eslint-disable-next-line react/display-name
loading: () => <SpinnerDots />,
})
: () => null,
[hasApp, topic, section, pageId]
)
const editPath = `${topic}/${section}/${pageId}`
const resetKey = `${editPath}/${resetCounter}`
const solutionHref = solutionUrl(editPath)
const issueHref = issueUrl(`Better solution! Suggestion for: ${editPath}`)
const editHref = editUrl(editPath)
const editThisPage = <SimpleLink href={editHref}>{"✏️ Edit"}</SimpleLink>
const viewSolution = (
<SimpleLink href={solutionHref}>{"👀 View the deployed code "}</SimpleLink>
)
const suggestAchange = (
<SimpleLink href={issueHref}>{"🐞🐛 Suggest a change."}</SimpleLink>
)
const callToActionBox = (
<BigHeadNotice headSize={60}>
<CallToActionUl>
<li>{viewSolution} on Github.</li>
<li>Not happy with the solution? {suggestAchange}</li>
<li>Grammar errors? {editThisPage} this page.</li>
</CallToActionUl>
</BigHeadNotice>
)
const { deployedSite, repository, title } = properties
const bp = BUTTONS_PROPS
const notebookPageButtons = (
<div style={{ display: "flex", margin: "10px 0" }}>
{deployedSite && <SquareButton href={deployedSite} {...bp.deployedSite} />}
{repository && <SquareButton href={repository} {...bp.repository} />}
{hasApp && <SquareButton href={solutionHref} {...bp.solution} />}
<SquareButton href={editHref} {...bp.edit} />
{hasApp && <SquareButton href={issueHref} {...bp.issue} />}
<SquareButton href="/" {...bp.home} />
<SquareButton href={KOFI_URL} {...bp.kofi} />
</div>
)
const articlePlus = (
<div id="currentArticlePlus">
{hasApp && callToActionBox}
{hasApp && notebookPageButtons}
<article>
<DefaultErrorBoundary
key={resetKey}
resetFunction={() => setResetCounter(resetCounter + 1)}
message={`It's a failure within the article (${editPath})`}
>
<DynamicMarkdownRender>{notesString}</DynamicMarkdownRender>
</DefaultErrorBoundary>
</article>
<div style={{ display: "flex", justifyContent: "center", margin: "20px" }}>
{notebookPageButtons}
</div>
</div>
)
const div1 = (
<>
<PrettyHeader id="sectionTitle" Component="h1" style={{ margin: "5px" }}>
{title}
</PrettyHeader>
<Pagination
currentPageId={Number(pageId)}
pathname={`/${topic}/${section}`}
{...{ numberOfPages }}
/>
<DefaultErrorBoundary
key={resetKey}
resetFunction={() => setResetCounter(resetCounter + 1)}
message={`It's a failure within the example component (${editPath})`}
>
{hasApp ? <App /> : articlePlus}
</DefaultErrorBoundary>
</>
)
const div2 = hasApp && articlePlus
const main = (
<Main>
<div id="main">
<NotebookLayout {...{ div1, div2 }} />
</div>
</Main>
)
useEffect(() => {
document.getElementById("currentArticlePlus").scrollIntoView()
document.getElementById("sectionTitle").scrollIntoView()
document.getElementById("main").scrollIntoView()
}, [div2])
return main
}
export default PageLayout