pocketbase/ui/src/stores/collections.js

119 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-09-30 00:23:19 +08:00
import ApiClient from "@/utils/ApiClient";
2022-07-07 05:19:05 +08:00
import CommonHelper from "@/utils/CommonHelper";
2024-09-30 00:23:19 +08:00
import { get, writable } from "svelte/store";
2022-07-07 05:19:05 +08:00
2024-09-30 00:23:19 +08:00
export const collections = writable([]);
export const activeCollection = writable({});
export const isCollectionsLoading = writable(false);
2023-04-17 18:28:41 +08:00
export const protectedFilesCollectionsCache = writable({});
2024-09-30 00:23:19 +08:00
export const scaffolds = writable({});
2022-07-07 05:19:05 +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-15 18:27:42 +08:00
refreshProtectedFilesCollectionsCache();
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();
notifyOtherTabs();
2022-07-07 05:19:05 +08:00
return list;
});
}
2024-09-30 00:23:19 +08:00
// load all collections
2022-07-07 05:19:05 +08:00
export async function loadCollections(activeId = null) {
isCollectionsLoading.set(true);
try {
let items = await ApiClient.collections.getFullList(200, {
2023-02-21 22:32:58 +08:00
"sort": "+name",
})
2022-07-07 05:19:05 +08:00
items = CommonHelper.sortCollections(items);
2022-07-07 05:19:05 +08:00
collections.set(items);
const item = activeId && CommonHelper.findByKey(items, "id", activeId);
if (item) {
activeCollection.set(item);
} else if (items.length) {
2024-09-30 00:23:19 +08:00
activeCollection.set(items.find((f) => !f.system) || items[0]);
}
2023-04-15 18:27:42 +08:00
refreshProtectedFilesCollectionsCache();
2024-09-30 00:23:19 +08:00
scaffolds.set(await ApiClient.collections.getScaffolds());
} catch (err) {
2023-05-14 03:10:14 +08:00
ApiClient.error(err);
}
isCollectionsLoading.set(false);
2022-07-07 05:19:05 +08:00
}
2023-04-15 18:27:42 +08:00
function refreshProtectedFilesCollectionsCache() {
2023-04-17 18:28:41 +08:00
protectedFilesCollectionsCache.update((cache) => {
collections.update((current) => {
for (let c of current) {
2024-09-30 00:23:19 +08:00
cache[c.id] = !!c.fields?.find((f) => f.type == "file" && f.protected);
}
return current;
});
return cache;
});
}
2024-09-30 00:23:19 +08:00