mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-13 20:10:07 +02:00
add new context menu implementation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useSessionStorage } from '/@/shared/hooks/use-session-storage';
|
||||
|
||||
interface RecentPlaylists {
|
||||
[serverId: string]: string;
|
||||
}
|
||||
|
||||
const RECENT_PLAYLISTS_KEY = 'recent-playlists';
|
||||
const DEFAULT_VALUE: RecentPlaylists = {};
|
||||
|
||||
export const useRecentPlaylists = (serverId: null | string) => {
|
||||
const [recentPlaylists, setRecentPlaylists] = useSessionStorage<RecentPlaylists>({
|
||||
defaultValue: DEFAULT_VALUE,
|
||||
key: RECENT_PLAYLISTS_KEY,
|
||||
});
|
||||
|
||||
const getRecentPlaylistId = useCallback((): null | string => {
|
||||
if (!serverId) return null;
|
||||
return recentPlaylists[serverId] || null;
|
||||
}, [recentPlaylists, serverId]);
|
||||
|
||||
const addRecentPlaylist = useCallback(
|
||||
(playlistId: string) => {
|
||||
if (!serverId || !playlistId) return;
|
||||
|
||||
setRecentPlaylists({
|
||||
...recentPlaylists,
|
||||
[serverId]: playlistId,
|
||||
});
|
||||
},
|
||||
[recentPlaylists, serverId, setRecentPlaylists],
|
||||
);
|
||||
|
||||
return {
|
||||
addRecentPlaylist,
|
||||
recentPlaylistId: getRecentPlaylistId(),
|
||||
};
|
||||
};
|
||||
@@ -3,12 +3,17 @@ import { AxiosError } from 'axios';
|
||||
|
||||
import { api } from '/@/renderer/api';
|
||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||
import { useRecentPlaylists } from '/@/renderer/features/playlists/hooks/use-recent-playlists';
|
||||
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
||||
import { useCurrentServerId } from '/@/renderer/store';
|
||||
import { AddToPlaylistArgs, AddToPlaylistResponse } from '/@/shared/types/domain-types';
|
||||
|
||||
export const useAddToPlaylist = (args: MutationHookArgs) => {
|
||||
const { options } = args || {};
|
||||
const queryClient = useQueryClient();
|
||||
const serverId = useCurrentServerId();
|
||||
|
||||
const { addRecentPlaylist } = useRecentPlaylists(serverId);
|
||||
|
||||
return useMutation<AddToPlaylistResponse, AxiosError, AddToPlaylistArgs, null>({
|
||||
mutationFn: (args) => {
|
||||
@@ -17,7 +22,7 @@ export const useAddToPlaylist = (args: MutationHookArgs) => {
|
||||
apiClientProps: { serverId: args.apiClientProps.serverId },
|
||||
});
|
||||
},
|
||||
onSuccess: (_data, variables) => {
|
||||
onSuccess: (_data, variables, context) => {
|
||||
const { apiClientProps } = variables;
|
||||
const serverId = apiClientProps.serverId;
|
||||
|
||||
@@ -33,6 +38,10 @@ export const useAddToPlaylist = (args: MutationHookArgs) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.playlists.songList(serverId, variables.query.id),
|
||||
});
|
||||
|
||||
addRecentPlaylist(variables.query.id);
|
||||
|
||||
options?.onSuccess?.(_data, variables, context);
|
||||
},
|
||||
...options,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user