2021-03-22 18:06:58 -06:00
|
|
|
import { h, Fragment } from 'preact';
|
|
|
|
import { useEffect, useState } from 'preact/hooks';
|
2021-03-25 18:23:45 -06:00
|
|
|
import Styles from './PluginSearchPage.module.css';
|
2021-03-19 17:17:38 -04:00
|
|
|
|
|
|
|
async function searchPlugins(val) {
|
|
|
|
const params3 = new URLSearchParams([
|
|
|
|
['q', 'snowpack plugin ' + (val || '')],
|
|
|
|
['count', '100'],
|
|
|
|
]);
|
|
|
|
const res = await fetch(
|
|
|
|
`https://api.skypack.dev/v1/search?${params3.toString()}`,
|
|
|
|
);
|
|
|
|
const jsonres = await res.json();
|
|
|
|
return jsonres.results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Card({ result }) {
|
2021-03-22 18:06:58 -06:00
|
|
|
const updatedAtFormatted = Intl.DateTimeFormat('en', {
|
|
|
|
month: 'long',
|
|
|
|
day: 'numeric',
|
|
|
|
year: 'numeric',
|
|
|
|
}).format(Date.parse(result.updatedAt));
|
|
|
|
return (
|
|
|
|
<li class={Styles.Card}>
|
|
|
|
<img class={Styles.Icon__Plugin} src="/img/plug-light.svg" />
|
|
|
|
<header class={Styles.CardHeader}>
|
|
|
|
<h3 class={Styles.CardName}>
|
|
|
|
<a href="https://www.npmjs.com/package/{result.name}" target="_blank">
|
|
|
|
<span itemprop="name">{result.name}</span>
|
|
|
|
</a>
|
|
|
|
</h3>
|
|
|
|
</header>
|
|
|
|
<p class={Styles.CardDesc} itemprop="description">
|
|
|
|
{result.description.split('. ')[0]}
|
|
|
|
</p>
|
|
|
|
<p class={Styles.CardSubtitle}>
|
|
|
|
Updated
|
|
|
|
<time class="" datetime={result.updatedAt}>
|
|
|
|
{updatedAtFormatted}
|
|
|
|
</time>
|
|
|
|
</p>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
2021-03-19 17:17:38 -04:00
|
|
|
|
2021-03-31 16:10:27 -04:00
|
|
|
function PluginSearchPageLive() {
|
2021-03-19 17:17:38 -04:00
|
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
|
|
const [results, setResults] = useState(null);
|
|
|
|
const [searchQuery, setSearchQuery] = useState(searchParams.get('q'));
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
setResults(await searchPlugins(searchParams.get('q')));
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
async function onFormSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const form = new FormData(e.target);
|
|
|
|
const formula = form.get('q');
|
|
|
|
// document.getElementById('loading-message').style.display = 'block';
|
|
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
|
|
searchParams.set('q', formula);
|
|
|
|
window.history.pushState(null, null, '?' + searchParams.toString());
|
|
|
|
setSearchQuery(formula);
|
|
|
|
setResults(await searchPlugins(formula));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<form
|
|
|
|
name="myform"
|
|
|
|
id="myform"
|
2021-03-22 18:06:58 -06:00
|
|
|
class={Styles.Form}
|
2021-03-19 17:17:38 -04:00
|
|
|
action="https://www.npmjs.com/search"
|
|
|
|
method="GET"
|
|
|
|
onSubmit={onFormSubmit}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="search"
|
|
|
|
name="q"
|
|
|
|
defaultValue={searchQuery}
|
|
|
|
placeholder="search Sass, sitemaps, image optimization..."
|
2021-03-22 18:06:58 -06:00
|
|
|
class={Styles.Input}
|
2021-03-19 17:17:38 -04:00
|
|
|
/>
|
2021-03-22 18:06:58 -06:00
|
|
|
<button type="submit" class={Styles.Submit}>
|
2021-03-19 17:17:38 -04:00
|
|
|
Search
|
|
|
|
</button>
|
|
|
|
</form>
|
2021-03-22 18:06:58 -06:00
|
|
|
<div class={Styles.Count} id="total-result-count">
|
|
|
|
{!searchQuery &&
|
|
|
|
results &&
|
|
|
|
results.length > 50 &&
|
|
|
|
`${results.length}+ plugins available!`}
|
2021-03-19 17:17:38 -04:00
|
|
|
</div>
|
2021-03-22 18:06:58 -06:00
|
|
|
<section id="search-results" class={Styles.Results}>
|
|
|
|
{!results && (
|
|
|
|
<div id="loading-message" class={Styles.Loading}>
|
|
|
|
Loading...
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{results && results.length === 0 && (
|
|
|
|
<ul class={Styles.CardList}>
|
|
|
|
<li style="margin: 1rem; text-align: center;">No results found.</li>
|
|
|
|
</ul>
|
|
|
|
)}
|
|
|
|
{results && results.length > 0 && (
|
|
|
|
<ul class={Styles.CardList}>
|
|
|
|
{results.map((r) => (
|
|
|
|
<Card result={r} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)}
|
2021-03-19 17:17:38 -04:00
|
|
|
</section>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2021-03-31 16:10:27 -04:00
|
|
|
|
|
|
|
export default function PluginSearchPage(props) {
|
2021-05-03 12:26:10 -06:00
|
|
|
return import.meta.env.astro ? (
|
|
|
|
<div>Loading...</div>
|
|
|
|
) : (
|
|
|
|
<PluginSearchPageLive {...props} />
|
|
|
|
);
|
2021-03-31 16:10:27 -04:00
|
|
|
}
|