-
Notifications
You must be signed in to change notification settings - Fork 471
/
hooks.js
118 lines (105 loc) 路 3.37 KB
/
hooks.js
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
import { useMemo, useEffect, useState } from "react";
export function useSearch({ type, page, filter, includeOfficial }) {
let [result, setResult] = useState();
useEffect(() => {
let outdated = false;
const searchParams = new URLSearchParams({
"x-algolia-agent": "parcel-plugin-browser",
"x-algolia-application-id": "OFCNCOG2CU",
"x-algolia-api-key": "f54e21fa3a2a0160595bb058179bfb1e",
});
const href = `https://ofcncog2cu-2.algolianet.com/1/indexes/*/queries?${searchParams.toString()}`;
fetch(href, {
method: "POST",
body: JSON.stringify({
// https://grep.app/search?q=OFCNCOG2CU
// https://github.com/algolia/npm-search#usage
requests: [
{
analyticsTags: ["parcel-plugin-browser"],
attributesToHighlight: ["name", "description", "keywords"],
restrictSearchableAttributes: ["_searchInternal.alternativeNames"],
attributesToRetrieve: [
"isDeprecated",
"description",
"dependencies",
// "downloadsLast30Days",
// "homepage",
"humanDownloadsLast30Days",
// "keywords",
"modified",
"name",
"owner",
// "repository",
// "version",
],
facets: ["owner.name", "isDeprecated"],
//facets: ["keywords", "keywords", "owner.name"],
filters: `${
!includeOfficial ? "NOT owner.name:parcel-bundler AND" : ""
} NOT owner.name:thejameskyle AND isDeprecated:false`,
hitsPerPage: 24,
indexName: "npm-search",
maxValuesPerFacet: 10,
page: page,
params: "",
query: `parcel-${type} ${filter}`,
tagFilters: "",
},
],
}),
}).then(async (r) => {
let json = await r.json();
if (!outdated) {
let [results] = json.results;
if (type !== "config") {
results.hits = results.hits?.filter(
(r) => "@parcel/plugin" in r.dependencies
);
}
setResult(results);
window.scrollTo(0, 0);
}
});
return () => {
outdated = true;
};
}, [type, page, filter, includeOfficial]);
return result;
}
export function useHashState(initialValue) {
const [search, setSearch] = useState(window.location.search);
useEffect(() => {
let onPopState = e => {
setSearch(window.location.search);
};
window.addEventListener('popstate', onPopState);
return () => {
window.removeEventListener('popstate', onPopState);
};
}, []);
const normalizedSearch = search.length > 0 ? search.substr(1) : "";
let state = useMemo(() => {
if (normalizedSearch.length > 0) {
try {
let params = new URLSearchParams(normalizedSearch);
return Object.fromEntries(
[...params.entries()].map(([k, v]) => [k, JSON.parse(v)])
);
} catch (e) {}
}
return initialValue;
}, [normalizedSearch]);
return [
state,
(change) => {
let newState = { ...state, ...change };
let params = new URLSearchParams();
for (let [k, v] of Object.entries(newState)) {
params.set(k, JSON.stringify(v));
}
setSearch('?' + params);
history.pushState(null, null, '?' + params);
},
];
}