Skip to content

Commit

Permalink
fix(setup): fix continue button disabled on refresh in setup 3 (#1211)
Browse files Browse the repository at this point in the history
This commit resolves an issue where the continue button in setup step 3 remained disabled after a
page refresh even when libraries are toggled. This was happening because
`mediaServerSettingsComplete` state was reset on refresh and not correctly re-initialized.
  • Loading branch information
fallenbagel authored Jan 3, 2025
1 parent 656cd91 commit 0b331ca
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/components/Setup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import useLocale from '@app/hooks/useLocale';
import useSettings from '@app/hooks/useSettings';
import defineMessages from '@app/utils/defineMessages';
import { MediaServerType } from '@server/constants/server';
import type { Library } from '@server/lib/settings';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { useToasts } from 'react-toast-notifications';
import useSWR, { mutate } from 'swr';
import SetupLogin from './SetupLogin';

Expand All @@ -35,6 +37,8 @@ const messages = defineMessages('components.Setup', {
signin: 'Sign In',
configuremediaserver: 'Configure Media Server',
configureservices: 'Configure Services',
librarieserror:
'Validation failed. Please toggle the libraries again to continue.',
});

const Setup = () => {
Expand All @@ -49,6 +53,7 @@ const Setup = () => {
const router = useRouter();
const { locale } = useLocale();
const settings = useSettings();
const toasts = useToasts();

const finishSetup = async () => {
setIsUpdating(true);
Expand Down Expand Up @@ -87,19 +92,70 @@ const Setup = () => {
if (settings.currentSettings.initialized) {
router.push('/');
}

if (
settings.currentSettings.mediaServerType !==
MediaServerType.NOT_CONFIGURED
) {
setCurrentStep(3);
setMediaServerType(settings.currentSettings.mediaServerType);
}

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,
]);

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

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

return (
Expand Down Expand Up @@ -225,14 +281,9 @@ const Setup = () => {
{currentStep === 3 && (
<div className="p-2">
{mediaServerType === MediaServerType.PLEX ? (
<SettingsPlex
onComplete={() => setMediaServerSettingsComplete(true)}
/>
<SettingsPlex onComplete={handleComplete} />
) : (
<SettingsJellyfin
isSetupSettings
onComplete={() => setMediaServerSettingsComplete(true)}
/>
<SettingsJellyfin isSetupSettings onComplete={handleComplete} />
)}
<div className="actions">
<div className="flex justify-end">
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@
"components.Setup.continue": "Continue",
"components.Setup.finish": "Finish Setup",
"components.Setup.finishing": "Finishing…",
"components.Setup.librarieserror": "Validation failed. Please toggle the libraries again to continue.",
"components.Setup.servertype": "Choose Server Type",
"components.Setup.setup": "Setup",
"components.Setup.signin": "Sign In",
Expand Down

0 comments on commit 0b331ca

Please sign in to comment.