2024-02-03 21:39:09 +08:00
|
|
|
import { writable, get } from "svelte/store";
|
2022-07-07 05:19:05 +08:00
|
|
|
import ApiClient from "@/utils/ApiClient";
|
|
|
|
import CommonHelper from "@/utils/CommonHelper";
|
|
|
|
|
2023-04-17 18:28:41 +08:00
|
|
|
export const collections = writable([]);
|
|
|
|
export const activeCollection = writable({});
|
|
|
|
export const isCollectionsLoading = writable(false);
|
|
|
|
export const protectedFilesCollectionsCache = writable({});
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2024-02-03 21:39:09 +08:00
|
|
|
let notifyChannel;
|
|
|
|
|
|
|
|
if (typeof BroadcastChannel != "undefined") {
|
|
|
|
notifyChannel = new BroadcastChannel("collections");
|
|
|
|
|
|
|
|
notifyChannel.onmessage = () => {
|
|
|
|
loadCollections(get(activeCollection)?.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyOtherTabs() {
|
|
|
|
notifyChannel?.postMessage("reload");
|
|
|
|
}
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
export function changeActiveCollectionById(collectionId) {
|
|
|
|
collections.update((list) => {
|
|
|
|
const found = CommonHelper.findByKey(list, "id", collectionId);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
activeCollection.set(found);
|
|
|
|
} else if (list.length) {
|
|
|
|
activeCollection.set(list[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
// add or update collection
|
|
|
|
export function addCollection(collection) {
|
|
|
|
activeCollection.update((current) => {
|
|
|
|
return CommonHelper.isEmpty(current?.id) || current.id === collection.id ? collection : current;
|
|
|
|
});
|
|
|
|
|
|
|
|
collections.update((list) => {
|
|
|
|
CommonHelper.pushOrReplaceByKey(list, collection, "id");
|
2023-04-05 18:23:22 +08:00
|
|
|
|
2023-04-15 18:27:42 +08:00
|
|
|
refreshProtectedFilesCollectionsCache();
|
2023-04-05 18:23:22 +08:00
|
|
|
|
2024-02-03 21:39:09 +08:00
|
|
|
notifyOtherTabs();
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
return CommonHelper.sortCollections(list);
|
2022-07-07 05:19:05 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeCollection(collection) {
|
|
|
|
collections.update((list) => {
|
|
|
|
CommonHelper.removeByKey(list, "id", collection.id);
|
|
|
|
|
|
|
|
activeCollection.update((current) => {
|
|
|
|
if (current.id === collection.id) {
|
2022-10-30 16:28:14 +08:00
|
|
|
return list[0];
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
});
|
|
|
|
|
2023-04-15 18:27:42 +08:00
|
|
|
refreshProtectedFilesCollectionsCache();
|
2023-04-05 18:23:22 +08:00
|
|
|
|
2024-02-03 21:39:09 +08:00
|
|
|
notifyOtherTabs();
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
return list;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// load all collections (excluding the user profile)
|
|
|
|
export async function loadCollections(activeId = null) {
|
|
|
|
isCollectionsLoading.set(true);
|
|
|
|
|
2023-01-24 03:57:35 +08:00
|
|
|
try {
|
|
|
|
let items = await ApiClient.collections.getFullList(200, {
|
2023-02-21 22:32:58 +08:00
|
|
|
"sort": "+name",
|
2023-01-24 03:57:35 +08:00
|
|
|
})
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-01-24 03:57:35 +08:00
|
|
|
items = CommonHelper.sortCollections(items);
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-01-24 03:57:35 +08:00
|
|
|
collections.set(items);
|
|
|
|
|
|
|
|
const item = activeId && CommonHelper.findByKey(items, "id", activeId);
|
|
|
|
if (item) {
|
|
|
|
activeCollection.set(item);
|
|
|
|
} else if (items.length) {
|
|
|
|
activeCollection.set(items[0]);
|
|
|
|
}
|
2023-04-05 18:23:22 +08:00
|
|
|
|
2023-04-15 18:27:42 +08:00
|
|
|
refreshProtectedFilesCollectionsCache();
|
2023-01-24 03:57:35 +08:00
|
|
|
} catch (err) {
|
2023-05-14 03:10:14 +08:00
|
|
|
ApiClient.error(err);
|
2023-01-24 03:57:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
isCollectionsLoading.set(false);
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
2023-04-05 18:23:22 +08:00
|
|
|
|
2023-04-15 18:27:42 +08:00
|
|
|
function refreshProtectedFilesCollectionsCache() {
|
2023-04-17 18:28:41 +08:00
|
|
|
protectedFilesCollectionsCache.update((cache) => {
|
2023-04-05 18:23:22 +08:00
|
|
|
collections.update((current) => {
|
|
|
|
for (let c of current) {
|
2023-04-15 18:27:42 +08:00
|
|
|
cache[c.id] = !!c.schema?.find((f) => f.type == "file" && f.options?.protected);
|
2023-04-05 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
});
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
});
|
|
|
|
}
|