2023-03-29 14:27:25 -07:00
|
|
|
import { useCallback, useMemo, useState } from 'react';
|
2024-02-18 20:22:38 -08:00
|
|
|
import { Box, Flex, Group } from '@mantine/core';
|
2023-03-29 14:27:25 -07:00
|
|
|
import { useDebouncedValue } from '@mantine/hooks';
|
2023-10-30 19:22:45 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-08-24 21:09:44 -07:00
|
|
|
import {
|
|
|
|
|
RiAddBoxFill,
|
|
|
|
|
RiAddCircleFill,
|
|
|
|
|
RiArrowDownSLine,
|
|
|
|
|
RiArrowUpSLine,
|
|
|
|
|
RiPlayFill,
|
|
|
|
|
} from 'react-icons/ri';
|
2023-02-05 18:59:39 -08:00
|
|
|
import { generatePath } from 'react-router';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
2024-09-26 04:23:08 +00:00
|
|
|
import { LibraryItem, Playlist, PlaylistListSort, SortOrder } from '/@/renderer/api/types';
|
2023-02-08 03:44:37 -08:00
|
|
|
import { Button, Text } from '/@/renderer/components';
|
2023-02-05 18:59:39 -08:00
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
|
|
|
|
import { usePlaylistList } from '/@/renderer/features/playlists';
|
|
|
|
|
import { AppRoute } from '/@/renderer/router/routes';
|
2024-03-04 02:37:37 -08:00
|
|
|
import { Play } from '/@/renderer/types';
|
2023-02-08 03:44:37 -08:00
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
|
|
|
|
import { FixedSizeList, ListChildComponentProps } from 'react-window';
|
|
|
|
|
import { useHideScrollbar } from '/@/renderer/hooks';
|
2024-08-24 21:09:44 -07:00
|
|
|
import { useCurrentServer, useGeneralSettings, useSettingsStoreActions } from '/@/renderer/store';
|
2024-08-25 22:08:07 -07:00
|
|
|
import { openContextMenu } from '/@/renderer/features/context-menu';
|
|
|
|
|
import { PLAYLIST_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items';
|
2023-02-05 18:59:39 -08:00
|
|
|
|
2023-03-28 23:59:51 -07:00
|
|
|
const PlaylistRow = ({ index, data, style }: ListChildComponentProps) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
const { t } = useTranslation();
|
2024-02-18 20:22:38 -08:00
|
|
|
|
2024-08-24 21:09:44 -07:00
|
|
|
if (Array.isArray(data?.items[index])) {
|
|
|
|
|
const [collapse, setCollapse] = data.items[index];
|
|
|
|
|
|
2024-02-18 20:22:38 -08:00
|
|
|
return (
|
|
|
|
|
<div style={{ margin: '0.5rem 0', padding: '0 1.5rem', ...style }}>
|
|
|
|
|
<Box
|
|
|
|
|
fw="600"
|
|
|
|
|
sx={{ fontSize: '1.2rem' }}
|
|
|
|
|
>
|
2024-08-24 21:09:44 -07:00
|
|
|
<Group>
|
|
|
|
|
<Text>{t('page.sidebar.shared', { postProcess: 'titleCase' })}</Text>
|
|
|
|
|
<Button
|
|
|
|
|
compact
|
|
|
|
|
tooltip={{
|
|
|
|
|
label: t(collapse ? 'common.expand' : 'common.collapse', {
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
}),
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
|
|
|
|
variant="default"
|
|
|
|
|
onClick={() => setCollapse()}
|
|
|
|
|
>
|
|
|
|
|
{collapse ? (
|
|
|
|
|
<RiArrowUpSLine size={20} />
|
|
|
|
|
) : (
|
|
|
|
|
<RiArrowDownSLine size={20} />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</Group>
|
2024-02-18 20:22:38 -08:00
|
|
|
</Box>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 15:57:40 -07:00
|
|
|
const path = data?.items[index].id
|
2024-09-26 04:23:08 +00:00
|
|
|
? generatePath(AppRoute.PLAYLISTS_DETAIL_SONGS, { playlistId: data.items[index].id })
|
2023-07-15 15:57:40 -07:00
|
|
|
: undefined;
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
2024-08-25 22:17:11 -07:00
|
|
|
<div
|
|
|
|
|
style={{ margin: '0.5rem 0', padding: '0 1.5rem', ...style }}
|
|
|
|
|
onContextMenu={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
if (!data?.items?.[index].id) return;
|
|
|
|
|
|
|
|
|
|
openContextMenu({
|
|
|
|
|
data: [data?.items?.[index]],
|
|
|
|
|
dataNodes: undefined,
|
|
|
|
|
menuItems: PLAYLIST_CONTEXT_MENU_ITEMS,
|
|
|
|
|
type: LibraryItem.PLAYLIST,
|
|
|
|
|
xPos: e.clientX + 15,
|
|
|
|
|
yPos: e.clientY + 5,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-07-01 19:10:05 -07:00
|
|
|
<Group
|
|
|
|
|
noWrap
|
|
|
|
|
className="sidebar-playlist-item"
|
|
|
|
|
pos="relative"
|
|
|
|
|
position="apart"
|
|
|
|
|
sx={{
|
|
|
|
|
'&:hover': {
|
|
|
|
|
'.sidebar-playlist-controls': {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
},
|
|
|
|
|
'.sidebar-playlist-name': {
|
|
|
|
|
color: 'var(--sidebar-fg-hover) !important',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Text
|
|
|
|
|
className="sidebar-playlist-name"
|
|
|
|
|
component={Link}
|
|
|
|
|
overflow="hidden"
|
|
|
|
|
size="md"
|
|
|
|
|
sx={{
|
|
|
|
|
color: 'var(--sidebar-fg) !important',
|
|
|
|
|
cursor: 'default',
|
|
|
|
|
width: '100%',
|
|
|
|
|
}}
|
2023-07-15 15:57:40 -07:00
|
|
|
to={path}
|
2023-07-01 19:10:05 -07:00
|
|
|
>
|
|
|
|
|
{data?.items[index].name}
|
|
|
|
|
</Text>
|
|
|
|
|
<Group
|
|
|
|
|
noWrap
|
|
|
|
|
className="sidebar-playlist-controls"
|
|
|
|
|
display="none"
|
|
|
|
|
pos="absolute"
|
|
|
|
|
right="0"
|
|
|
|
|
spacing="sm"
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
compact
|
|
|
|
|
size="md"
|
2023-10-30 19:22:45 -07:00
|
|
|
tooltip={{
|
|
|
|
|
label: t('player.play', { postProcess: 'sentenceCase' }),
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
2023-07-01 19:10:05 -07:00
|
|
|
variant="default"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (!data?.items?.[index].id) return;
|
|
|
|
|
data.handlePlay(data?.items[index].id, Play.NOW);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<RiPlayFill />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
compact
|
|
|
|
|
size="md"
|
2023-10-30 19:22:45 -07:00
|
|
|
tooltip={{
|
|
|
|
|
label: t('player.addLast', { postProcess: 'sentenceCase' }),
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
2023-07-01 19:10:05 -07:00
|
|
|
variant="default"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (!data?.items?.[index].id) return;
|
|
|
|
|
data.handlePlay(data?.items[index].id, Play.LAST);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<RiAddBoxFill />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
compact
|
|
|
|
|
size="md"
|
2023-10-30 19:22:45 -07:00
|
|
|
tooltip={{
|
|
|
|
|
label: t('player.addNext', { postProcess: 'sentenceCase' }),
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
2023-07-01 19:10:05 -07:00
|
|
|
variant="default"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (!data?.items?.[index].id) return;
|
|
|
|
|
data.handlePlay(data?.items[index].id, Play.NEXT);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<RiAddCircleFill />
|
|
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Group>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-03-28 23:59:51 -07:00
|
|
|
};
|
2023-02-05 18:59:39 -08:00
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
export const SidebarPlaylistList = () => {
|
2023-07-01 19:10:05 -07:00
|
|
|
const { isScrollbarHidden, hideScrollbarElementProps } = useHideScrollbar(0);
|
|
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
2024-09-26 04:23:08 +00:00
|
|
|
const { sidebarCollapseShared } = useGeneralSettings();
|
2024-08-24 21:09:44 -07:00
|
|
|
const { toggleSidebarCollapseShare } = useSettingsStoreActions();
|
2024-09-26 04:23:08 +00:00
|
|
|
const server = useCurrentServer();
|
|
|
|
|
|
|
|
|
|
const playlistsQuery = usePlaylistList({
|
|
|
|
|
query: {
|
|
|
|
|
sortBy: PlaylistListSort.NAME,
|
|
|
|
|
sortOrder: SortOrder.ASC,
|
|
|
|
|
startIndex: 0,
|
|
|
|
|
},
|
|
|
|
|
serverId: server?.id,
|
|
|
|
|
});
|
2023-02-05 18:59:39 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const [rect, setRect] = useState({
|
|
|
|
|
height: 0,
|
|
|
|
|
width: 0,
|
|
|
|
|
});
|
2023-03-29 14:27:25 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const [debounced] = useDebouncedValue(rect, 25);
|
2023-03-29 14:27:25 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handlePlayPlaylist = useCallback(
|
|
|
|
|
(id: string, playType: Play) => {
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: {
|
|
|
|
|
id: [id],
|
|
|
|
|
type: LibraryItem.PLAYLIST,
|
|
|
|
|
},
|
|
|
|
|
playType,
|
|
|
|
|
});
|
2023-02-08 03:44:37 -08:00
|
|
|
},
|
2023-07-01 19:10:05 -07:00
|
|
|
[handlePlayQueueAdd],
|
|
|
|
|
);
|
2023-02-08 03:44:37 -08:00
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
const data = playlistsQuery.data;
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const memoizedItemData = useMemo(() => {
|
2024-09-26 04:23:08 +00:00
|
|
|
const base = { handlePlay: handlePlayPlaylist };
|
2024-02-18 20:22:38 -08:00
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
if (!server?.type || !server?.username || !data?.items) {
|
2024-02-18 20:22:38 -08:00
|
|
|
return { ...base, items: data?.items };
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 21:09:44 -07:00
|
|
|
const owned: Array<Playlist | [boolean, () => void]> = [];
|
2024-02-18 20:22:38 -08:00
|
|
|
const shared: Playlist[] = [];
|
|
|
|
|
|
|
|
|
|
for (const playlist of data.items) {
|
2024-09-26 04:23:08 +00:00
|
|
|
if (playlist.owner && playlist.owner !== server.username) {
|
2024-02-18 20:22:38 -08:00
|
|
|
shared.push(playlist);
|
|
|
|
|
} else {
|
|
|
|
|
owned.push(playlist);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shared.length > 0) {
|
2024-08-24 21:09:44 -07:00
|
|
|
owned.push([sidebarCollapseShared, toggleSidebarCollapseShare]);
|
2024-02-18 20:22:38 -08:00
|
|
|
}
|
|
|
|
|
|
2024-08-24 21:09:44 -07:00
|
|
|
const final = sidebarCollapseShared ? owned : owned.concat(shared);
|
|
|
|
|
|
|
|
|
|
return { ...base, items: final };
|
|
|
|
|
}, [
|
|
|
|
|
data?.items,
|
|
|
|
|
handlePlayPlaylist,
|
2024-09-26 04:23:08 +00:00
|
|
|
server?.type,
|
|
|
|
|
server?.username,
|
|
|
|
|
sidebarCollapseShared,
|
2024-08-24 21:09:44 -07:00
|
|
|
toggleSidebarCollapseShare,
|
|
|
|
|
]);
|
2023-02-05 18:59:39 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
h="100%"
|
|
|
|
|
{...hideScrollbarElementProps}
|
|
|
|
|
>
|
|
|
|
|
<AutoSizer onResize={(e) => setRect(e as { height: number; width: number })}>
|
|
|
|
|
{() => (
|
|
|
|
|
<FixedSizeList
|
|
|
|
|
className={
|
|
|
|
|
isScrollbarHidden
|
|
|
|
|
? 'hide-scrollbar overlay-scrollbar'
|
|
|
|
|
: 'overlay-scrollbar'
|
|
|
|
|
}
|
|
|
|
|
height={debounced.height}
|
2024-02-18 20:22:38 -08:00
|
|
|
itemCount={memoizedItemData?.items?.length || 0}
|
2023-07-01 19:10:05 -07:00
|
|
|
itemData={memoizedItemData}
|
|
|
|
|
itemSize={25}
|
|
|
|
|
overscanCount={20}
|
|
|
|
|
width={debounced.width}
|
|
|
|
|
>
|
|
|
|
|
{PlaylistRow}
|
|
|
|
|
</FixedSizeList>
|
|
|
|
|
)}
|
|
|
|
|
</AutoSizer>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
2023-02-05 18:59:39 -08:00
|
|
|
};
|