Ensure sections edited from the configuration popover on the home screen correctly sync to the native jellyfin homeSectionN prefs

This commit is contained in:
HighImKevin
2026-10-18 11:23:34 -04:00
parent 3ab92d5cb4
commit dfdaeacaa3
2 changed files with 90 additions and 2 deletions
@@ -1628,6 +1628,85 @@
} }
} }
/**
* Persist kefinTweaks.homeScreen and rewrite Jellyfin homesection0–9 to match
* (same outbound pairing as saveUserPreferences / getConfig). Single displayPrefs write.
* Used by the home section configure popover after order/enable changes.
* @param {Object} homeScreen
* @returns {Promise<boolean>}
*/
async function saveKefinTweaksHomeScreenAndSyncNative(homeScreen) {
if (!window.userHelper?.getUserDisplayPreferences && !window.userHelper?.getUserDisplayPreferencesForUser) {
ERR('userHelper not available for homeScreen + native sync save');
return false;
}
try {
const displayPrefs = await getEditorDisplayPreferences();
if (!displayPrefs) {
ERR('Could not load display preferences for homeScreen + native sync');
return false;
}
if (!displayPrefs.CustomPrefs) displayPrefs.CustomPrefs = {};
const sanitized = sanitizeHomeScreenSections(homeScreen).homeScreen;
const kefin = typeof window.userHelper?.parseKefinTweaks === 'function'
? window.userHelper.parseKefinTweaks(displayPrefs)
: {};
kefin.homeScreen = sanitized;
displayPrefs.CustomPrefs.kefinTweaks = JSON.stringify(kefin);
displayPrefs.CustomPrefs.kefinHomeScreen = JSON.stringify(homeScreenToLegacyArray(sanitized));
// Build effective sections for slot packing (no inbound native→Kefin enable pass)
let sections = [];
const sectionsApi = window.KefinHomeScreen?.getSections;
if (typeof sectionsApi === 'function') {
const sectionsResult = await Promise.resolve(sectionsApi());
const { enabledHomeSections = [] } = sectionsResult || {};
const serverSections = [...enabledHomeSections];
const pinnedSections = buildPinnedSectionConfigs(sanitized);
const serverSectionsById = await buildServerSectionsById();
serverSections.forEach((section) => {
if (section?.id && !serverSectionsById.has(section.id)) {
serverSectionsById.set(section.id, section);
}
});
sections = deduplicateHomeScreenSections([...serverSections, ...pinnedSections]);
sections = applyUserSectionOverrides(sections, sanitized, { serverSectionsById });
try {
sections = await resolveRecentlyAddedSectionsForUser(sections);
sections = applyUserSectionOverrides(sections, sanitized, { serverSectionsById });
} catch (e) {
WARN('resolveRecentlyAddedSectionsForUser failed during native sync save:', e);
}
sections = sections.filter((section) => (
section.enabled
|| section.userConfigurable === true
|| section.userConfigurable === undefined
));
sections = deduplicateHomeScreenSections(sections);
} else {
WARN('KefinHomeScreen.getSections unavailable; saving homeScreen without native slot rebuild');
}
if (sections.length) {
syncNativeHomeSectionsFromKefin(
sections,
displayPrefs.CustomPrefs,
sanitized,
{ syncUi: false }
);
}
const ok = await saveEditorDisplayPreferences(displayPrefs);
if (ok) LOG('kefinTweaks homeScreen saved (+ native homesectionN sync)');
else ERR('Failed to save homeScreen + native sync');
return ok;
} catch (e) {
ERR('Error saving homeScreen + syncing native homesectionN:', e);
return false;
}
}
/** /**
* Persist items layout UI preference for a section (row|grid). * Persist items layout UI preference for a section (row|grid).
* Home / seasonal / discovery → kefinTweaks.homeScreen; otherwise → kefinTweaks.sectionState. * Home / seasonal / discovery → kefinTweaks.homeScreen; otherwise → kefinTweaks.sectionState.
@@ -2739,6 +2818,7 @@
applyUserSectionOverrides, applyUserSectionOverrides,
buildPinnedSectionConfigs, buildPinnedSectionConfigs,
saveKefinTweaksHomeScreen, saveKefinTweaksHomeScreen,
saveKefinTweaksHomeScreenAndSyncNative,
loadKefinTweaksHomeScreenFromDisplayPrefs, loadKefinTweaksHomeScreenFromDisplayPrefs,
createEmptyHomeScreen, createEmptyHomeScreen,
parsePinnedListString, parsePinnedListString,
+10 -2
View File
@@ -180,6 +180,14 @@
return window.KefinUserHomeScreenConfig; return window.KefinUserHomeScreenConfig;
} }
/** Persist homeScreen and rewrite Jellyfin homesectionN when the syncing API is available. */
async function saveHomeScreenPreferNativeSync(api, homeScreen) {
if (typeof api?.saveKefinTweaksHomeScreenAndSyncNative === 'function') {
return api.saveKefinTweaksHomeScreenAndSyncNative(homeScreen);
}
return api.saveKefinTweaksHomeScreen(homeScreen);
}
function getCachePresetOptions() { function getCachePresetOptions() {
const presets = window.KefinHomeScreenAdvancedEditor?.getCachePresets?.() const presets = window.KefinHomeScreenAdvancedEditor?.getCachePresets?.()
|| buildFallbackCachePresets(); || buildFallbackCachePresets();
@@ -624,7 +632,7 @@
); );
homeScreen = api.upsertSectionPref(homeScreen, prefStr); homeScreen = api.upsertSectionPref(homeScreen, prefStr);
pendingHomeScreen = homeScreen; pendingHomeScreen = homeScreen;
await api.saveKefinTweaksHomeScreen(homeScreen); await saveHomeScreenPreferNativeSync(api, homeScreen);
pendingHomeScreen = null; pendingHomeScreen = null;
applyPresentationToElement(entry); applyPresentationToElement(entry);
@@ -651,7 +659,7 @@
homeScreen = api.upsertSectionPref(homeScreen, prefStr); homeScreen = api.upsertSectionPref(homeScreen, prefStr);
}); });
pendingHomeScreen = homeScreen; pendingHomeScreen = homeScreen;
await api.saveKefinTweaksHomeScreen(homeScreen); await saveHomeScreenPreferNativeSync(api, homeScreen);
pendingHomeScreen = null; pendingHomeScreen = null;
} catch (e) { } catch (e) {
WARN('Failed to save section preferences:', e); WARN('Failed to save section preferences:', e);