Skip to content

Commit

Permalink
refactor: resolve missing dependency warning for validateLibraries
Browse files Browse the repository at this point in the history
…in useEffect
  • Loading branch information
fallenbagel committed Jan 11, 2025
1 parent 7d08f58 commit fdb6200
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/components/Setup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 { useCallback, useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { useToasts } from 'react-toast-notifications';
import useSWR, { mutate } from 'swr';
Expand Down Expand Up @@ -82,6 +82,37 @@ const Setup = () => {
}
};

const validateLibraries = useCallback(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);
}
}, [intl, mediaServerType, toasts]);

const { data: backdrops } = useSWR<string[]>('/api/v1/backdrops', {
refreshInterval: 0,
refreshWhenHidden: false,
Expand Down Expand Up @@ -114,39 +145,9 @@ const Setup = () => {
intl,
currentStep,
mediaServerType,
validateLibraries,
]);

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 = () => {
validateLibraries();
};
Expand Down

0 comments on commit fdb6200

Please sign in to comment.