Skip to content

Commit 1f8240b

Browse files
committed
ref: Upgrade several components to typescript
1 parent 273d21e commit 1f8240b

37 files changed

+962
-739
lines changed

.babelrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ if (process.env.NODE_ENV !== `test`) {
99
module.exports = {
1010
sourceMaps: true,
1111
presets: ["babel-preset-gatsby-package"],
12-
ignore
12+
ignore,
1313
};

jest-transformer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const babelPreset = require(`babel-preset-gatsby-package`)();
22
module.exports = require(`babel-jest`).createTransformer({
3-
...babelPreset
3+
...babelPreset,
44
});

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
transform: {
3-
"^.+\\.[jt]sx?$": `<rootDir>/jest-transformer.js`
3+
"^.+\\.[jt]sx?$": `<rootDir>/jest-transformer.js`,
44
},
55
testEnvironment: `jest-environment-jsdom-fourteen`,
66
testPathIgnorePatterns: [`node_modules`, `\\.cache`, `<rootDir>.*/public`],
7-
transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`]
7+
transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
88
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@sentry/gatsby": "^5.20.1",
1919
"@sentry/react": "^5.20.0",
2020
"@sentry/tracing": "^5.20.1",
21+
"@types/dompurify": "^2.0.3",
2122
"@types/node": "^12",
2223
"@types/react": "^16.9.46",
2324
"@types/react-dom": "^16.9.8",
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import SmartLink from "./smartLink";
77
import TableOfContents from "./tableOfContents";
88
import * as Sentry from "@sentry/gatsby";
99

10-
const GitHubCTA = ({ sourceInstanceName, relativePath }) => (
10+
type GitHubCTAProps = {
11+
sourceInstanceName: string;
12+
relativePath: string;
13+
};
14+
15+
const GitHubCTA = ({
16+
sourceInstanceName,
17+
relativePath,
18+
}: GitHubCTAProps): JSX.Element => (
1119
<div className="github-cta">
1220
<small>
1321
You can{" "}
@@ -21,12 +29,33 @@ const GitHubCTA = ({ sourceInstanceName, relativePath }) => (
2129
</div>
2230
);
2331

32+
type Props = {
33+
data?: {
34+
file?: {
35+
childMarkdownRemark?: {
36+
tableOfContents?: any;
37+
[key: string]: any;
38+
};
39+
childMdx?: {
40+
tableOfContents?: any;
41+
[key: string]: any;
42+
};
43+
[key: string]: any;
44+
};
45+
};
46+
pageContext?: {
47+
title?: string;
48+
};
49+
sidebar?: JSX.Element;
50+
children?: JSX.Element;
51+
};
52+
2453
export default ({
25-
data: { file } = { data: {} },
26-
pageContext: { title } = { pageContext: {} },
54+
data: { file } = {},
55+
pageContext: { title } = {},
2756
sidebar,
2857
children,
29-
}) => {
58+
}: Props): JSX.Element => {
3059
const tx = Sentry.getCurrentHub()
3160
.getScope()
3261
.getTransaction();
@@ -37,7 +66,7 @@ export default ({
3766
const child = file && (file.childMarkdownRemark || file.childMdx);
3867
const hasToc = child && !!child.tableOfContents.items;
3968
return (
40-
<Layout title={title} file={file} sidebar={sidebar}>
69+
<Layout sidebar={sidebar}>
4170
<SEO title={title} file={file} />
4271

4372
<div className="row">

src/components/breadcrumbs.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/components/breadcrumbs.tsx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from "react";
2+
import { useLocation } from "@reach/router";
3+
import { StaticQuery, graphql } from "gatsby";
4+
import SmartLink from "./smartLink";
5+
6+
const query = graphql`
7+
query BreadcrumbsQuery {
8+
allSitePage {
9+
nodes {
10+
id
11+
path
12+
context {
13+
title
14+
}
15+
}
16+
}
17+
}
18+
`;
19+
20+
type Node = {
21+
id: string;
22+
path: string;
23+
context: {
24+
title: string;
25+
};
26+
};
27+
28+
type Props = {
29+
data: {
30+
allSitePage: {
31+
nodes: Node[];
32+
};
33+
};
34+
};
35+
36+
export const Breadcrumbs = ({
37+
data: {
38+
allSitePage: { nodes },
39+
},
40+
}: Props) => {
41+
const location = useLocation();
42+
const currentPath = location.pathname;
43+
let rootNode = nodes.find(n => n.path === currentPath);
44+
if (!rootNode) {
45+
console.warn(`Cant find root node for breadcrumbs: ${currentPath}`);
46+
return null;
47+
}
48+
let trailNodes = nodes.filter(n => rootNode.path.indexOf(n.path) === 0);
49+
50+
return (
51+
<ul className="breadcrumb" style={{ marginBottom: "1rem" }}>
52+
{trailNodes
53+
.sort((a, b) => a.path.localeCompare(b.path))
54+
.map(n => {
55+
return (
56+
<li className="breadcrumb-item" key={n.id}>
57+
<SmartLink to={n.path}>{n.context.title}</SmartLink>
58+
</li>
59+
);
60+
})}
61+
</ul>
62+
);
63+
};
64+
65+
export default () => {
66+
return (
67+
<StaticQuery
68+
query={query}
69+
render={data => {
70+
return <Breadcrumbs data={data} />;
71+
}}
72+
/>
73+
);
74+
};
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useState, useRef, useContext } from "react";
2-
import PropTypes from "prop-types";
32
import copy from "copy-to-clipboard";
43
import { MDXProvider } from "@mdx-js/react";
54
import { Clipboard } from "react-feather";
@@ -15,7 +14,7 @@ function makeKeywordsClickable(children) {
1514

1615
KEYWORDS_REGEX.lastIndex = 0;
1716

18-
return children.reduce((arr, child) => {
17+
return children.reduce((arr: any[], child) => {
1918
if (typeof child !== "string") {
2019
arr.push(child);
2120
return arr;
@@ -55,20 +54,22 @@ function Selector({ keyword, group, ...props }) {
5554
sharedSelection,
5655
setSharedSelection,
5756
] = codeContext.sharedKeywordSelection;
58-
const spanRef = useRef();
59-
const [menuRef, setMenuRef] = useRefWithCallback(menuNode => {
60-
if (menuNode) {
61-
for (const node of menuNode.childNodes) {
62-
if (node.getAttribute("data-active") === "1") {
63-
node.parentNode.scrollTop =
64-
node.offsetTop -
65-
node.parentNode.getBoundingClientRect().height / 2 +
66-
node.getBoundingClientRect().height / 2;
67-
break;
57+
const spanRef = useRef<HTMLSpanElement>();
58+
const [menuRef, setMenuRef] = useRefWithCallback<HTMLSpanElement>(
59+
menuNode => {
60+
if (menuNode) {
61+
for (const node of menuNode.childNodes as any) {
62+
if (node.getAttribute("data-active") === "1") {
63+
node.parentNode.scrollTop =
64+
node.offsetTop -
65+
node.parentNode.getBoundingClientRect().height / 2 +
66+
node.getBoundingClientRect().height / 2;
67+
break;
68+
}
6869
}
6970
}
7071
}
71-
});
72+
);
7273

7374
useOnClickOutside(menuRef, () => {
7475
if (isOpen) {
@@ -87,7 +88,9 @@ function Selector({ keyword, group, ...props }) {
8788

8889
// this is not super clean but since we can depend on the span
8990
// rendering before the menu this works.
90-
const style = {};
91+
const style: {
92+
[key: string]: any;
93+
} = {};
9194
if (spanRef.current) {
9295
const rect = spanRef.current.getBoundingClientRect();
9396
style.left = spanRef.current.offsetLeft - 10 + "px";
@@ -140,7 +143,7 @@ function Selector({ keyword, group, ...props }) {
140143
);
141144
}
142145

143-
function CodeWrapper(props) {
146+
function CodeWrapper(props): JSX.Element {
144147
let { children, class: className, ...rest } = props;
145148
if (children) {
146149
children = makeKeywordsClickable(children);
@@ -152,7 +155,7 @@ function CodeWrapper(props) {
152155
);
153156
}
154157

155-
function SpanWrapper(props) {
158+
function SpanWrapper(props): JSX.Element {
156159
let { children, class: className, ...rest } = props;
157160
if (children) {
158161
children = makeKeywordsClickable(children);
@@ -164,7 +167,12 @@ function SpanWrapper(props) {
164167
);
165168
}
166169

167-
function CodeBlock({ filename, children }) {
170+
type Props = {
171+
filename?: string;
172+
children: JSX.Element;
173+
};
174+
175+
export default ({ filename, children }: Props): JSX.Element => {
168176
const [showCopied, setShowCopied] = useState(false);
169177
const codeRef = useRef(null);
170178

@@ -197,12 +205,4 @@ function CodeBlock({ filename, children }) {
197205
</div>
198206
</div>
199207
);
200-
}
201-
202-
CodeBlock.propTypes = {
203-
language: PropTypes.string,
204-
filename: PropTypes.string,
205-
title: PropTypes.string,
206208
};
207-
208-
export default CodeBlock;

src/components/codeContext.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ const DEFAULTS: CodeKeywords = {
6161
],
6262
};
6363

64-
const CodeContext = React.createContext(DEFAULTS);
64+
type CodeContextType = {
65+
codeKeywords: CodeKeywords;
66+
sharedCodeSelection: any;
67+
sharedKeywordSelection: any;
68+
};
69+
70+
const CodeContext = React.createContext(null as CodeContextType | null);
6571

6672
const parseDsn = function(dsn: string): Dsn {
6773
const match = dsn.match(/^(.*?\/\/)(.*?):(.*?)@(.*?)(\/.*?)$/);
@@ -145,15 +151,15 @@ export function fetchCodeKeywords() {
145151
export default CodeContext;
146152

147153
export function useCodeContextState(fetcher = fetchCodeKeywords) {
148-
let [codeKeywords, setCodeKeywords] = useState(null);
154+
let [codeKeywords, setCodeKeywords] = useState(DEFAULTS);
149155
if (codeKeywords === null && cachedCodeKeywords !== null) {
150156
setCodeKeywords(cachedCodeKeywords);
151157
codeKeywords = cachedCodeKeywords;
152158
}
153159

154160
useEffect(() => {
155161
if (cachedCodeKeywords === null) {
156-
fetcher().then(config => {
162+
fetcher().then((config: CodeKeywords) => {
157163
cachedCodeKeywords = config;
158164
setCodeKeywords(config);
159165
});
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const LANGUAGES = {
1616
yaml: "YAML",
1717
};
1818

19-
function CodeTabs({ children }) {
19+
type Props = {
20+
children: JSX.Element | JSX.Element[];
21+
};
22+
23+
export default ({ children }: Props): JSX.Element => {
2024
if (!Array.isArray(children)) {
2125
children = [children];
2226
} else {
@@ -119,6 +123,4 @@ function CodeTabs({ children }) {
119123
<div className="tab-content">{code}</div>
120124
</div>
121125
);
122-
}
123-
124-
export default CodeTabs;
126+
};

0 commit comments

Comments
 (0)