Use react query to fetch syncplay groups
This commit is contained in:
parent
55a2ca3590
commit
e46330c70d
2 changed files with 28 additions and 22 deletions
|
@ -19,6 +19,7 @@ import React, { FC, useCallback, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { pluginManager } from 'components/pluginManager';
|
import { pluginManager } from 'components/pluginManager';
|
||||||
import { useApi } from 'hooks/useApi';
|
import { useApi } from 'hooks/useApi';
|
||||||
|
import { useSyncPlayGroups } from 'hooks/useSyncPlayGroups';
|
||||||
import globalize from 'scripts/globalize';
|
import globalize from 'scripts/globalize';
|
||||||
import { PluginType } from 'types/plugin';
|
import { PluginType } from 'types/plugin';
|
||||||
import Events from 'utils/events';
|
import Events from 'utils/events';
|
||||||
|
@ -47,7 +48,6 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
|
||||||
}) => {
|
}) => {
|
||||||
const [ syncPlay, setSyncPlay ] = useState<SyncPlayInstance>();
|
const [ syncPlay, setSyncPlay ] = useState<SyncPlayInstance>();
|
||||||
const { __legacyApiClient__, api, user } = useApi();
|
const { __legacyApiClient__, api, user } = useApi();
|
||||||
const [ groups, setGroups ] = useState<GroupInfoDto[]>([]);
|
|
||||||
const [ currentGroup, setCurrentGroup ] = useState<GroupInfoDto>();
|
const [ currentGroup, setCurrentGroup ] = useState<GroupInfoDto>();
|
||||||
const isSyncPlayEnabled = Boolean(currentGroup);
|
const isSyncPlayEnabled = Boolean(currentGroup);
|
||||||
|
|
||||||
|
@ -55,25 +55,7 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
|
||||||
setSyncPlay(pluginManager.firstOfType(PluginType.SyncPlay)?.instance);
|
setSyncPlay(pluginManager.firstOfType(PluginType.SyncPlay)?.instance);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
const { data: groups } = useSyncPlayGroups();
|
||||||
let isMounted = true;
|
|
||||||
|
|
||||||
const fetchGroups = async () => {
|
|
||||||
if (api) {
|
|
||||||
const response = await getSyncPlayApi(api).syncPlayGetGroups();
|
|
||||||
if (isMounted) setGroups(response.data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchGroups()
|
|
||||||
.catch(err => {
|
|
||||||
console.error('[SyncPlayMenu] unable to fetch SyncPlay groups', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
isMounted = false;
|
|
||||||
};
|
|
||||||
}, [ api ]);
|
|
||||||
|
|
||||||
const onGroupAddClick = useCallback(() => {
|
const onGroupAddClick = useCallback(() => {
|
||||||
if (api && user) {
|
if (api && user) {
|
||||||
|
@ -231,7 +213,7 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
|
||||||
/>
|
/>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
);
|
);
|
||||||
} else if (groups.length === 0 && user?.Policy?.SyncPlayAccess !== SyncPlayUserAccessType.CreateAndJoinGroups) {
|
} else if (!groups?.length && user?.Policy?.SyncPlayAccess !== SyncPlayUserAccessType.CreateAndJoinGroups) {
|
||||||
menuItems.push(
|
menuItems.push(
|
||||||
<MenuItem key='sync-play-unavailable' disabled>
|
<MenuItem key='sync-play-unavailable' disabled>
|
||||||
<ListItemIcon>
|
<ListItemIcon>
|
||||||
|
@ -241,7 +223,7 @@ const SyncPlayMenu: FC<SyncPlayMenuProps> = ({
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if (groups.length > 0) {
|
if (groups && groups.length > 0) {
|
||||||
groups.forEach(group => {
|
groups.forEach(group => {
|
||||||
menuItems.push(
|
menuItems.push(
|
||||||
<MenuItem
|
<MenuItem
|
||||||
|
|
24
src/hooks/useSyncPlayGroups.ts
Normal file
24
src/hooks/useSyncPlayGroups.ts
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue