Skip to content

Commit

Permalink
feat: history suggestions (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollorion authored Jun 1, 2022
1 parent f757e2f commit fc17338
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extension/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { main } from './main.js';

main().then(()=>{
console.log("Finished");
console.log("Fly extension loaded");
});
19 changes: 19 additions & 0 deletions extension/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ async function requestJson(url: string): Promise<string> {
});
}

export async function setHistory(history: string[]){
await setLocalStorage("history", JSON.stringify(history))
}

export async function getHistory(query: string[]): Promise<string[]> {
let history;

try {
history = await getLocalStorage("history");
history = [...JSON.parse(history)];
history.push(query.join(" "));
history = [...new Set(history)]
} catch (e) {
history = [query.join(" ")];
}

return history;
}

export async function repoManagement(query: string[]): Promise<string> {
query.shift();
const action = query[0];
Expand Down
12 changes: 9 additions & 3 deletions extension/localstorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export async function getLocalStorage(key: string): Promise<string>{
return new Promise((resolve, reject) => {
console.log("get", key);
// @ts-ignore
chrome.storage.local.get([key], async function (items: { [x: string]: string; }) {
if(chrome.runtime.lastError) {
console.warn("Whoops.. " + chrome.runtime.lastError.message);
}
if(key in items){
resolve(items[key]);
} else {
Expand All @@ -17,8 +19,10 @@ export async function setLocalStorage(key: string, value: string){
console.log("set", key, value);
let obj: any = {};
obj[key] = value;
// @ts-ignore
chrome.storage.local.set(obj, function () {
if(chrome.runtime.lastError) {
console.warn("Whoops.. " + chrome.runtime.lastError.message);
}
resolve(undefined);
});
});
Expand All @@ -27,8 +31,10 @@ export async function setLocalStorage(key: string, value: string){
export async function unsetLocalStorage(key: string){
return new Promise((resolve, reject) => {
console.log("unset", key);
// @ts-ignore
chrome.storage.local.remove(key, function () {
if(chrome.runtime.lastError) {
console.warn("Whoops.. " + chrome.runtime.lastError.message);
}
resolve(undefined);
});
});
Expand Down
44 changes: 39 additions & 5 deletions extension/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import {
repoManagement,
makeType,
getValue,
getType
getType,
getHistory,
setHistory
} from "./helpers.js";

import {getLocalStorage } from "./localstorage.js";
import SuggestResult = chrome.omnibox.SuggestResult;

export async function main() {
// @ts-ignore
chrome.omnibox.onInputEntered.addListener(async (text, OnInputEnteredDisposition) => {
if(chrome.runtime.lastError) {
console.warn("Whoops.. " + chrome.runtime.lastError.message);
}

const query = text.split(" ");
if(query[0] === "set"){
await setCalled(query);
Expand All @@ -29,9 +35,37 @@ export async function main() {
let resp = await repoManagement(query);
console.log(resp);
} else {
const flightPlans = await getFlightPlans();
const flight = await getFlightFromQuery(query, flightPlans);
await handleFlight(flight, flightPlans);
const history = await getHistory(query);
setHistory(history).then(async () => {
const flightPlans = await getFlightPlans();
const flight = await getFlightFromQuery(query, flightPlans);
await handleFlight(flight, flightPlans);
});
}
});

chrome.omnibox.onInputChanged.addListener(async (text, suggest) => {
if(chrome.runtime.lastError) {
console.warn("Whoops.. " + chrome.runtime.lastError.message);
}
const query = text.split(" ");
if(!["set", "unset", "repo"].includes(query[0])){
const history = await getHistory([]);
const initialSuggestions = history.map((item: string) => {
if (item !== "" && item.toLowerCase().includes(text.toLowerCase())) {
return {
content: item,
description: item
};
}
});
const filtered = initialSuggestions.filter(function (x: any) {
return x !== undefined;
}) as SuggestResult[];

if(filtered.length > 0){
suggest(filtered);
}
}
});
}
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "Apollorion",
"license": "MIT",
"devDependencies": {
"@types/chrome": "^0.0.188",
"@types/jest": "^27.4.0",
"jest": "^27.5.1",
"ts-jest": "^27.1.3",
Expand All @@ -18,8 +19,12 @@
"dependencies": {},
"jest": {
"preset": "ts-jest",
"modulePathIgnorePatterns": ["dist"],
"extensionsToTreatAsEsm": [".ts"],
"modulePathIgnorePatterns": [
"dist"
],
"extensionsToTreatAsEsm": [
".ts"
],
"globals": {
"ts-jest": {
"useESM": true
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,38 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/chrome@^0.0.188":
version "0.0.188"
resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.188.tgz#ed73feca28a6d84f796ec50a2dbe7e41510175b3"
integrity sha512-fVxbcnSrQqCaaTFfaP9QooRr0Lf47Ni+QVGpd4SQafe6x8RlLrlp+AAgc4QKNUVK7W6xEoOePMAu5sBXrc2qhA==
dependencies:
"@types/filesystem" "*"
"@types/har-format" "*"

"@types/filesystem@*":
version "0.0.32"
resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.32.tgz#307df7cc084a2293c3c1a31151b178063e0a8edf"
integrity sha512-Yuf4jR5YYMR2DVgwuCiP11s0xuVRyPKmz8vo6HBY3CGdeMj8af93CFZX+T82+VD1+UqHOxTq31lO7MI7lepBtQ==
dependencies:
"@types/filewriter" "*"

"@types/filewriter@*":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee"
integrity sha512-BsPXH/irW0ht0Ji6iw/jJaK8Lj3FJemon2gvEqHKpCdDCeemHa+rI3WBGq5z7cDMZgoLjY40oninGxqk+8NzNQ==

"@types/graceful-fs@^4.1.2":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
integrity sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==
dependencies:
"@types/node" "*"

"@types/har-format@*":
version "1.2.8"
resolved "https://registry.yarnpkg.com/@types/har-format/-/har-format-1.2.8.tgz#e6908b76d4c88be3db642846bb8b455f0bfb1c4e"
integrity sha512-OP6L9VuZNdskgNN3zFQQ54ceYD8OLq5IbqO4VK91ORLfOm7WdT/CiT/pHEBSQEqCInJ2y3O6iCm/zGtPElpgJQ==

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"
Expand Down

0 comments on commit fc17338

Please sign in to comment.