From 87bb21820e301be4b3258fa2fc522fbe3cc8b2d4 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Fri, 17 Sep 2021 06:59:08 -0700 Subject: [PATCH] update contextmenu - move to root app - styling changes --- src/App.tsx | 2 + src/components/shared/ContextMenu.tsx | 156 ++++++++++++++++++++- src/components/shared/styled.ts | 4 +- src/components/viewtypes/ListViewTable.tsx | 6 +- 4 files changed, 159 insertions(+), 9 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7f7916a..2e4fb88 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,6 +23,7 @@ import { defaultDark, defaultLight } from './styles/styledTheme'; import { useAppSelector } from './redux/hooks'; import PageModal from './components/modal/PageModal'; import NowPlayingMiniView from './components/player/NowPlayingMiniView'; +import { GlobalContextMenu } from './components/shared/ContextMenu'; const keyMap = { FOCUS_SEARCH: 'ctrl+f', @@ -96,6 +97,7 @@ const App = () => { + diff --git a/src/components/shared/ContextMenu.tsx b/src/components/shared/ContextMenu.tsx index 04f42e2..a206faa 100644 --- a/src/components/shared/ContextMenu.tsx +++ b/src/components/shared/ContextMenu.tsx @@ -1,5 +1,23 @@ -import React from 'react'; -import { ContextMenuWindow, StyledContextMenuButton } from './styled'; +import React, { useRef, useState } from 'react'; +import { useQuery } from 'react-query'; +import { useHistory } from 'react-router'; +import { Popover, Whisper } from 'rsuite'; +import { getPlaylists, populatePlaylist } from '../../api/api'; +import { useAppDispatch, useAppSelector } from '../../redux/hooks'; +import { + addProcessingPlaylist, + removeProcessingPlaylist, + setContextMenu, +} from '../../redux/miscSlice'; +import { + ContextMenuTitle, + ContextMenuDivider, + ContextMenuWindow, + StyledContextMenuButton, + StyledInputPicker, + StyledButton, +} from './styled'; +import { notifyToast } from './toast'; export const ContextMenuButton = ({ children, ...rest }: any) => { return ( @@ -9,7 +27,7 @@ export const ContextMenuButton = ({ children, ...rest }: any) => { ); }; -export const NowPlayingContextMenu = ({ +export const ContextMenu = ({ yPos, xPos, width, @@ -31,3 +49,135 @@ export const NowPlayingContextMenu = ({ ); }; + +export const GlobalContextMenu = () => { + const history = useHistory(); + const dispatch = useAppDispatch(); + const misc = useAppSelector((state) => state.misc); + const multiSelect = useAppSelector((state) => state.multiSelect); + const playlistTriggerRef = useRef(); + const [selectedPlaylistId, setSelectedPlaylistId] = useState(''); + + const { data: playlists }: any = useQuery(['playlists', 'name'], () => + getPlaylists('name') + ); + + const handleAddToPlaylist = async () => { + // If the window is closed, the selectedPlaylistId will be deleted + const localSelectedPlaylistId = selectedPlaylistId; + dispatch(addProcessingPlaylist(selectedPlaylistId)); + + const sortedEntries = [...multiSelect.selected].sort( + (a: any, b: any) => a.rowIndex - b.rowIndex + ); + + try { + const res = await populatePlaylist( + localSelectedPlaylistId, + sortedEntries + ); + + if (res.status === 'failed') { + notifyToast('error', res.error.message); + } else { + notifyToast( + 'success', + <> +

+ Added {sortedEntries.length} song(s) to playlist " + { + playlists.find( + (playlist: any) => playlist.id === localSelectedPlaylistId + )?.name + } + " +

+ { + history.push(`/playlist/${localSelectedPlaylistId}`); + dispatch(setContextMenu({ show: false })); + }} + > + Go to playlist + + + ); + } + } catch (err) { + console.log(err); + } + + dispatch(removeProcessingPlaylist(localSelectedPlaylistId)); + }; + + return ( + <> + {misc.contextMenu.show && misc.contextMenu.type === 'nowPlaying' && ( + <> + + + Selected: {multiSelect.selected.length} + + + Add to queue + Remove from current + + + + setSelectedPlaylistId(e)} + /> + + Add + + + } + > + + playlistTriggerRef.current.state.isOverlayShown + ? playlistTriggerRef.current.close() + : playlistTriggerRef.current.open() + } + > + Add to playlist + + + + + Add to favorites + Remove from favorites + + + )} + + ); +}; diff --git a/src/components/shared/styled.ts b/src/components/shared/styled.ts index 8897fe6..a5d1ad6 100644 --- a/src/components/shared/styled.ts +++ b/src/components/shared/styled.ts @@ -170,7 +170,7 @@ export const ContextMenuWindow = styled.div<{ left: ${(props) => `${props.xPos}px`}; height: ${(props) => `${ - props.numOfButtons * 30 + + props.numOfButtons * 30.5 + props.numOfDividers * 7 + (props.hasTitle ? 16 : 0) }px`}; @@ -180,7 +180,7 @@ export const ContextMenuWindow = styled.div<{ overflow: hidden; overflow-x: hidden; font-size: smaller; - background: ${(props) => props.theme.primary.background}; + background: ${(props) => props.theme.primary.sideBar}; border: 1px #3c4043 solid; `; diff --git a/src/components/viewtypes/ListViewTable.tsx b/src/components/viewtypes/ListViewTable.tsx index bfc59f1..2d11c15 100644 --- a/src/components/viewtypes/ListViewTable.tsx +++ b/src/components/viewtypes/ListViewTable.tsx @@ -213,13 +213,11 @@ const ListViewTable = ({ (entry: any) => entry.uniqueId === rowData.uniqueId ).length > 0 ) { - const xFix = misc.expandSidebar ? 185 : 50; - const yFix = nowPlaying ? 150 : 215; dispatch( setContextMenu({ show: true, - xPos: e.pageX - xFix, - yPos: e.pageY - yFix, + xPos: e.pageX, + yPos: e.pageY, rowId: rowData.uniqueId, type: nowPlaying ? 'nowPlaying' : 'other', })