Skip to content

Commit

Permalink
Fixes Fillers & Custom Shows Preloading (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdavella authored Jun 11, 2024
1 parent f568150 commit 6a96a82
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 237 deletions.
4 changes: 2 additions & 2 deletions web/src/external/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
makeErrors,
parametersBuilder,
} from '@zodios/core';
import { isEmpty } from 'lodash-es';
import { z } from 'zod';
import { getFfmpegInfoEndpoint } from './ffmpegApi.ts';
import {
createPlexServerEndpoint,
deletePlexServerEndpoint,
Expand All @@ -45,8 +47,6 @@ import {
updateSystemSettings,
updateXmlTvSettings,
} from './settingsApi.ts';
import { isEmpty } from 'lodash-es';
import { getFfmpegInfoEndpoint } from './ffmpegApi.ts';

export const api = makeApi([
{
Expand Down
3 changes: 3 additions & 0 deletions web/src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const OneDayMillis = 1000 * 60 * 60 * 24;

// Special ID to use for in-progress entity operations
export const UnsavedId = 'unsaved';
44 changes: 39 additions & 5 deletions web/src/hooks/useCustomShows.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
DataTag,
DefinedInitialDataOptions,
QueriesResults,
UseQueryResult,
useQueries,
useQuery,
} from '@tanstack/react-query';
import { CustomShow } from '@tunarr/types';
import { CustomProgram, CustomShow } from '@tunarr/types';
import { ApiClient } from '../external/api.ts';
import { ZodiosAliasReturnType } from '../types/index.ts';
import { makeQueryOptionsInitialData } from './useQueryHelpers.ts';
Expand Down Expand Up @@ -56,19 +58,51 @@ export const customShowProgramsQuery = (apiClient: ApiClient, id: string) => ({
queryFn: () => apiClient.getCustomShowPrograms({ params: { id } }),
});

export const useCustomShow = (
// Tried to do a clever overload here but it's easier to just blow out the method
// and get the rid type inference...
export function useCustomShowWithInitialData(
apiClient: ApiClient,
id: string,
enabled: boolean,
initialData: { customShow: CustomShow; programs: CustomProgram[] },
) {
return useQueries({
queries: [
{
...customShowQuery(apiClient, id),
enabled,
initialData: initialData?.customShow,
},
{
...customShowProgramsQuery(apiClient, id),
enabled: enabled,
initialData: initialData?.programs,
},
],
});
}

export function useCustomShow(
apiClient: ApiClient,
id: string,
enabled: boolean,
includePrograms: boolean,
) => {
initialData: { customShow?: CustomShow; programs?: CustomProgram[] } = {},
): QueriesResults<
[UseQueryResult<CustomShow>, UseQueryResult<CustomProgram[]>]
> {
return useQueries({
queries: [
{ ...customShowQuery(apiClient, id), enabled },
{
...customShowQuery(apiClient, id),
enabled,
initialData: initialData?.customShow,
},
{
...customShowProgramsQuery(apiClient, id),
enabled: enabled && includePrograms,
initialData: initialData?.programs,
},
],
});
};
}
32 changes: 30 additions & 2 deletions web/src/hooks/useFillerLists.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useQuery } from '@tanstack/react-query';
import { useQueries, useQuery } from '@tanstack/react-query';
import { FillerList, FillerListProgramming } from '@tunarr/types';
import { ApiClient } from '../external/api.ts';
import useStore from '../store/index.ts';
import { makeQueryOptions } from './useQueryHelpers.ts';
import { ApiClient } from '../external/api.ts';
import { useTunarrApi } from './useTunarrApi.ts';

export const fillerListsQuery = (apiClient: ApiClient) =>
Expand All @@ -23,3 +24,30 @@ export const fillerListProgramsQuery = (apiClient: ApiClient, id: string) =>
makeQueryOptions(['fillers', id, 'programs'], () =>
apiClient.getFillerListPrograms({ params: { id } }),
);

// Tried to do a clever overload here but it's easier to just blow out the method
// and get the rid type inference...
export function useFillersWithInitialData(
apiClient: ApiClient,
id: string,
enabled: boolean,
initialData: {
filler: FillerList;
fillerListPrograms: FillerListProgramming;
},
) {
return useQueries({
queries: [
{
...fillerListQuery(apiClient, id),
enabled,
initialData: initialData?.filler,
},
{
...fillerListProgramsQuery(apiClient, id),
enabled: enabled,
initialData: initialData?.fillerListPrograms,
},
],
});
}
31 changes: 31 additions & 0 deletions web/src/hooks/usePreloadedCustomShow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useLoaderData } from 'react-router-dom';
import { UnsavedId } from '../helpers/constants';
import { CustomShowPreload } from '../preloaders/customShowLoaders';
import { setCurrentCustomShow } from '../store/channelEditor/actions';
import { useCustomShowEditor } from '../store/selectors';
import { useCustomShowWithInitialData } from './useCustomShows';
import { useTunarrApi } from './useTunarrApi';

export const usePreloadedCustomShow = () => {
const apiClient = useTunarrApi();
const { show: preloadShow, programs: preloadPrograms } =
useLoaderData() as CustomShowPreload;

const [customShow, customPrograms] = useCustomShowWithInitialData(
apiClient,
preloadShow.id,
preloadShow.id !== UnsavedId,
{
customShow: preloadShow,
programs: preloadPrograms,
},
);

const customShowEditor = useCustomShowEditor();

if (customShowEditor.currentEntity?.id !== customShow.data.id) {
setCurrentCustomShow(customShow.data, customPrograms.data);
}

return customShowEditor;
};
31 changes: 31 additions & 0 deletions web/src/hooks/usePreloadedFiller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FillerPreload } from '@/preloaders/fillerListLoader';
import { setCurrentFillerList } from '@/store/channelEditor/actions';
import { useLoaderData } from 'react-router-dom';
import { UnsavedId } from '../helpers/constants';
import { useFillerListEditor } from '../store/selectors';
import { useFillersWithInitialData } from './useFillerLists';
import { useTunarrApi } from './useTunarrApi';

