2023-01-03 00:50:09 -08:00
|
|
|
import { Divider, Group, Stack } from '@mantine/core';
|
2022-12-28 15:32:02 -08:00
|
|
|
import { useClickOutside, useResizeObserver, useSetState, useViewportSize } from '@mantine/hooks';
|
2023-01-03 00:50:09 -08:00
|
|
|
import { closeAllModals, openModal } from '@mantine/modals';
|
2022-12-29 17:03:49 -08:00
|
|
|
import { createContext, Fragment, useState } from 'react';
|
2023-01-05 21:59:07 -08:00
|
|
|
import { LibraryItem } from '/@/renderer/api/types';
|
2023-01-03 00:50:09 -08:00
|
|
|
import { ConfirmModal, ContextMenu, ContextMenuButton, Text, toast } from '/@/renderer/components';
|
2022-12-28 15:32:02 -08:00
|
|
|
import {
|
|
|
|
|
OpenContextMenuProps,
|
|
|
|
|
useContextMenuEvents,
|
|
|
|
|
} from '/@/renderer/features/context-menu/events';
|
2022-12-31 19:26:58 -08:00
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
2023-01-03 00:50:09 -08:00
|
|
|
import { useDeletePlaylist } from '/@/renderer/features/playlists';
|
2023-01-07 18:23:10 -08:00
|
|
|
import { useCreateFavorite, useDeleteFavorite } from '/@/renderer/features/shared';
|
2023-01-05 21:59:07 -08:00
|
|
|
import { Play } from '/@/renderer/types';
|
2022-12-28 15:32:02 -08:00
|
|
|
|
|
|
|
|
type ContextMenuContextProps = {
|
|
|
|
|
closeContextMenu: () => void;
|
|
|
|
|
openContextMenu: (args: OpenContextMenuProps) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ContextMenuContext = createContext<ContextMenuContextProps>({
|
|
|
|
|
closeContextMenu: () => {},
|
|
|
|
|
openContextMenu: (args: OpenContextMenuProps) => {
|
|
|
|
|
return args;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export interface ContextMenuProviderProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => {
|
|
|
|
|
const [opened, setOpened] = useState(false);
|
|
|
|
|
const clickOutsideRef = useClickOutside(() => setOpened(false));
|
|
|
|
|
const viewport = useViewportSize();
|
|
|
|
|
const [ref, menuRect] = useResizeObserver();
|
2023-01-07 18:16:19 -08:00
|
|
|
const [ctx, setCtx] = useSetState<OpenContextMenuProps>({
|
2022-12-28 15:32:02 -08:00
|
|
|
data: [],
|
2023-01-07 18:16:19 -08:00
|
|
|
dataNodes: [],
|
2022-12-28 15:32:02 -08:00
|
|
|
menuItems: [],
|
2023-01-07 18:16:19 -08:00
|
|
|
tableRef: undefined,
|
2022-12-28 15:32:02 -08:00
|
|
|
type: LibraryItem.SONG,
|
|
|
|
|
xPos: 0,
|
|
|
|
|
yPos: 0,
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-31 19:26:58 -08:00
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
2022-12-28 15:32:02 -08:00
|
|
|
|
|
|
|
|
const openContextMenu = (args: OpenContextMenuProps) => {
|
2023-01-07 18:16:19 -08:00
|
|
|
const { xPos, yPos, menuItems, data, type, tableRef, dataNodes } = args;
|
2022-12-28 15:32:02 -08:00
|
|
|
|
|
|
|
|
const shouldReverseY = yPos + menuRect.height > viewport.height;
|
|
|
|
|
const shouldReverseX = xPos + menuRect.width > viewport.width;
|
|
|
|
|
|
|
|
|
|
const calculatedXPos = shouldReverseX ? xPos - menuRect.width : xPos;
|
|
|
|
|
const calculatedYPos = shouldReverseY ? yPos - menuRect.height : yPos;
|
|
|
|
|
|
2023-01-07 18:16:19 -08:00
|
|
|
setCtx({
|
|
|
|
|
data,
|
|
|
|
|
dataNodes,
|
|
|
|
|
menuItems,
|
|
|
|
|
tableRef,
|
|
|
|
|
type,
|
|
|
|
|
xPos: calculatedXPos,
|
|
|
|
|
yPos: calculatedYPos,
|
|
|
|
|
});
|
2022-12-28 15:32:02 -08:00
|
|
|
setOpened(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const closeContextMenu = () => {
|
|
|
|
|
setOpened(false);
|
2023-01-07 18:16:19 -08:00
|
|
|
setCtx({
|
|
|
|
|
data: [],
|
|
|
|
|
dataNodes: [],
|
|
|
|
|
menuItems: [],
|
|
|
|
|
tableRef: undefined,
|
|
|
|
|
type: LibraryItem.SONG,
|
|
|
|
|
xPos: 0,
|
|
|
|
|
yPos: 0,
|
|
|
|
|
});
|
2022-12-28 15:32:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useContextMenuEvents({
|
|
|
|
|
closeContextMenu,
|
|
|
|
|
openContextMenu,
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-28 19:19:51 -08:00
|
|
|
const handlePlay = (play: Play) => {
|
|
|
|
|
switch (ctx.type) {
|
|
|
|
|
case LibraryItem.ALBUM:
|
2022-12-31 19:26:58 -08:00
|
|
|
handlePlayQueueAdd?.({
|
2022-12-28 19:19:51 -08:00
|
|
|
byItemType: { id: ctx.data.map((item) => item.id), type: ctx.type },
|
|
|
|
|
play,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case LibraryItem.ARTIST:
|
2022-12-31 19:26:58 -08:00
|
|
|
handlePlayQueueAdd?.({
|
2022-12-28 19:19:51 -08:00
|
|
|
byItemType: { id: ctx.data.map((item) => item.id), type: ctx.type },
|
|
|
|
|
play,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case LibraryItem.ALBUM_ARTIST:
|
2022-12-31 19:26:58 -08:00
|
|
|
handlePlayQueueAdd?.({
|
2022-12-28 19:19:51 -08:00
|
|
|
byItemType: { id: ctx.data.map((item) => item.id), type: ctx.type },
|
|
|
|
|
play,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case LibraryItem.SONG:
|
2022-12-31 19:26:58 -08:00
|
|
|
handlePlayQueueAdd?.({ byData: ctx.data, play });
|
2022-12-28 19:19:51 -08:00
|
|
|
break;
|
|
|
|
|
case LibraryItem.PLAYLIST:
|
2022-12-31 20:07:44 -08:00
|
|
|
for (const item of ctx.data) {
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: { id: [item.id], type: ctx.type },
|
|
|
|
|
play,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-28 19:19:51 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 00:50:09 -08:00
|
|
|
const deletePlaylistMutation = useDeletePlaylist();
|
|
|
|
|
|
|
|
|
|
const handleDeletePlaylist = () => {
|
|
|
|
|
for (const item of ctx.data) {
|
|
|
|
|
deletePlaylistMutation?.mutate(
|
|
|
|
|
{ query: { id: item.id } },
|
|
|
|
|
{
|
|
|
|
|
onError: (err) => {
|
|
|
|
|
toast.error({
|
|
|
|
|
message: err.message,
|
|
|
|
|
title: 'Error deleting playlist',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
toast.success({
|
|
|
|
|
message: `${item.name} was successfully deleted`,
|
|
|
|
|
title: 'Playlist deleted',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
closeAllModals();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openDeletePlaylistModal = () => {
|
|
|
|
|
openModal({
|
|
|
|
|
children: (
|
|
|
|
|
<ConfirmModal onConfirm={handleDeletePlaylist}>
|
|
|
|
|
<Stack>
|
|
|
|
|
<Text>Are you sure you want to delete the following playlist(s)?</Text>
|
|
|
|
|
<ul>
|
|
|
|
|
{ctx.data.map((item) => (
|
|
|
|
|
<li key={item.id}>
|
|
|
|
|
<Group>
|
|
|
|
|
—<Text $secondary>{item.name}</Text>
|
|
|
|
|
</Group>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</Stack>
|
|
|
|
|
</ConfirmModal>
|
|
|
|
|
),
|
|
|
|
|
title: 'Delete playlist(s)',
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-07 18:23:10 -08:00
|
|
|
const createFavoriteMutation = useCreateFavorite();
|
|
|
|
|
const deleteFavoriteMutation = useDeleteFavorite();
|
|
|
|
|
const handleAddToFavorites = () => {
|
|
|
|
|
if (!ctx.dataNodes) return;
|
|
|
|
|
const nodesToFavorite = ctx.dataNodes.filter((item) => !item.data.userFavorite);
|
|
|
|
|
createFavoriteMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: nodesToFavorite.map((item) => item.data.id),
|
|
|
|
|
type: ctx.type,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onError: (err) => {
|
|
|
|
|
toast.error({
|
|
|
|
|
message: err.message,
|
|
|
|
|
title: 'Error adding to favorites',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
for (const node of nodesToFavorite) {
|
|
|
|
|
node.setData({ ...node.data, userFavorite: true });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleRemoveFromFavorites = () => {
|
|
|
|
|
if (!ctx.dataNodes) return;
|
|
|
|
|
const nodesToUnfavorite = ctx.dataNodes.filter((item) => item.data.userFavorite);
|
|
|
|
|
|
|
|
|
|
deleteFavoriteMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
query: {
|
|
|
|
|
id: nodesToUnfavorite.map((item) => item.data.id),
|
|
|
|
|
type: ctx.type,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
for (const node of nodesToUnfavorite) {
|
|
|
|
|
node.setData({ ...node.data, userFavorite: false });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-28 19:19:51 -08:00
|
|
|
const contextMenuItems = {
|
2023-01-07 18:23:10 -08:00
|
|
|
addToFavorites: {
|
|
|
|
|
id: 'addToFavorites',
|
|
|
|
|
label: 'Add to favorites',
|
|
|
|
|
onClick: handleAddToFavorites,
|
|
|
|
|
},
|
2022-12-28 19:19:51 -08:00
|
|
|
addToPlaylist: { id: 'addToPlaylist', label: 'Add to playlist', onClick: () => {} },
|
2023-01-03 00:50:09 -08:00
|
|
|
createPlaylist: { id: 'createPlaylist', label: 'Create playlist', onClick: () => {} },
|
|
|
|
|
deletePlaylist: {
|
|
|
|
|
id: 'deletePlaylist',
|
|
|
|
|
label: 'Delete playlist',
|
|
|
|
|
onClick: openDeletePlaylistModal,
|
|
|
|
|
},
|
2022-12-28 19:19:51 -08:00
|
|
|
play: {
|
|
|
|
|
id: 'play',
|
|
|
|
|
label: 'Play',
|
|
|
|
|
onClick: () => handlePlay(Play.NOW),
|
|
|
|
|
},
|
|
|
|
|
playLast: {
|
|
|
|
|
id: 'playLast',
|
2023-01-07 03:49:18 -08:00
|
|
|
label: 'Add to queue',
|
2022-12-28 19:19:51 -08:00
|
|
|
onClick: () => handlePlay(Play.LAST),
|
|
|
|
|
},
|
|
|
|
|
playNext: {
|
|
|
|
|
id: 'playNext',
|
2023-01-07 03:49:18 -08:00
|
|
|
label: 'Add to queue next',
|
2022-12-28 19:19:51 -08:00
|
|
|
onClick: () => handlePlay(Play.NEXT),
|
|
|
|
|
},
|
|
|
|
|
removeFromFavorites: {
|
|
|
|
|
id: 'removeFromFavorites',
|
|
|
|
|
label: 'Remove from favorites',
|
2023-01-07 18:23:10 -08:00
|
|
|
onClick: handleRemoveFromFavorites,
|
2022-12-28 19:19:51 -08:00
|
|
|
},
|
|
|
|
|
setRating: { id: 'setRating', label: 'Set rating', onClick: () => {} },
|
|
|
|
|
};
|
2022-12-28 15:32:02 -08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ContextMenuContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
closeContextMenu,
|
|
|
|
|
openContextMenu,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{opened && (
|
|
|
|
|
<ContextMenu
|
|
|
|
|
ref={ref}
|
|
|
|
|
minWidth={125}
|
|
|
|
|
xPos={ctx.xPos}
|
|
|
|
|
yPos={ctx.yPos}
|
|
|
|
|
>
|
|
|
|
|
<Stack
|
|
|
|
|
ref={clickOutsideRef}
|
|
|
|
|
spacing={0}
|
|
|
|
|
onClick={closeContextMenu}
|
|
|
|
|
>
|
|
|
|
|
{ctx.menuItems?.map((item) => {
|
|
|
|
|
return (
|
2022-12-29 17:03:49 -08:00
|
|
|
<Fragment key={`context-menu-${item.id}`}>
|
|
|
|
|
<ContextMenuButton
|
|
|
|
|
disabled={item.disabled}
|
|
|
|
|
variant="default"
|
|
|
|
|
onClick={contextMenuItems[item.id as keyof typeof contextMenuItems].onClick}
|
|
|
|
|
>
|
|
|
|
|
{contextMenuItems[item.id as keyof typeof contextMenuItems].label}
|
|
|
|
|
</ContextMenuButton>
|
|
|
|
|
{item.divider && <Divider key={`context-menu-divider-${item.id}`} />}
|
|
|
|
|
</Fragment>
|
2022-12-28 15:32:02 -08:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Stack>
|
|
|
|
|
</ContextMenu>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{children}
|
|
|
|
|
</ContextMenuContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|