Skip to content

Commit

Permalink
Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
notmarek committed Jul 13, 2023
1 parent 9755308 commit 224e473
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 20 deletions.
19 changes: 9 additions & 10 deletions static/content/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export let token = () => {
return `${localStorage.getItem("token_type")} ${token}`;
}


export const save_tokens_from_response = (res) => {
localStorage.setItem("token", res.token);
localStorage.setItem("refresh_token", res.refresh_token);
localStorage.setItem("token_type", res.token_type);
localStorage.setItem("token_exp", res.expiration);
}
export let submit = {
login: async (event) => {
event.preventDefault();
Expand All @@ -49,10 +54,7 @@ export let submit = {
let passwd = form.querySelector("input[name='password']").value;
let res = await user.login(uname, passwd);
if (res.status !== "error") {
localStorage.setItem("token", res.token);
localStorage.setItem("refresh_token", res.refresh_token);
localStorage.setItem("token_type", res.token_type);
localStorage.setItem("token_exp", res.expiration);
save_tokens_from_response(res);
await save_user_info();
navigate('/');
} else {
Expand All @@ -67,15 +69,12 @@ export let submit = {
let passwd = form.querySelector("input[name='password']").value;
let res = await user.register(uname, passwd);
if (res.status !== "error") {
localStorage.setItem("token", res.token);
localStorage.setItem("refresh_token", res.refresh_token);
localStorage.setItem("token_type", res.token_type);
localStorage.setItem("token_exp", res.expiration);
save_tokens_from_response(res);
await save_user_info();
navigate('/');
} else {
document.querySelector("#error").innerText = res.error;
}
return false;
}
}
}
7 changes: 7 additions & 0 deletions static/content/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export const user = {
noauth: 1,
})
.json(),
refresh_token: async (refresh_token) =>
await http
.post("/na/user", {
json: { refresh_token, identifier: "refresh_token" },
noauth: 1
})
.json(),
register: async (username, password) =>
await http
.put("/na/user", {
Expand Down
32 changes: 23 additions & 9 deletions static/content/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { submit, navigate, token, self } from "./api.js";
import { get_info } from "./api_client.js";
import { submit, navigate, token, self, save_tokens_from_response } from "./api.js";
import { get_info, user } from "./api_client.js";
import { ThemeManager } from "./theme.js";
console.log(await get_info());
window.submit = submit;
let path = window.location.pathname;
window.addEventListener("popstate", () => {
Expand All @@ -10,14 +9,16 @@ window.addEventListener("popstate", () => {
});
window.addEventListener(`click`, (e) => {
const origin = e.target.closest(`a`);
if (origin) {
if (origin && origin.href) {
e.preventDefault();
navigate(origin.href);
console.log(`Soft navigating to ${origin.href}.`);
return false;
}
});

const log = (src, msg) => {
return console.log.bind(console, `%c[${src}]`, `color: ${window.ThemeManager.style.accentColor}`)(msg);
}
const dbgr = async () => {
let container = document.createElement("div");

Expand All @@ -37,20 +38,23 @@ const dbgr = async () => {
let cache_misses = 0;
window.session.set = (k, v) => {
let r = set_og(k, v);
log("session.set", `Setting '${k}' to '${v}'`);
document.getElementById("dbgr-cache-size").innerHTML =
Object.keys(window.session).length - 2;
document.getElementById("dbgr-cache-inv").innerHTML = window.session.invalid.length;
return r;
};
let invalidate_og = window.session.invalidate;
window.session.invalidate = (k) => {
log("session.invalidate", `Invalidated '${k}'`);
let r = invalidate_og(k);
document.getElementById("dbgr-cache-inv").innerHTML = window.session.invalid.length;
return r;
}
let get_og = window.session.get;
window.session.get = (k) => {
let og = get_og(k);
log("session.get", `Cache ${og ? "hit" : "miss"} on key "${k}".`);
if (og) {
cache_hits++;
} else {
Expand All @@ -60,6 +64,11 @@ const dbgr = async () => {
document.getElementById("dbgr-cache-misses").innerHTML = cache_misses;
return og;
};
let setItem_og = window.localStorage.setItem;
localStorage.__proto__.setItem = (...args) => {
log("localStorage.setItem", `Set '${args[0]}' to '${args[1]}'`)
return setItem_og.apply(localStorage, args);
}

const modules_num = () => {
if (container.hidden) return;
Expand All @@ -68,10 +77,10 @@ const dbgr = async () => {
};
let interval = setInterval(modules_num, 300);
container.onclick = (e) => (
(container.hidden = 1), clearInterval(interval)
(log("dbgr", "Hiding debugger")), (container.hidden = 1), clearInterval(interval)
);
window.showDbgr = () => (
(container.hidden = false), (interval = setInterval(modules_num, 300))
(log("dbgr", "Showing debugger")), (container.hidden = false), (interval = setInterval(modules_num, 300))
);
document.body.appendChild(container);
};
Expand Down Expand Up @@ -165,7 +174,7 @@ let renderModule = (module_path, dom_id, variables = null) => {
);
mutated = mutated.replace(
"console.log",
`console.log.bind(console, "%c[Modules/${module_path}]", "color: #ff0069")`
`console.log.bind(console, "%c[Modules/${module_path}]", "color: ${window.ThemeManager.style.accentColor}")`
);
const blob = new Blob([mutated], {
type: "application/javascript",
Expand Down Expand Up @@ -247,8 +256,13 @@ const setup_theme_manager = () => {
window.ThemeManager = ThemeManager;
window.ThemeManager.init();
};

setup_theme_manager();
setup_storage();
await dbgr();
if (localStorage.getItem("refresh_token"))
user.refresh_token(localStorage.getItem("refresh_token")).then(res => {
if (res.status !== "error")
save_tokens_from_response(res);
}
);
router();
1 change: 1 addition & 0 deletions static/content/modules/folder/authenticated.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ <h2 class="secondary" id="libraryPath"></h2>
entry.path.split(".").pop().toLowerCase()
] || "File.svg";
let item = blueprint.cloneNode(true);
item.id = "";
item.style = null;
item.href =
(entry.folder ? "/folder/" : "/file/") + entry.id;
Expand Down
1 change: 1 addition & 0 deletions static/content/modules/home/authenticated.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h2 class="secondary">All you can eat.</h2>
for (const entry of libraries) {
console.log(entry);
let item = blueprint.cloneNode(true);
item.id = "";
item.style = null;
item.href = "/library/" + entry.id;
item.querySelector(".item-title>span#inner").innerText = entry.name;
Expand Down
1 change: 1 addition & 0 deletions static/content/modules/library/authenticated.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ <h2 class="secondary" id="libraryPath"></h2>
entry.path.split(".").pop().toLowerCase()
] || "File.svg";
let item = blueprint.cloneNode(true);
item.id = null;
item.style = null;
item.href =
(entry.folder ? "/folder/" : "/file/") + entry.id;
Expand Down
3 changes: 2 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/content/modules/style.css">
<link rel="stylesheet" preload href="/content/modules/style.css">
<meta name="theme-color" content="#ff0069">
<meta name="description" content="Some kind of library software">
<title>io</title>
<style id="default-theme">
:root { --accent-color: #ff006900;
Expand Down

0 comments on commit 224e473

Please sign in to comment.