diff --git a/src/components/settings/tabs/changelog/changelogManager.ts b/src/components/settings/tabs/changelog/changelogManager.ts index f61eb710b..406426633 100644 --- a/src/components/settings/tabs/changelog/changelogManager.ts +++ b/src/components/settings/tabs/changelog/changelogManager.ts @@ -24,7 +24,7 @@ export interface UpdateSession { commits: ChangelogEntry[]; newPlugins: string[]; updatedPlugins: string[]; - newSettings?: Map; + newSettings?: Map | Record; type: "update" | "repository_fetch"; } @@ -38,10 +38,45 @@ const LAST_REPO_CHECK_KEY = "EquicordChangelog_LastRepoCheck"; type KnownPluginSettingsMap = Map>; +function isMapLike(value: any): value is Map { + return ( + value && + typeof value.get === "function" && + typeof value.size === "number" + ); +} + +export function getNewSettingsSize( + newSettings: Map | Record | undefined, +): number { + if (!newSettings) return 0; + if (isMapLike(newSettings)) return newSettings.size; + return Object.keys(newSettings).length; +} + +export function getNewSettingsEntries( + newSettings: Map | Record | undefined, +): [string, string[]][] { + if (!newSettings) return []; + if (isMapLike(newSettings)) return Array.from(newSettings.entries()); + return Object.entries(newSettings); +} + export async function getChangelogHistory(): Promise { const history = (await DataStore.get( CHANGELOG_HISTORY_KEY, )) as ChangelogHistory; + + if (history) { + history.forEach(session => { + if (session.newSettings && !(session.newSettings instanceof Map)) { + session.newSettings = new Map( + Object.entries(session.newSettings), + ); + } + }); + } + return history || []; } @@ -74,7 +109,7 @@ export async function saveUpdateSession( commits.length === 0 && newPlugins.length === 0 && updatedPlugins.length === 0 && - newSettings.size === 0 + getNewSettingsSize(newSettings) === 0 ) { return; } @@ -106,7 +141,10 @@ export async function saveUpdateSession( commits, newPlugins, updatedPlugins, - newSettings, + newSettings: + getNewSettingsSize(newSettings) > 0 + ? Object.fromEntries(newSettings) + : undefined, type: sessionType, }; @@ -165,14 +203,27 @@ function getCurrentSettings(pluginList: string[]): KnownPluginSettingsMap { } export async function getKnownSettings(): Promise { - let map = (await DataStore.get( - KNOWN_SETTINGS_KEY, - )) as KnownPluginSettingsMap; - if (map === undefined) { + const mapData = (await DataStore.get(KNOWN_SETTINGS_KEY)) as any; + let map: KnownPluginSettingsMap; + + if (mapData === undefined) { const knownPlugins = await getKnownPlugins(); const Plugins = [...Object.keys(plugins), ...Array.from(knownPlugins)]; map = getCurrentSettings(Plugins); await DataStore.set(KNOWN_SETTINGS_KEY, map); + } else { + // convert the plain object back to a map with set value(s) + map = new Map(); + if (mapData && typeof mapData === "object") { + for (const [plugin, settings] of Object.entries(mapData)) { + if (Array.isArray(settings)) { + map.set(plugin, new Set(settings)); + } else if (settings && typeof settings === "object") { + // should fix serialization issues + map.set(plugin, new Set(Object.values(settings))); + } + } + } } return map; } @@ -211,7 +262,15 @@ export async function updateKnownSettings(): Promise { }, ); - await DataStore.set(KNOWN_SETTINGS_KEY, allSettings); + // convert map to serialized storage format + const serializableSettings = Object.fromEntries( + Array.from(allSettings.entries()).map(([plugin, settings]) => [ + plugin, + Array.from(settings), + ]), + ); + + await DataStore.set(KNOWN_SETTINGS_KEY, serializableSettings); } export async function getNewPlugins(): Promise { diff --git a/src/components/settings/tabs/changelog/index.tsx b/src/components/settings/tabs/changelog/index.tsx index ee4519649..22ee5dfc6 100644 --- a/src/components/settings/tabs/changelog/index.tsx +++ b/src/components/settings/tabs/changelog/index.tsx @@ -27,6 +27,8 @@ import { getLastRepositoryCheckHash, getNewPlugins, getNewSettings, + getNewSettingsEntries, + getNewSettingsSize, getUpdatedPlugins, initializeChangelog, saveUpdateSession, @@ -154,8 +156,8 @@ function UpdateLogCard({ {log.updatedPlugins.length > 0 && ` • ${log.updatedPlugins.length} updated plugins`} {log.newSettings && - log.newSettings.size > 0 && - ` • ${Array.from(log.newSettings.values()).reduce((sum, arr) => sum + arr.length, 0)} new settings`} + getNewSettingsSize(log.newSettings) > 0 && + ` • ${getNewSettingsEntries(log.newSettings).reduce((sum, [, arr]) => sum + arr.length, 0)} new settings`}
)} - {log.newSettings && log.newSettings.size > 0 && ( -
- - New Settings - -
- {Array.from( - log.newSettings?.entries() || [], - ).map(([pluginName, settings]) => - settings.map(setting => ( - - {pluginName}.{setting} - - )), - )} + {log.newSettings && + getNewSettingsSize(log.newSettings) > 0 && ( +
+ + New Settings + +
+ {getNewSettingsEntries(log.newSettings).map( + ([pluginName, settings]) => + settings.map(setting => ( + + {pluginName}.{setting} + + )), + )} +
-
- )} + )} {log.commits.length > 0 && (
@@ -348,7 +350,6 @@ function ChangelogContent() { return; } - if (updates.ok && updates.value) { setChangelog(updates.value);