fix changelog error "t.get is not a function" (#418)

* preserve Map methods by adding proper serialization in DataStore

* Formatting

---------

Co-authored-by: thororen1234 <78185467+thororen1234@users.noreply.github.com>
Co-authored-by: justjxke <Jake0300@protonmai.com>
This commit is contained in:
Jake 2025-09-27 16:17:09 +01:00 • committed by GitHub
parent e29fe9c1ae
commit d9e30a7b44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 95 additions and 35 deletions

View file

@ -24,7 +24,7 @@ export interface UpdateSession {
commits: ChangelogEntry[];
newPlugins: string[];
updatedPlugins: string[];
newSettings?: Map<string, string[]>;
newSettings?: Map<string, string[]> | Record<string, string[]>;
type: "update" | "repository_fetch";
}
@ -38,10 +38,45 @@ const LAST_REPO_CHECK_KEY = "EquicordChangelog_LastRepoCheck";
type KnownPluginSettingsMap = Map<string, Set<string>>;
function isMapLike(value: any): value is Map<string, string[]> {
return (
value &&
typeof value.get === "function" &&
typeof value.size === "number"
);
}
export function getNewSettingsSize(
newSettings: Map<string, string[]> | Record<string, string[]> | undefined,
): number {
if (!newSettings) return 0;
if (isMapLike(newSettings)) return newSettings.size;
return Object.keys(newSettings).length;
}
export function getNewSettingsEntries(
newSettings: Map<string, string[]> | Record<string, string[]> | undefined,
): [string, string[]][] {
if (!newSettings) return [];
if (isMapLike(newSettings)) return Array.from(newSettings.entries());
return Object.entries(newSettings);
}
export async function getChangelogHistory(): Promise<ChangelogHistory> {
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<KnownPluginSettingsMap> {
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<void> {
},
);
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<string[]> {

View file

@ -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`}
</div>
</div>
<div
@ -191,31 +193,31 @@ function UpdateLogCard({
</div>
)}
{log.newSettings && log.newSettings.size > 0 && (
<div className="vc-changelog-log-plugins">
<Forms.FormTitle
tag="h6"
className={Margins.bottom8}
>
New Settings
</Forms.FormTitle>
<div className="vc-changelog-new-plugins-list">
{Array.from(
log.newSettings?.entries() || [],
).map(([pluginName, settings]) =>
settings.map(setting => (
<span
key={`${pluginName}-${setting}`}
className="vc-changelog-new-plugin-tag"
title={`New setting in ${pluginName}`}
>
{pluginName}.{setting}
</span>
)),
)}
{log.newSettings &&
getNewSettingsSize(log.newSettings) > 0 && (
<div className="vc-changelog-log-plugins">
<Forms.FormTitle
tag="h6"
className={Margins.bottom8}
>
New Settings
</Forms.FormTitle>
<div className="vc-changelog-new-plugins-list">
{getNewSettingsEntries(log.newSettings).map(
([pluginName, settings]) =>
settings.map(setting => (
<span
key={`${pluginName}-${setting}`}
className="vc-changelog-new-plugin-tag"
title={`New setting in ${pluginName}`}
>
{pluginName}.{setting}
</span>
)),
)}
</div>
</div>
</div>
)}
)}
{log.commits.length > 0 && (
<div className="vc-changelog-log-commits">
@ -348,7 +350,6 @@ function ChangelogContent() {
return;
}
if (updates.ok && updates.value) {
setChangelog(updates.value);