Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Navidrome smart playlist operators (up to v0.52.0) #604

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"owner": "owner",
"path": "path",
"playerMustBePaused": "player must be paused",
"preview": "preview",
"previousSong": "previous $t(entity.track_one)",
"quit": "quit",
"random": "random",
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/api/navidrome.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ export const NDSongQueryFields = [
{ label: 'File Type', type: 'string', value: 'filetype' },
{ label: 'Genre', type: 'string', value: 'genre' },
{ label: 'Has CoverArt', type: 'boolean', value: 'hascoverart' },
{ label: 'Playlist', type: 'playlist', value: 'id' },
{ label: 'Is Compilation', type: 'boolean', value: 'compilation' },
{ label: 'Is Favorite', type: 'boolean', value: 'loved' },
{ label: 'Lyrics', type: 'string', value: 'lyrics' },
Expand All @@ -415,6 +416,11 @@ export const NDSongQueryFields = [
{ label: 'Year', type: 'number', value: 'year' },
];

export const NDSongQueryPlaylistOperators = [
{ label: 'is in', value: 'inPlaylist' },
{ label: 'is not in', value: 'notInPlaylist' },
];

export const NDSongQueryDateOperators = [
{ label: 'is', value: 'is' },
{ label: 'is not', value: 'isNot' },
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/components/query-builder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ interface QueryBuilderProps {
boolean: { label: string; value: string }[];
date: { label: string; value: string }[];
number: { label: string; value: string }[];
playlist: { label: string; value: string }[];
string: { label: string; value: string }[];
};
playlists?: { label: string; value: string }[];
uniqueId: string;
}

Expand All @@ -73,6 +75,7 @@ export const QueryBuilder = ({
onChangeValue,
onClearFilters,
onResetFilters,
playlists,
groupIndex,
uniqueId,
filters,
Expand Down Expand Up @@ -180,6 +183,7 @@ export const QueryBuilder = ({
level={level}
noRemove={data?.rules?.length === 1}
operators={operators}
selectData={playlists}
onChangeField={onChangeField}
onChangeOperator={onChangeOperator}
onChangeValue={onChangeValue}
Expand All @@ -204,6 +208,7 @@ export const QueryBuilder = ({
groupIndex={[...(groupIndex || []), index]}
level={level + 1}
operators={operators}
playlists={playlists}
uniqueId={group.uniqueId}
onAddRule={onAddRule}
onAddRuleGroup={onAddRuleGroup}
Expand Down
20 changes: 13 additions & 7 deletions src/renderer/components/query-builder/query-builder-option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ interface QueryOptionProps {
number: { label: string; value: string }[];
string: { label: string; value: string }[];
};
selectData?: { label: string; value: string }[];
}

const QueryValueInput = ({ onChange, type, ...props }: any) => {
const QueryValueInput = ({ onChange, type, data, ...props }: any) => {
const [numberRange, setNumberRange] = useState([0, 0]);

switch (type) {
Expand Down Expand Up @@ -59,7 +60,6 @@ const QueryValueInput = ({ onChange, type, ...props }: any) => {
{...props}
/>
);

case 'dateRange':
return (
<>
Expand Down Expand Up @@ -87,7 +87,6 @@ const QueryValueInput = ({ onChange, type, ...props }: any) => {
/>
</>
);

case 'boolean':
return (
<Select
Expand All @@ -99,6 +98,14 @@ const QueryValueInput = ({ onChange, type, ...props }: any) => {
{...props}
/>
);
case 'playlist':
return (
<Select
data={data}
onChange={onChange}
{...props}
/>
);

default:
return <></>;
Expand All @@ -116,6 +123,7 @@ export const QueryBuilderOption = ({
onChangeField,
onChangeOperator,
onChangeValue,
selectData,
}: QueryOptionProps) => {
const { field, operator, uniqueId, value } = data;

Expand All @@ -133,10 +141,7 @@ export const QueryBuilderOption = ({

const handleChangeValue = (e: any) => {
const isDirectValue =
typeof e === 'string' ||
typeof e === 'number' ||
typeof e === 'undefined' ||
typeof e === null;
typeof e === 'string' || typeof e === 'number' || typeof e === 'undefined';

if (isDirectValue) {
return onChangeValue({
Expand Down Expand Up @@ -207,6 +212,7 @@ export const QueryBuilderOption = ({
/>
{field ? (
<QueryValueInput
data={selectData || []}
defaultValue={value}
maxWidth={170}
size="sm"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { forwardRef, Ref, useImperativeHandle, useState } from 'react';
import { forwardRef, Ref, useImperativeHandle, useMemo, useState } from 'react';
import { Group } from '@mantine/core';
import { useForm } from '@mantine/form';
import { openModal } from '@mantine/modals';
import clone from 'lodash/clone';
import get from 'lodash/get';
import setWith from 'lodash/setWith';
Expand All @@ -21,14 +22,18 @@ import {
import { QueryBuilderGroup, QueryBuilderRule } from '/@/renderer/types';
import { useTranslation } from 'react-i18next';
import { RiMore2Fill, RiSaveLine } from 'react-icons/ri';
import { SongListSort } from '/@/renderer/api/types';
import { PlaylistListSort, SongListSort, SortOrder } from '/@/renderer/api/types';
import {
NDSongQueryBooleanOperators,
NDSongQueryDateOperators,
NDSongQueryFields,
NDSongQueryNumberOperators,
NDSongQueryPlaylistOperators,
NDSongQueryStringOperators,
} from '/@/renderer/api/navidrome.types';
import { usePlaylistList } from '/@/renderer/features/playlists/queries/playlist-list-query';
import { useCurrentServer } from '/@/renderer/store';
import { JsonPreview } from '/@/renderer/features/shared/components/json-preview';

type AddArgs = {
groupIndex: number[];
Expand All @@ -52,6 +57,7 @@ interface PlaylistQueryBuilderProps {
parsedFilter: any,
extraFilters: { limit?: number; sortBy?: string; sortOrder?: string },
) => void;
playlistId?: string;
query: any;
sortBy: SongListSort;
sortOrder: 'asc' | 'desc';
Expand Down Expand Up @@ -84,14 +90,43 @@ export type PlaylistQueryBuilderRef = {

export const PlaylistQueryBuilder = forwardRef(
(
{ sortOrder, sortBy, limit, isSaving, query, onSave, onSaveAs }: PlaylistQueryBuilderProps,
{
sortOrder,
sortBy,
limit,
isSaving,
query,
onSave,
onSaveAs,
playlistId,
}: PlaylistQueryBuilderProps,
ref: Ref<PlaylistQueryBuilderRef>,
) => {
const { t } = useTranslation();
const server = useCurrentServer();
const [filters, setFilters] = useState<QueryBuilderGroup>(
query ? convertNDQueryToQueryGroup(query) : DEFAULT_QUERY,
);

const { data: playlists } = usePlaylistList({
query: { sortBy: PlaylistListSort.NAME, sortOrder: SortOrder.ASC, startIndex: 0 },
serverId: server?.id,
});

const playlistData = useMemo(() => {
if (!playlists) return [];

return playlists.items
.filter((p) => {
if (!playlistId) return true;
return p.id !== playlistId;
})
.map((p) => ({
label: p.name,
value: p.id,
}));
}, [playlistId, playlists]);

const extraFiltersForm = useForm({
initialValues: {
limit,
Expand Down Expand Up @@ -131,6 +166,16 @@ export const PlaylistQueryBuilder = forwardRef(
onSaveAs?.(convertQueryGroupToNDQuery(filters), extraFiltersForm.values);
};

const openPreviewModal = () => {
const previewValue = convertQueryGroupToNDQuery(filters);

openModal({
children: <JsonPreview value={previewValue} />,
size: 'xl',
title: t('common.preview', { postProcess: 'titleCase' }),
});
};

const handleAddRuleGroup = (args: AddArgs) => {
const { level, groupIndex } = args;
const filtersCopy = clone(filters);
Expand Down Expand Up @@ -367,7 +412,7 @@ export const PlaylistQueryBuilder = forwardRef(
return (
<MotionFlex
direction="column"
h="calc(100% - 2.5rem)"
h="calc(100% - 3.5rem)"
justify="space-between"
>
<ScrollArea
Expand All @@ -383,8 +428,10 @@ export const PlaylistQueryBuilder = forwardRef(
boolean: NDSongQueryBooleanOperators,
date: NDSongQueryDateOperators,
number: NDSongQueryNumberOperators,
playlist: NDSongQueryPlaylistOperators,
string: NDSongQueryStringOperators,
}}
playlists={playlistData}
uniqueId={filters.uniqueId}
onAddRule={handleAddRule}
onAddRuleGroup={handleAddRuleGroup}
Expand Down Expand Up @@ -428,7 +475,7 @@ export const PlaylistQueryBuilder = forwardRef(
value: 'desc',
},
]}
label={t('common.order', { postProcess: 'titleCase' })}
label={t('common.sortOrder', { postProcess: 'titleCase' })}
maxWidth="20%"
width={125}
{...extraFiltersForm.getInputProps('sortOrder')}
Expand All @@ -452,6 +499,13 @@ export const PlaylistQueryBuilder = forwardRef(
>
{t('common.saveAs', { postProcess: 'titleCase' })}
</Button>
<Button
p="0.5em"
variant="default"
onClick={openPreviewModal}
>
{t('common.preview', { postProcess: 'titleCase' })}
</Button>
<DropdownMenu position="bottom-end">
<DropdownMenu.Target>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const PlaylistDetailSongListRoute = () => {
key={JSON.stringify(detailQuery?.data?.rules)}
isSaving={createPlaylistMutation?.isLoading}
limit={detailQuery?.data?.rules?.limit}
playlistId={playlistId}
query={detailQuery?.data?.rules}
sortBy={detailQuery?.data?.rules?.sort || SongListSort.ALBUM}
sortOrder={detailQuery?.data?.rules?.order || 'asc'}
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/features/shared/components/json-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface JsonPreviewProps {
value: string | Record<string, any>;
}

export const JsonPreview = ({ value }: JsonPreviewProps) => {
return <pre style={{ userSelect: 'all' }}>{JSON.stringify(value, null, 2)}</pre>;
};
Loading