-
Notifications
You must be signed in to change notification settings - Fork 2
/
home.tsx
94 lines (90 loc) · 2.66 KB
/
home.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
/** @jsx h */
import { h } from "preact";
import { renderToString } from "preact-render-to-string";
import { PluginData, PluginsData, readInfoFile } from "./readInfoFile.ts";
export async function renderHome() {
const content = await renderContent();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="Latest plugin versions for dprint." />
<title>Plugins - dprint</title>
<script>
addEventListener("load", () => {
for (const button of document.getElementsByClassName("copy-button")) {
button.addEventListener("click", () => {
// hack to copy to clipboard
const textArea = document.createElement("textarea");
textArea.value = button.dataset.url;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
});
}
});
</script>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
${content}
</body>
</html>
`;
}
async function renderContent() {
const pluginsData = await readInfoFile();
const section = (
<section id="content">
<h1>Plugins</h1>
{renderPlugins(pluginsData)}
<p>
Helpful commands:
<ul>
<li>
<code>dprint config update</code> - Automatically updates the plugins in a config file.
</li>
<li>
<code>dprint config add</code> - Adds one of these plugins via a multi-select prompt.
</li>
<li>
<code>dprint config add <plugin-name></code> - Adds a plugin by name.
</li>
<li>
<code>dprint config add <gh-org>/<gh-repo></code> - Adds a plugin by GitHub repo.
</li>
</ul>
</p>
</section>
);
return renderToString(section);
}
function renderPlugins(data: PluginsData) {
return (
<div id="plugins">
<div id="plugins-header">
<div>Name</div>
<div>Latest URL</div>
<div>Downloads</div>
<div></div>
</div>
{data.latest.map((plugin) => renderPlugin(plugin))}
</div>
);
}
function renderPlugin(plugin: PluginData) {
return (
<div className="plugin" key={plugin.name}>
<div>{plugin.name}</div>
<div>{plugin.url}</div>
<div>{plugin.downloadCount.allVersions?.toLocaleString("en-US")}</div>
<div>
<button className="copy-button" title="Copy to clipboard" data-url={plugin.url}>
Copy
</button>
</div>
</div>
);
}