-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathPage.jsx
162 lines (147 loc) · 4.41 KB
/
Page.jsx
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
// Import External Dependencies
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { useLocation } from 'react-router-dom';
// Import Components
import PageLinks from '../PageLinks/PageLinks';
import Markdown from '../Markdown/Markdown';
import Contributors from '../Contributors/Contributors';
import { PlaceholderString } from '../Placeholder/Placeholder';
import AdjacentPages from './AdjacentPages';
// Load Styling
import './Page.scss';
import Link from '../Link/Link';
export default function Page(props) {
const {
title,
contributors = [],
related = [],
previous,
next,
...rest
} = props;
const isDynamicContent = props.content instanceof Promise;
const [content, setContent] = useState(
isDynamicContent
? PlaceholderString()
: () => props.content.default || props.content
);
const [contentLoaded, setContentLoaded] = useState(
isDynamicContent ? false : true
);
useEffect(() => {
if (props.content instanceof Promise) {
props.content
.then((mod) => {
setContent(() => mod.default || mod);
setContentLoaded(true);
})
.catch(() => setContent('Error loading content.'));
}
}, [props.content]);
const { hash, pathname } = useLocation();
useEffect(() => {
let observer;
if (contentLoaded) {
if (hash) {
const target = document.querySelector('#md-content');
// two cases here
// 1. server side rendered page, so hash target is already there
if (document.querySelector(hash)) {
document.querySelector(hash).scrollIntoView();
} else {
// 2. dynamic loaded content
// we need to observe the dom change to tell if hash exists
observer = new MutationObserver(() => {
const element = document.querySelector(hash);
if (element) {
element.scrollIntoView();
}
});
observer.observe(target, {
childList: true,
attributes: false,
subtree: false,
});
}
} else {
window.scrollTo(0, 0);
}
}
return () => {
if (observer) {
observer.disconnect();
}
};
}, [contentLoaded, pathname, hash]);
const numberOfContributors = contributors.length;
const loadRelated = contentLoaded && related && related.length !== 0;
const loadContributors =
contentLoaded && contributors && numberOfContributors !== 0;
let contentRender;
if (typeof content === 'function') {
contentRender = content({}).props.children;
} else {
contentRender = (
<div
dangerouslySetInnerHTML={{
__html: content,
}}
/>
);
}
return (
<section className="page">
<Markdown>
<h1>{title}</h1>
{rest.thirdParty ? (
<div className="italic my-[20px]">
<strong className="font-bold">Disclaimer:</strong> {title} is a
third-party package maintained by community members, it potentially
does not have the same support, security policy or license as
webpack, and it is not maintained by webpack.
</div>
) : null}
<div id="md-content">{contentRender}</div>
{loadRelated && (
<div className="print:hidden">
<h2>Further Reading</h2>
<ul>
{related.map((link, index) => (
<li key={index}>
<Link to={link.url}>{link.title}</Link>
</li>
))}
</ul>
</div>
)}
<PageLinks page={rest} />
{(previous || next) && (
<AdjacentPages previous={previous} next={next} />
)}
{loadContributors && (
<div data-testid="contributors" className="print:hidden">
<h2 className="font-sans! font-normal!">
{numberOfContributors}{' '}
{numberOfContributors === 1 ? 'Contributor' : 'Contributors'}
</h2>
<Contributors contributors={contributors} />
</div>
)}
</Markdown>
</section>
);
}
Page.propTypes = {
title: PropTypes.string,
contributors: PropTypes.array,
related: PropTypes.array,
previous: PropTypes.object,
next: PropTypes.object,
content: PropTypes.oneOfType([
PropTypes.shape({
then: PropTypes.func.isRequired,
default: PropTypes.string,
}),
]),
};