Skip to content

Commit

Permalink
fix(setup): plex library setting validation (#1233)
Browse files Browse the repository at this point in the history
* fix(setup): plex library setting validation

* fix(setup): fixes plex continue button and scroll-to-top when clicked issue in setup
  • Loading branch information
Fallenbagel authored Jan 9, 2025
1 parent 24c6208 commit b8dbfaa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/components/Settings/SettingsPlex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ const SettingsPlex = ({ onComplete }: SettingsPlexProps) => {
);
if (!res.ok) throw new Error();
}

if (onComplete) {
onComplete();
}
setIsSyncing(false);
revalidate();
};
Expand Down Expand Up @@ -435,10 +439,6 @@ const SettingsPlex = ({ onComplete }: SettingsPlexProps) => {
autoDismiss: true,
appearance: 'success',
});

if (onComplete) {
onComplete();
}
} catch (e) {
if (toastId) {
removeToast(toastId);
Expand Down
79 changes: 37 additions & 42 deletions src/components/Setup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,63 +97,58 @@ const Setup = () => {
settings.currentSettings.mediaServerType !==
MediaServerType.NOT_CONFIGURED
) {
setCurrentStep(3);
setMediaServerType(settings.currentSettings.mediaServerType);
if (currentStep < 3) {
setCurrentStep(3);
}
}

if (currentStep === 3) {
const validateLibraries = async () => {
try {
const endpoint =
settings.currentSettings.mediaServerType ===
MediaServerType.JELLYFIN || MediaServerType.EMBY
? '/api/v1/settings/jellyfin'
: '/api/v1/settings/plex';

const res = await fetch(endpoint);
if (!res.ok) throw new Error('Fetch failed');
const data = await res.json();

const hasEnabledLibraries = data?.libraries?.some(
(library: Library) => library.enabled
);

setMediaServerSettingsComplete(hasEnabledLibraries);
if (hasEnabledLibraries) {
localStorage.setItem('mediaServerSettingsComplete', 'true');
} else {
localStorage.removeItem('mediaServerSettingsComplete');
}
} catch (e) {
toasts.addToast(intl.formatMessage(messages.librarieserror), {
autoDismiss: true,
appearance: 'error',
});

setMediaServerSettingsComplete(false);
localStorage.removeItem('mediaServerSettingsComplete');
}
};

validateLibraries();
} else {
// Initialize from localStorage on mount
const storedState =
localStorage.getItem('mediaServerSettingsComplete') === 'true';
setMediaServerSettingsComplete(storedState);
}
}, [
settings.currentSettings.mediaServerType,
settings.currentSettings.initialized,
router,
currentStep,
toasts,
intl,
currentStep,
mediaServerType,
]);

const validateLibraries = async () => {
try {
const endpointMap: Record<MediaServerType, string> = {
[MediaServerType.JELLYFIN]: '/api/v1/settings/jellyfin',
[MediaServerType.EMBY]: '/api/v1/settings/jellyfin',
[MediaServerType.PLEX]: '/api/v1/settings/plex',
[MediaServerType.NOT_CONFIGURED]: '',
};

const endpoint = endpointMap[mediaServerType];
if (!endpoint) return;

const res = await fetch(endpoint);
if (!res.ok) throw new Error('Fetch failed');
const data = await res.json();

const hasEnabledLibraries = data?.libraries?.some(
(library: Library) => library.enabled
);

setMediaServerSettingsComplete(hasEnabledLibraries);
} catch (e) {
toasts.addToast(intl.formatMessage(messages.librarieserror), {
autoDismiss: true,
appearance: 'error',
});

setMediaServerSettingsComplete(false);
}
};

const handleComplete = () => {
setMediaServerSettingsComplete(true);
localStorage.setItem('mediaServerSettingsComplete', 'true');
validateLibraries();
};

if (settings.currentSettings.initialized) return <></>;
Expand Down

0 comments on commit b8dbfaa

Please sign in to comment.