feat: favorite & watch lists

This commit is contained in:
ner committed 2024-10-19 16:09:15 +03:00
1 parent 960a7a6242
commit 8858e47d38
51 files changed
+561 -290

No files matched your search

-27
View File
@@ -1,27 +0,0 @@
import { account } from '@/api';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useSession } from './useSession';
const KEY = ['account'];
const useAccount = () => {
const queryClient = useQueryClient();
const session = useSession();
const query = useQuery({
enabled: Boolean(session),
staleTime: Infinity,
notifyOnChangeProps: ['data'],
queryKey: KEY,
queryFn: () => account()
});
if (session === null) {
queryClient.removeQueries({ queryKey: KEY });
queryClient.resetQueries({ queryKey: KEY });
}
return session ? query.data : undefined;
};
export default useAccount;
-21
View File
@@ -1,21 +0,0 @@
import { usersShows } from '@/api';
import useInfiniteList from './useInfiniteList';
import { useSession } from './useSession';
const useFavorites = ({
showType,
listType = 'favorites'
}: {
showType: Show['type'];
listType: 'favorites' | 'watchlist';
}) => {
const session = useSession();
return useInfiniteList({
queryKey: [`users-${listType}`, showType],
queryFn: () => usersShows({ showType, listType }),
enabled: Boolean(session)
});
};
export default useFavorites;
+78
View File
@@ -0,0 +1,78 @@
import { Atom, atom, PrimitiveAtom, useAtomValue, useSetAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
import { useCallback } from 'react';
type MapShow = Map<Show['id'], Show>;
const createStorage = (key: string) => {
const listAtom = atomWithStorage<Show[]>(key, [], undefined, {
unstable_getOnInit: true
});
const mapAtom = atom<MapShow>((get) => {
const map = new Map();
const list = get(listAtom);
list.forEach((show) => map.set(show.id, show));
return map;
});
return { listAtom, mapAtom };
};
const createUseShows =
({ listAtom, mapAtom }: { listAtom: PrimitiveAtom<Show[]>; mapAtom: Atom<MapShow> }) =>
() => ({
shows: useAtomValue(listAtom),
map: useAtomValue(mapAtom)
});
const createUseShowsMutation =
({ listAtom }: { listAtom: PrimitiveAtom<Show[]> }) =>
() => {
const setShows = useSetAtom(listAtom);
const add = (show: Show) => setShows((shows) => [show, ...shows]);
const remove = (showId: number) => setShows((shows) => shows.filter(({ id }) => id !== showId));
return {
add,
remove
};
};
const createUseStateToggle =
({
useShows,
useShowsMutation
}: {
useShows: ReturnType<typeof createUseShows>;
useShowsMutation: ReturnType<typeof createUseShowsMutation>;
}) =>
(show: Show) => {
const { add, remove } = useShowsMutation();
const { map } = useShows();
const active = map.has(show.id);
const toggle = useCallback(() => (active ? remove(show.id) : add(show)), [add, remove, show]);
return { active, toggle };
};
const createStorageHooks = (key: string) => {
const storage = createStorage(key);
const useShows = createUseShows(storage);
const useShowsMutation = createUseShowsMutation(storage);
const useStateToggle = createUseStateToggle({
useShows,
useShowsMutation
});
return { useShows, useShowsMutation, useStateToggle };
};
export const { useShows: useFavorites, useStateToggle: useFavoritesStateToggle } =
createStorageHooks('users-favorites');
export const { useShows: useWatchList, useStateToggle: useWatchListStateToggle } =
createStorageHooks('users-watch-list');
-31
View File
@@ -1,31 +0,0 @@
import { useCallback } from 'react';
import { useAtomValue, useSetAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
import showHostManagerModal from '@/components/Show/components/Torrents/components/HostManager';
const magnetPrefixAtom = atomWithStorage<string>('magnet-prefix', 'http://{host}:65220/playuri?uri=', undefined, {
unstable_getOnInit: true
});
export const usePrefix = () => useAtomValue(magnetPrefixAtom);
export const useSetPrefix = () => useSetAtom(magnetPrefixAtom);
export const useCastMagnet = () => {
const prefix = usePrefix();
return useCallback(
(magnet: string) => {
const open = (prefix: string) => {
const link = window.open(`${prefix}${magnet}`, '_blank');
setTimeout(() => link?.close(), 1000);
};
if (!prefix || prefix.includes('{host}')) return showHostManagerModal().then(open);
return Promise.resolve(open(prefix));
},
[prefix]
);
};
+69
View File
@@ -0,0 +1,69 @@
import { accountStates, updateAccountStates } from '@/api';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
import { useSession } from './useSession';
import { KEY as USERS_SHOWS_KEY } from './useUsersShows';
const KEY = ['show-state'];
const useShowState = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) => {
const session = useSession();
const { map: favorites } = useFavorites();
const { map: watchList } = useWatchList();
const query = useQuery({
enabled: Boolean(session && showId && showType),
queryKey: KEY.concat(showId!.toString(), showType),
queryFn: () => accountStates({ showId: showId!, showType }),
notifyOnChangeProps: ['data'],
staleTime: Infinity
});
return session ? query.data : { id: showId, watchlist: watchList.has(showId!), favorite: favorites.has(showId!) };
};
export const useMutateShowState = (show: Show) => {
const session = useSession();
const { toggle: toggleFavorite } = useFavoritesStateToggle(show);
const { toggle: toggleWatchList } = useWatchListStateToggle(show);
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: updateAccountStates,
onMutate({ showId, showType, list, value }) {
const key = KEY.concat(showId.toString(), showType);
const snapshot = queryClient.getQueryData<ShowState>(key);
queryClient.setQueryData<ShowState>(key, (state) => ({ ...state, [list]: value }) as ShowState);
return snapshot;
},
onError(_, { showId, showType }, snapshot) {
const key = KEY.concat(showId.toString(), showType);
queryClient.setQueryData<ShowState>(key, snapshot);
},
onSuccess(_, { list, showType }) {
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }) });
}
});
return {
list: mutation.variables?.list,
isPending: session ? mutation.isPending : false,
mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => {
if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value });
if (list === 'favorite') return toggleFavorite();
return toggleWatchList();
}
};
};
export default useShowState;
+36
View File
@@ -0,0 +1,36 @@
import { usersShows } from '@/api';
import useInfiniteList from './useInfiniteList';
import { useSession } from './useSession';
import { useFavorites, useWatchList } from './useLocalUsersLists';
export const KEY = ({ list, showType }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => [
`users-${list}`,
showType
];
const useUsersShows = ({ showType, list = 'favorite' }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => {
const session = useSession();
const { shows: favorite } = useFavorites();
const { shows: watchlist } = useWatchList();
const query = useInfiniteList({
queryKey: KEY({ list, showType }),
queryFn: () => usersShows({ showType, list }),
enabled: Boolean(session)
});
return session
? query
: {
fetchNextPage: undefined,
isLoading: false,
isFetched: true,
shows: {
favorite: favorite.filter((show) => show.type === showType),
watchlist: watchlist.filter((show) => show.type === showType)
}[list]
};
};
export default useUsersShows;