UI overhaul, import fixes, fix user store

This commit is contained in:
2026-07-30 17:07:46 +02:00
parent 773149169b
commit 4b8bd11a4c
75 changed files with 1106 additions and 744 deletions

View File

@@ -1,6 +1,8 @@
// Response types
// ==============
import { user } from "./UserStore"
export type GenericResponse = {
value: string,
message: string,
@@ -35,14 +37,11 @@ export type Subscription = {
name: string,
type: string,
file_size_limit: number,
file_expiry_days: number,
storage_space: number,
storage_limit: number,
price_per_tb_storage: number,
price_per_tb_bandwidth: number,
monthly_transfer_cap: number,
file_viewer_branding: boolean,
filesystem_access: boolean,
filesystem_storage_limit: number,
file_sharing: boolean,
}
// Utility funcs
@@ -107,34 +106,66 @@ export type UserSession = {
valid_domains: string[],
}
let cached_user: User = null
// If cached_user is undefined it means that the value is not initialized yet,
// null means the user is not logged in
let cached_user: User = undefined
// The request which is currently in flight. Callers which arrive while the
// request is still running share this promise, so the user is only requested
// from the server once
let user_request: Promise<User> | null = null
export const get_user = async (): Promise<User> => {
if ((window as any).user !== undefined) {
return (window as any).user as User
} else if (cached_user !== null) {
if (cached_user !== undefined) {
return cached_user
}
if (user_request === null) {
user_request = request_user()
}
console.warn("user property is not defined on window")
const u = await user_request
if (u === undefined) {
// The request failed. Clear it so the next caller tries again
user_request = null
}
cached_user = await check_response(await fetch(get_endpoint() + "/user")) as User
return u
}
const request_user = async (): Promise<User> => {
try {
let resp = await fetch(get_endpoint() + "/user")
if (resp.status === 401) {
// User is not authenticated
cached_user = null
} else if (resp.status >= 400) {
throw await resp.text()
} else {
cached_user = (await resp.json()) as User
}
} catch (err) {
alert("failed to fetch user:\n" + err)
}
user.set(cached_user)
return cached_user
}
export const put_user = async (data: Object) => {
check_response(await fetch(
await check_response(await fetch(
get_endpoint() + "/user",
{ method: "PUT", body: dict_to_form(data) },
))
// Update the window.user variable
// Update the user store
for (let key of Object.keys(data)) {
((window as any).user as User)[key] = data[key]
cached_user[key] = data[key]
}
user.set(cached_user)
}
export const logout_user = async (redirect_path: string) => {
check_response(await fetch(
await check_response(await fetch(
get_endpoint() + "/user/session",
{ method: "DELETE" },
))