-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathindex.tsx
164 lines (144 loc) · 4.75 KB
/
index.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
162
163
164
import React, { useReducer } from "react";
import { useLocation } from "react-router-dom";
import type bcd from "@mdn/browser-compat-data/types";
import { BrowserInfoContext } from "./browser-info";
import { BrowserCompatibilityErrorBoundary } from "./error-boundary";
import { FeatureRow } from "./feature-row";
import { PLATFORM_BROWSERS, Headers } from "./headers";
import { Legend } from "./legend";
import { listFeatures } from "./utils";
// Note! Don't import any SCSS here inside *this* component.
// It's done in the component that lazy-loads this component.
// This string is used to prefill the body when clicking to file a new BCD
// issue over on github.com/mdn/browser-compat-data
const NEW_ISSUE_TEMPLATE = `
<!-- Tips: where applicable, specify browser name, browser version, and mobile operating system version -->
#### What information was incorrect, unhelpful, or incomplete?
#### What did you expect to see?
#### Did you test this? If so, how?
<!-- Do not make changes below this line -->
<details>
<summary>MDN page report details</summary>
* Query: \`$QUERY_ID\`
* MDN URL: https://developer.mozilla.org$PATHNAME
* Report started: $DATE
</details>
`;
function gatherPlatformsAndBrowsers(
category: string
): [string[], bcd.BrowserNames[]] {
let platforms = ["desktop", "mobile"];
if (category === "javascript") {
platforms.push("server");
} else if (category === "webextensions") {
platforms = ["webextensions-desktop", "webextensions-mobile"];
}
return [
platforms,
platforms.map((platform) => PLATFORM_BROWSERS[platform] || []).flat(),
];
}
type CellIndex = [number, number];
function FeatureListAccordion({
features,
browsers,
locale,
}: {
features: ReturnType<typeof listFeatures>;
browsers: bcd.BrowserNames[];
locale: string;
}) {
const [[activeRow, activeColumn], dispatchCellToggle] = useReducer<
React.Reducer<CellIndex | [null, null], CellIndex>
>(
([activeRow, activeColumn], [row, column]) =>
activeRow === row && activeColumn === column
? [null, null]
: [row, column],
[null, null]
);
return (
<>
{features.map((feature, i) => (
<FeatureRow
key={i}
{...{ feature, browsers }}
index={i}
activeCell={activeRow === i ? activeColumn : null}
onToggleCell={([row, column]: [number, number]) => {
dispatchCellToggle([row, column]);
}}
locale={locale}
/>
))}
</>
);
}
export default function BrowserCompatibilityTable({
query,
data,
browsers: browserInfo,
locale,
}: {
query: string;
data: bcd.Identifier;
browsers: bcd.Browsers;
locale: string;
}) {
const location = useLocation();
if (!data || !Object.keys(data).length) {
throw new Error(
"BrowserCompatibilityTable component called with empty data"
);
}
const breadcrumbs = query.split(".");
const category = breadcrumbs[0];
const name = breadcrumbs[breadcrumbs.length - 1];
const [platforms, browsers] = gatherPlatformsAndBrowsers(category);
function getNewIssueURL() {
const url = "https://github.com/mdn/browser-compat-data/issues/new";
const sp = new URLSearchParams();
const body = NEW_ISSUE_TEMPLATE.replace(/\$PATHNAME/g, location.pathname)
.replace(/\$DATE/g, new Date().toISOString())
.replace(/\$QUERY_ID/g, query)
.trim();
sp.set("body", body);
sp.set("title", `${query} - <PUT TITLE HERE>`);
return `${url}?${sp.toString()}`;
}
return (
<BrowserCompatibilityErrorBoundary>
<BrowserInfoContext.Provider value={browserInfo}>
<a
className="bc-github-link external external-icon"
href={getNewIssueURL()}
target="_blank"
rel="noopener noreferrer"
title="Report an issue with this compatibility data"
>
Report problems with this compatibility data on GitHub
</a>
<table key="bc-table" className="bc-table bc-table-web">
<Headers {...{ platforms, browsers }} />
<tbody>
<FeatureListAccordion
browsers={browsers}
features={listFeatures(data, "", name)}
locale={locale}
/>
</tbody>
</table>
<Legend compat={data} name={name} />
{/* https://github.com/mdn/yari/issues/1191 */}
<div className="hidden">
The compatibility table on this page is generated from structured
data. If you'd like to contribute to the data, please check out{" "}
<a href="https://github.com/mdn/browser-compat-data">
https://github.com/mdn/browser-compat-data
</a>{" "}
and send us a pull request.
</div>
</BrowserInfoContext.Provider>
</BrowserCompatibilityErrorBoundary>
);
}