Skip to content

Commit f33ca58

Browse files
feat(search-view): show recent packages (#466)
1 parent 7025a19 commit f33ca58

File tree

8 files changed

+58
-25
lines changed

8 files changed

+58
-25
lines changed

i18n/english.js

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const ui = {
192192
"Available licenses": "Available licenses",
193193
"Available flags": "Available flags",
194194
default: "Search options",
195+
recentPackages: "Recent packages",
195196
packagesCache: "Packages available in the cache",
196197
noPackageFound: "No package found",
197198
packageLengthErr: "Package name must be between 2 and 64 characters.",

i18n/french.js

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const ui = {
192192
"Available licenses": "Licences disponibles",
193193
"Available flags": "Drapeaux disponibles",
194194
default: "Options de recherche",
195+
recentPackages: "Packages récents",
195196
packagesCache: "Packages disponibles dans le cache",
196197
noPackageFound: "Aucun package trouvé",
197198
packageLengthErr: "Le nom du package doit être compris entre 2 et 64 caractères.",

public/components/views/search/search.css

+4-5
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,10 @@ input:-webkit-autofill {
232232
width: 20px;
233233
}
234234

235-
.cache-packages {
235+
.cache-packages, .recent-packages {
236236
display: flex;
237237
flex-direction: column;
238238
align-items: center;
239-
justify-content: center;
240239
margin-top: 20px;
241240
margin-bottom: 20px;
242241
width: 100%;
@@ -250,18 +249,18 @@ input:-webkit-autofill {
250249
overflow: auto;
251250
}
252251

253-
.cache-packages h1 {
252+
.cache-packages h1, .recent-packages h1 {
254253
font-family: "mononoki";
255254
color: #546884;
256255
}
257256

258-
.cache-packages .package-result {
257+
.cache-packages .package-result, .recent-packages .package-result {
259258
margin-top: 10px;
260259
background: #54688419;
261260
padding: 5px 10px;
262261
}
263262

264-
.cache-packages .package-result:has(span:hover) {
263+
.cache-packages .package-result:has(span:hover), .recent-packages .package-result:has(span:hover) {
265264
color: var(--secondary-darker);
266265
background: #5468842a;
267266
cursor: pointer;

public/components/views/search/search.js

+41-18
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,49 @@ export class SearchView {
162162
cachePackagesElement.appendChild(h1Element);
163163

164164
for (const pkg of window.scannedPackageCache) {
165-
const { name, version, local } = parseNpmSpec(pkg);
166-
const pkgElement = document.createElement("div");
167-
pkgElement.classList.add("package-result");
168-
const pkgSpanElement = document.createElement("span");
169-
pkgSpanElement.innerHTML = `${name}@${version}${local ? " <b>local</b>" : ""}`;
170-
pkgSpanElement.addEventListener("click", () => {
171-
window.socket.send(JSON.stringify({ action: "SEARCH", pkg }));
172-
}, { once: true });
173-
const removeButton = createDOMElement("button", {
174-
classList: ["remove"],
175-
text: "x"
176-
});
177-
removeButton.addEventListener("click", (event) => {
178-
event.stopPropagation();
179-
window.socket.send(JSON.stringify({ action: "REMOVE", pkg }));
180-
}, { once: true });
181-
pkgElement.append(pkgSpanElement, removeButton);
182-
cachePackagesElement.appendChild(pkgElement);
165+
cachePackagesElement.appendChild(this.#cachePackageElement(pkg));
183166
}
184167
}
185168
else {
186169
cachePackagesElement.classList.add("hidden");
187170
}
171+
172+
const recentPackagesElement = this.searchContainer.querySelector(".recent-packages");
173+
if (window.recentPackageCache.length > 0) {
174+
recentPackagesElement.classList.remove("hidden");
175+
const h1Element = document.createElement("h1");
176+
h1Element.textContent = window.i18n[lang].search.recentPackages;
177+
recentPackagesElement.appendChild(h1Element);
178+
179+
for (const pkg of window.recentPackageCache) {
180+
recentPackagesElement.appendChild(this.#cachePackageElement(pkg));
181+
}
182+
}
183+
else {
184+
recentPackagesElement.classList.add("hidden");
185+
}
186+
}
187+
188+
#cachePackageElement(pkg) {
189+
const { name, version, local } = parseNpmSpec(pkg);
190+
const pkgElement = document.createElement("div");
191+
pkgElement.classList.add("package-result");
192+
const pkgSpanElement = document.createElement("span");
193+
pkgSpanElement.innerHTML = `${name}@${version}${local ? " <b>local</b>" : ""}`;
194+
pkgSpanElement.addEventListener("click", () => {
195+
window.socket.send(JSON.stringify({ action: "SEARCH", pkg }));
196+
}, { once: true });
197+
const removeButton = createDOMElement("button", {
198+
classList: ["remove"],
199+
text: "x"
200+
});
201+
removeButton.addEventListener("click", (event) => {
202+
event.stopPropagation();
203+
window.socket.send(JSON.stringify({ action: "REMOVE", pkg }));
204+
}, { once: true });
205+
pkgElement.append(pkgSpanElement, removeButton);
206+
207+
return pkgElement;
188208
}
189209

190210
async fetchPackage(packageName, version) {
@@ -222,6 +242,9 @@ export class SearchView {
222242
const cachePackagesElement = document.createElement("div");
223243
cachePackagesElement.classList.add("cache-packages", "hidden");
224244
searchViewContainer.appendChild(cachePackagesElement);
245+
const recentPackagesElement = document.createElement("div");
246+
recentPackagesElement.classList.add("recent-packages", "hidden");
247+
searchViewContainer.appendChild(recentPackagesElement);
225248

226249
this.initialize();
227250
}

public/main.js

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let packageInfoOpened = false;
2929

3030
document.addEventListener("DOMContentLoaded", async() => {
3131
window.scannedPackageCache = [];
32+
window.recentPackageCache = [];
3233
window.locker = null;
3334
window.popup = new Popup();
3435
window.settings = await new Settings().fetchUserConfig();
@@ -56,10 +57,15 @@ document.addEventListener("DOMContentLoaded", async() => {
5657
}
5758
else if (data.status === "INIT" || data.status === "RELOAD") {
5859
window.scannedPackageCache = data.availables;
60+
window.recentPackageCache = data.lru;
5961
console.log(
6062
"[INFO] Older packages are loaded!",
6163
window.scannedPackageCache
6264
);
65+
console.log(
66+
"[INFO] Recent packages are loaded!",
67+
window.recentPackageCache
68+
);
6369

6470
initSearchNav(data, {
6571
searchOptions: {

src/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class _AppCache {
4747

4848
getPayload(pkg) {
4949
try {
50-
return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll(kSlashReplaceToken, "/")), "utf-8"));
50+
return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", kSlashReplaceToken)), "utf-8"));
5151
}
5252
catch (err) {
5353
logger.error(`[cache|get](pkg: ${pkg}|cache: not found)`);

src/http-server/endpoints/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function get(_req, res) {
3737
mru: [formatted],
3838
current: formatted,
3939
lru: [],
40-
availables: appCache.availablePayloads(),
40+
availables: appCache.availablePayloads().filter((pkg) => pkg !== formatted),
4141
lastUsed: {
4242
[formatted]: Date.now()
4343
},

views/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
<input type="text" id="package" name="package" placeholder="[[=z.token('search.registryPlaceholder')]]" autocomplete="off" required>
9696
</div>
9797
</form>
98+
<div class="recent-packages">
99+
<h2>[[=z.token('search.recentPackages')]]</h2>
100+
</div>
98101
<div class="cache-packages">
99102
<h2>[[=z.token('search.packagesCache')]]</h2>
100103
</div>

0 commit comments

Comments
 (0)