export const usePreloadedFiller = () => {
const apiClient = useTunarrApi();
const { filler: preloadFiller, programs: preloadPrograms } =
useLoaderData() as FillerPreload;

const [filler, fillerPrograms] = useFillersWithInitialData(
apiClient,
preloadFiller.id,
preloadFiller.id !== UnsavedId,
{
filler: preloadFiller,
fillerListPrograms: preloadPrograms,
},
);

const fillerListEditor = useFillerListEditor();

if (fillerListEditor.currentEntity?.id !== filler.data.id) {
setCurrentFillerList(filler.data, fillerPrograms.data);
}

return fillerListEditor;
};
36 changes: 31 additions & 5 deletions web/src/pages/channels/ProgrammingSelectorPage.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
import SelectedProgrammingList from '@/components/channel_config/SelectedProgrammingList.tsx';
import {
addMediaToCurrentChannel,
addMediaToCurrentCustomShow,
addMediaToCurrentFillerList,
} from '@/store/channelEditor/actions.ts';
import useStore from '@/store/index.ts';
import { useLocation, useNavigate } from 'react-router-dom';
import Breadcrumbs from '../../components/Breadcrumbs.tsx';
import PaddedPaper from '../../components/base/PaddedPaper.tsx';
import ProgrammingSelector from '../../components/channel_config/ProgrammingSelector.tsx';
import { useNavigate } from 'react-router-dom';
import { addMediaToCurrentChannel } from '@/store/channelEditor/actions.ts';
import useStore from '@/store/index.ts';

export default function ProgrammingSelectorPage() {
const selectedLibrary = useStore((s) => s.currentLibrary);
console.log(selectedLibrary);
const navigate = useNavigate();
const location = useLocation();

const displayPaths = [
{
path: 'fillers/programming/add',
onMediaAdd: addMediaToCurrentFillerList,
},
{
path: 'custom-shows/programming/add',
onMediaAdd: addMediaToCurrentCustomShow,
},
{
path: 'channels/:id/programming/add',
onMediaAdd: addMediaToCurrentChannel,
},
];
const displaySelectedProgramming = displayPaths.find((pathObject) => {
const { path } = pathObject; // Destructure path from the object
return location.pathname.match(new RegExp(path));
});

return (
<>
<Breadcrumbs />
<PaddedPaper>
<ProgrammingSelector />
<SelectedProgrammingList
onAddSelectedMedia={addMediaToCurrentChannel}
onAddSelectedMedia={
displaySelectedProgramming?.onMediaAdd || addMediaToCurrentChannel
}
onAddMediaSuccess={() => navigate(-1)}
selectAllEnabled={selectedLibrary?.type === 'plex'}
/>
Expand Down
Loading

0 comments on commit 6a96a82

Please sign in to comment.