From 6ddf20360909b80610c82ef139bcdd7f9a1cf33a Mon Sep 17 00:00:00 2001 From: Elvyra <88881326+EepyElvyra@users.noreply.github.com> Date: Sun, 19 Jan 2025 19:40:53 +0100 Subject: [PATCH] revert the changes that migrate datastore to array settings --- src/plugins/messageTags/index.ts | 33 ++++++++++++--------- src/plugins/pinDms/data.ts | 20 ++++--------- src/plugins/pinDms/index.tsx | 7 +---- src/plugins/textReplace/index.tsx | 48 +++++++++---------------------- 4 files changed, 40 insertions(+), 68 deletions(-) diff --git a/src/plugins/messageTags/index.ts b/src/plugins/messageTags/index.ts index 65699541d..50bb62665 100644 --- a/src/plugins/messageTags/index.ts +++ b/src/plugins/messageTags/index.ts @@ -34,10 +34,10 @@ interface Tag { const getTag = (name: string) => settings.store.data.find((tt: Tag) => tt.name === name); const addTag = (tag: Tag) => { - settings.store.data = [...settings.store.data, tag]; + settings.store.data.push(tag); }; const removeTag = (name: string) => { - settings.store.data = settings.store.data.filter((t: Tag) => t.name !== name); + settings.store.data.filter((t: Tag) => t.name !== name); }; function createTagCommand(tag: Tag) { @@ -64,16 +64,17 @@ function createTagCommand(tag: Tag) { const settings = definePluginSettings({ - clyde: { - description: "If enabled, clyde will send you an ephemeral message when a tag was used.", - type: OptionType.BOOLEAN, - default: true - }, data: { type: OptionType.ARRAY, hidden: true, description: "" }, + migrated: { + type: OptionType.BOOLEAN, + description: "", + default: false, + hidden: true + } }); @@ -81,16 +82,22 @@ export default definePlugin({ name: "MessageTags", description: "Allows you to save messages and to use them with a simple command.", authors: [Devs.Luna], + options: { + clyde: { + name: "Clyde message on send", + description: "If enabled, clyde will send you an ephemeral message when a tag was used.", + type: OptionType.BOOLEAN, + default: true + } + }, settings, async start() { - const data = await DataStore.get(DATA_KEY); - - if (data != null) { - settings.store.data = data; - await DataStore.del(DATA_KEY); + if (!settings.store.migrated) { + const data = await DataStore.get(DATA_KEY); + if (!!data) settings.store.data = data; + settings.store.migrated = true; } - for (const tag of settings.store.data) createTagCommand(tag); }, diff --git a/src/plugins/pinDms/data.ts b/src/plugins/pinDms/data.ts index 8adc2dbfc..a4e40dde0 100644 --- a/src/plugins/pinDms/data.ts +++ b/src/plugins/pinDms/data.ts @@ -28,18 +28,19 @@ const OLD_CATEGORY_KEY = "BetterPinDMsCategories-"; export let categories: Category[] = []; export async function saveCats(cats: Category[]) { - settings.store.data = cats; + const { id } = UserStore.getCurrentUser(); + await DataStore.set(CATEGORY_BASE_KEY + id, cats); } export async function init() { const id = UserStore.getCurrentUser()?.id; - await initCategories(); + await initCategories(id); await migrateData(id); forceUpdate(); } -export async function initCategories() { - categories = settings.store.data ?? []; +export async function initCategories(userId: string) { + categories = await DataStore.get(CATEGORY_BASE_KEY + userId) ?? []; } export function getCategory(id: string) { @@ -206,22 +207,13 @@ 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 await migrateFromDatastore(userId); - } + if (m1 && m2) return; // 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 a7abcb8d6..fce725cab 100644 --- a/src/plugins/pinDms/index.tsx +++ b/src/plugins/pinDms/index.tsx @@ -54,12 +54,7 @@ export const settings = definePluginSettings({ description: "Collapse DM sections", default: false, onChange: () => forceUpdate() - }, - data: { - type: OptionType.ARRAY, - hidden: true, - description: "", - }, + } }); export default definePlugin({ diff --git a/src/plugins/textReplace/index.tsx b/src/plugins/textReplace/index.tsx index f5426dc4c..615477d07 100644 --- a/src/plugins/textReplace/index.tsx +++ b/src/plugins/textReplace/index.tsx @@ -46,6 +46,9 @@ const makeEmptyRule: () => Rule = () => ({ }); const makeEmptyRuleArray = () => [makeEmptyRule()]; +let stringRules = makeEmptyRuleArray(); +let regexRules = makeEmptyRuleArray(); + const settings = definePluginSettings({ replace: { type: OptionType.COMPONENT, @@ -56,13 +59,13 @@ const settings = definePluginSettings({ <> @@ -71,16 +74,6 @@ const settings = definePluginSettings({ ); } }, - stringRules: { - type: OptionType.ARRAY, - hidden: true, - description: "" - }, - regexRules: { - type: OptionType.ARRAY, - hidden: true, - description: "" - }, }); function stringToRegex(str: string) { @@ -134,8 +127,7 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps) if (index === rulesArray.length - 1) return; rulesArray.splice(index, 1); - if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray]; - else settings.store.regexRules = [...rulesArray]; + await DataStore.set(rulesKey, rulesArray); update(); } @@ -148,8 +140,7 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps) if (rulesArray[index].find === "" && rulesArray[index].replace === "" && rulesArray[index].onlyIfIncludes === "" && index !== rulesArray.length - 1) rulesArray.splice(index, 1); - if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray]; - else settings.store.regexRules = [...rulesArray]; + await DataStore.set(rulesKey, rulesArray); update(); } @@ -220,8 +211,8 @@ function applyRules(content: string): string { if (content.length === 0) return content; - if (settings.store.stringRules) { - for (const rule of settings.store.stringRules) { + if (stringRules) { + for (const rule of stringRules) { if (!rule.find) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; @@ -229,8 +220,8 @@ function applyRules(content: string): string { } } - if (settings.store.regexRules) { - for (const rule of settings.store.regexRules) { + if (regexRules) { + for (const rule of regexRules) { if (!rule.find) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; @@ -258,21 +249,8 @@ export default definePlugin({ settings, async start() { - - if (settings.store.stringRules.length === 0 || settings.store.regexRules.length === 0) { - const stringRules = await DataStore.get(STRING_RULES_KEY); - const regexRules = await DataStore.get(REGEX_RULES_KEY); - - if (stringRules != null) { - settings.store.stringRules = stringRules; - await DataStore.del(STRING_RULES_KEY); - } else if (settings.store.stringRules.length === 0) settings.store.stringRules = makeEmptyRuleArray(); - - if (regexRules != null) { - settings.store.regexRules = regexRules; - await DataStore.del(REGEX_RULES_KEY); - } else if (settings.store.regexRules.length === 0) settings.store.regexRules = makeEmptyRuleArray(); - } + stringRules = await DataStore.get(STRING_RULES_KEY) ?? makeEmptyRuleArray(); + regexRules = await DataStore.get(REGEX_RULES_KEY) ?? makeEmptyRuleArray(); this.preSend = addPreSendListener((channelId, msg) => { // Channel used for sharing rules, applying rules here would be messy