From ff95a51dd7aa00ee6a7fdf328ab99d787821609b Mon Sep 17 00:00:00 2001 From: Elvy <88881326+EepyElvyra@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:14:44 +0100 Subject: [PATCH] update pindms --- src/plugins/pinDms/data.ts | 20 ++++++++++++++------ src/plugins/pinDms/index.tsx | 7 ++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/plugins/pinDms/data.ts b/src/plugins/pinDms/data.ts index a4e40dde0..f15b69480 100644 --- a/src/plugins/pinDms/data.ts +++ b/src/plugins/pinDms/data.ts @@ -28,19 +28,18 @@ const OLD_CATEGORY_KEY = "BetterPinDMsCategories-"; export let categories: Category[] = []; export async function saveCats(cats: Category[]) { - const { id } = UserStore.getCurrentUser(); - await DataStore.set(CATEGORY_BASE_KEY + id, cats); + settings.store.data = cats; } export async function init() { const id = UserStore.getCurrentUser()?.id; - await initCategories(id); + await initCategories(); await migrateData(id); forceUpdate(); } -export async function initCategories(userId: string) { - categories = await DataStore.get(CATEGORY_BASE_KEY + userId) ?? []; +export async function initCategories() { + categories = settings.store.data; } export function getCategory(id: string) { @@ -207,13 +206,22 @@ async function migrateOldCategories(userId: string) { await DataStore.set(CATEGORY_MIGRATED_KEY, true); } +async function migrateFromDatastore(userId: string) { + const cats = await DataStore.get(CATEGORY_BASE_KEY + userId); + if (cats !== undefined) settings.store.data = cats; +} + export async function migrateData(userId: string) { + if (settings.store.data.length > 0) return; const m1 = await DataStore.get(CATEGORY_MIGRATED_KEY), m2 = await DataStore.get(CATEGORY_MIGRATED_PINDMS_KEY); - if (m1 && m2) return; + if (m1 && m2) { + return await migrateFromDatastore(userId); + } // want to migrate the old categories first and then slove any conflicts with the PinDMs pins if (!m1) await migrateOldCategories(userId); if (!m2) await migratePinDMs(); + if (settings.store.data.length === 0) await migrateFromDatastore(userId); await saveCats(categories); } diff --git a/src/plugins/pinDms/index.tsx b/src/plugins/pinDms/index.tsx index 3e2044116..d10937997 100644 --- a/src/plugins/pinDms/index.tsx +++ b/src/plugins/pinDms/index.tsx @@ -55,7 +55,12 @@ export const settings = definePluginSettings({ description: "Collapse DM sections", default: false, onChange: () => forceUpdate() - } + }, + data: { + type: OptionType.ARRAY, + hidden: true, + description: "", + }, }); export default definePlugin({