Add path to bookmarks, add logout button
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import { writable } from "svelte/store"
|
||||
import { fs_check_response, fs_path_url } from "./FilesystemAPI"
|
||||
import { fs_check_response, fs_path_url, type FSNode } from "./FilesystemAPI"
|
||||
import { loading_finish, loading_start } from "lib/Loading"
|
||||
import { get_user } from "./PixeldrainAPI"
|
||||
|
||||
const bookmarks_file = "/me/.fnx/bookmarks.json"
|
||||
|
||||
export type Bookmark = {
|
||||
id: string,
|
||||
path: string,
|
||||
icon: string,
|
||||
label: string,
|
||||
}
|
||||
@@ -22,52 +24,88 @@ export let bookmarks_store = writable<Bookmark[]>(
|
||||
)
|
||||
|
||||
export const bookmarks_get = async (): Promise<Bookmark[]> => {
|
||||
let bookmarks: Bookmark[] = []
|
||||
try {
|
||||
bookmarks = await fs_check_response(
|
||||
loading_start()
|
||||
const user = await get_user()
|
||||
if (
|
||||
user.username === undefined ||
|
||||
user.username === "" ||
|
||||
user.subscription.filesystem_access === undefined ||
|
||||
user.subscription.filesystem_access === false
|
||||
) {
|
||||
return []
|
||||
}
|
||||
|
||||
const bookmarks = await fs_check_response(
|
||||
await fetch(fs_path_url(bookmarks_file), { cache: "no-store" })
|
||||
)
|
||||
) as Bookmark[]
|
||||
|
||||
// Sanity checks
|
||||
for (const bookmark of bookmarks) {
|
||||
if (typeof bookmark.id !== "string") { bookmark.id = "" }
|
||||
if (typeof bookmark.path !== "string") { bookmark.path = "" }
|
||||
if (typeof bookmark.icon !== "string") { bookmark.icon = "" }
|
||||
if (typeof bookmark.label !== "string") { bookmark.label = "" }
|
||||
}
|
||||
|
||||
console.debug("Fetched", bookmarks.length, "bookmarks:", bookmarks)
|
||||
bookmarks_store.set(bookmarks)
|
||||
return bookmarks
|
||||
} catch (err) {
|
||||
// If the bookmarks were not found when we return an empty bookmarks
|
||||
// list
|
||||
if (err.value !== "path_not_found") {
|
||||
if (
|
||||
err.value !== "path_not_found" &&
|
||||
err.value !== "forbidden" &&
|
||||
err.value !== "authentication_required"
|
||||
) {
|
||||
throw err
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
} finally {
|
||||
loading_finish()
|
||||
}
|
||||
|
||||
console.debug("Fetched", bookmarks.length, "bookmarks:", bookmarks)
|
||||
bookmarks_store.set(bookmarks)
|
||||
return bookmarks
|
||||
}
|
||||
|
||||
export const bookmarks_save = async (bookmarks: Bookmark[]) => {
|
||||
await fs_check_response(
|
||||
await fetch(
|
||||
fs_path_url(bookmarks_file) + "?make_parents=true",
|
||||
{ method: "PUT", body: JSON.stringify(bookmarks) },
|
||||
try {
|
||||
loading_start()
|
||||
|
||||
await fs_check_response(
|
||||
await fetch(
|
||||
fs_path_url(bookmarks_file) + "?make_parents=true",
|
||||
{ method: "PUT", body: JSON.stringify(bookmarks) },
|
||||
)
|
||||
)
|
||||
)
|
||||
console.debug("Saved", bookmarks.length, "bookmarks:", bookmarks)
|
||||
bookmarks_store.set(bookmarks)
|
||||
console.debug("Saved", bookmarks.length, "bookmarks:", bookmarks)
|
||||
bookmarks_store.set(bookmarks)
|
||||
} finally {
|
||||
loading_finish()
|
||||
}
|
||||
}
|
||||
|
||||
export const bookmark_add = async (bm: Bookmark): Promise<Bookmark[]> => {
|
||||
let bookmarks: Bookmark[] = []
|
||||
export const bookmark_add = async (node: FSNode): Promise<Bookmark[]> => {
|
||||
try {
|
||||
loading_start()
|
||||
|
||||
// Get bookmarks
|
||||
bookmarks = await bookmarks_get()
|
||||
const bookmarks = await bookmarks_get()
|
||||
|
||||
// Add new bookmark
|
||||
bookmarks.push(bm)
|
||||
bookmarks.push({
|
||||
id: node.id,
|
||||
path: node.path,
|
||||
icon: "bookmark",
|
||||
label: node.name,
|
||||
})
|
||||
|
||||
// Save new bookmarks
|
||||
await bookmarks_save(bookmarks)
|
||||
return bookmarks
|
||||
} finally {
|
||||
loading_finish()
|
||||
}
|
||||
return bookmarks
|
||||
}
|
||||
|
||||
export const bookmark_del = async (id: string): Promise<Bookmark[]> => {
|
||||
@@ -94,3 +132,12 @@ export const bookmark_del = async (id: string): Promise<Bookmark[]> => {
|
||||
}
|
||||
return bookmarks
|
||||
}
|
||||
|
||||
export const is_bookmark = (bookmarks: Bookmark[], id: string): boolean => {
|
||||
for (const bm of bookmarks) {
|
||||
if (bm.id === id) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user