unpkg/server/components/IndexPage.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-17 00:00:06 +00:00
const React = require("react");
const semver = require("semver");
const DirectoryListing = require("./DirectoryListing");
const readCSS = require("../utils/readCSS");
2017-05-25 18:25:42 +00:00
2018-02-17 00:00:06 +00:00
const e = React.createElement;
2017-05-25 18:25:42 +00:00
2018-02-17 00:00:06 +00:00
const IndexPageStyle = readCSS(__dirname, "IndexPage.css");
2017-05-25 18:25:42 +00:00
const IndexPageScript = `
var s = document.getElementById('version'), v = s.value
s.onchange = function () {
window.location.href = window.location.href.replace('@' + v, '@' + s.value)
}
2018-02-17 00:00:06 +00:00
`;
2017-05-25 18:25:42 +00:00
2018-02-17 00:00:06 +00:00
const byVersion = (a, b) => (semver.lt(a, b) ? -1 : semver.gt(a, b) ? 1 : 0);
2017-05-25 18:25:42 +00:00
2018-02-17 00:00:06 +00:00
function IndexPage({ packageInfo, version, dir, entries }) {
const versions = Object.keys(packageInfo.versions).sort(byVersion);
const options = versions.map(v =>
e("option", { key: v, value: v }, `${packageInfo.name}@${v}`)
);
2017-05-25 18:25:42 +00:00
2017-11-08 16:57:15 +00:00
return e(
2017-11-25 21:25:01 +00:00
"html",
2017-11-08 16:57:15 +00:00
null,
e(
2017-11-25 21:25:01 +00:00
"head",
2017-11-08 16:57:15 +00:00
null,
2017-11-25 21:25:01 +00:00
e("meta", { charSet: "utf-8" }),
e("title", null, `Index of ${dir}`),
e("style", { dangerouslySetInnerHTML: { __html: IndexPageStyle } })
2017-11-08 16:57:15 +00:00
),
e(
2017-11-25 21:25:01 +00:00
"body",
2017-11-08 16:57:15 +00:00
null,
e(
2017-11-25 21:25:01 +00:00
"div",
{ className: "content-wrapper" },
2017-11-08 16:57:15 +00:00
e(
2017-11-25 21:25:01 +00:00
"div",
{ className: "version-wrapper" },
e("select", { id: "version", defaultValue: version }, options)
2017-11-08 16:57:15 +00:00
),
2017-11-25 21:25:01 +00:00
e("h1", null, `Index of ${dir}`),
e("script", { dangerouslySetInnerHTML: { __html: IndexPageScript } }),
e("hr"),
2017-11-08 16:57:15 +00:00
e(DirectoryListing, { dir, entries }),
2017-11-25 21:25:01 +00:00
e("hr"),
e("address", null, `${packageInfo.name}@${version}`)
2017-05-25 18:25:42 +00:00
)
)
2018-02-17 00:00:06 +00:00
);
2017-05-25 18:25:42 +00:00
}
2018-02-17 00:00:06 +00:00
module.exports = IndexPage;