Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on unmount in syncplay menu #4793

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import React, { FC, useCallback, useEffect, useState } from 'react';

import { pluginManager } from 'components/pluginManager';
import { useApi } from 'hooks/useApi';
import { useSyncPlayGroups } from 'hooks/useSyncPlayGroups';
import globalize from 'scripts/globalize';
import { PluginType } from 'types/plugin';
import Events from 'utils/events';
Expand Down Expand Up @@ -47,26 +48,14 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
}) => {
const [ syncPlay, setSyncPlay ] = useState<SyncPlayInstance>();
const { __legacyApiClient__, api, user } = useApi();
const [ groups, setGroups ] = useState<GroupInfoDto[]>([]);
const [ currentGroup, setCurrentGroup ] = useState<GroupInfoDto>();
const isSyncPlayEnabled = Boolean(currentGroup);

useEffect(() => {
setSyncPlay(pluginManager.firstOfType(PluginType.SyncPlay)?.instance);
}, []);

useEffect(() => {
const fetchGroups = async () => {
if (api) {
setGroups((await getSyncPlayApi(api).syncPlayGetGroups()).data);
}
};

fetchGroups()
.catch(err => {
console.error('[SyncPlayMenu] unable to fetch SyncPlay groups', err);
});
}, [ api ]);
const { data: groups } = useSyncPlayGroups();

const onGroupAddClick = useCallback(() => {
if (api && user) {
Expand Down Expand Up @@ -224,7 +213,7 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
/>
</MenuItem>
);
} else if (groups.length === 0 && user?.Policy?.SyncPlayAccess !== SyncPlayUserAccessType.CreateAndJoinGroups) {
} else if (!groups?.length && user?.Policy?.SyncPlayAccess !== SyncPlayUserAccessType.CreateAndJoinGroups) {
menuItems.push(
<MenuItem key='sync-play-unavailable' disabled>
<ListItemIcon>
Expand All @@ -234,7 +223,7 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
</MenuItem>
);
} else {
if (groups.length > 0) {
if (groups && groups.length > 0) {
groups.forEach(group => {
menuItems.push(
<MenuItem
Expand Down
24 changes: 24 additions & 0 deletions src/hooks/useSyncPlayGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useQuery } from '@tanstack/react-query';
import { JellyfinApiContext, useApi } from './useApi';
import { getSyncPlayApi } from '@jellyfin/sdk/lib/utils/api/sync-play-api';
import { AxiosRequestConfig } from 'axios';

const fetchSyncPlayGroups = async (
currentApi: JellyfinApiContext,
options?: AxiosRequestConfig
) => {
const { api } = currentApi;
if (!api) throw new Error('No API instance available');

const response = await getSyncPlayApi(api)
.syncPlayGetGroups(options);
return response.data;
};

export const useSyncPlayGroups = () => {
const currentApi = useApi();
return useQuery({
queryKey: [ 'SyncPlay', 'Groups' ],
queryFn: ({ signal }) => fetchSyncPlayGroups(currentApi, { signal })
});
};
Loading