mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-16 16:34:24 +02:00
handle favorite/rating column mutations
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
import { useQueryClient, useSuspenseQuery, UseSuspenseQueryOptions } from '@tanstack/react-query';
|
import { useQueryClient, useSuspenseQuery, UseSuspenseQueryOptions } from '@tanstack/react-query';
|
||||||
import throttle from 'lodash/throttle';
|
import throttle from 'lodash/throttle';
|
||||||
import { useMemo, useRef, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
||||||
|
import { UserFavoriteEventPayload, UserRatingEventPayload } from '/@/renderer/events/events';
|
||||||
import { getServerById } from '/@/renderer/store';
|
import { getServerById } from '/@/renderer/store';
|
||||||
|
|
||||||
export interface InfiniteListProps<TQuery> {
|
|
||||||
itemsPerPage?: number;
|
|
||||||
query: Omit<TQuery, 'limit' | 'startIndex'>;
|
|
||||||
serverId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UseItemListInfiniteLoaderProps {
|
interface UseItemListInfiniteLoaderProps {
|
||||||
|
eventKey: string;
|
||||||
itemsPerPage: number;
|
itemsPerPage: number;
|
||||||
listCountQuery: UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
listCountQuery: UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
listQueryFn: (args: { apiClientProps: any; query: any }) => Promise<{ items: unknown[] }>;
|
listQueryFn: (args: { apiClientProps: any; query: any }) => Promise<{ items: unknown[] }>;
|
||||||
@@ -24,6 +21,7 @@ function getInitialData(itemCount: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useItemListInfiniteLoader = ({
|
export const useItemListInfiniteLoader = ({
|
||||||
|
eventKey,
|
||||||
itemsPerPage = 100,
|
itemsPerPage = 100,
|
||||||
listCountQuery,
|
listCountQuery,
|
||||||
listQueryFn,
|
listQueryFn,
|
||||||
@@ -32,6 +30,8 @@ export const useItemListInfiniteLoader = ({
|
|||||||
}: UseItemListInfiniteLoaderProps) => {
|
}: UseItemListInfiniteLoaderProps) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const currentPageRef = useRef(0);
|
||||||
|
|
||||||
const scrollStateRef = useRef<ScrollState>({
|
const scrollStateRef = useRef<ScrollState>({
|
||||||
direction: 'unknown',
|
direction: 'unknown',
|
||||||
lastRange: null,
|
lastRange: null,
|
||||||
@@ -56,6 +56,8 @@ export const useItemListInfiniteLoader = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentPageRef.current = pageNumber;
|
||||||
|
|
||||||
const queryParams = {
|
const queryParams = {
|
||||||
limit: fetchRange.limit,
|
limit: fetchRange.limit,
|
||||||
startIndex: fetchRange.startIndex,
|
startIndex: fetchRange.startIndex,
|
||||||
@@ -84,7 +86,103 @@ export const useItemListInfiniteLoader = ({
|
|||||||
}, 500);
|
}, 500);
|
||||||
}, [itemsPerPage, queryClient, serverId, listQueryFn, query]);
|
}, [itemsPerPage, queryClient, serverId, listQueryFn, query]);
|
||||||
|
|
||||||
return { data, onRangeChanged };
|
const refresh = useCallback(
|
||||||
|
async (force?: boolean) => {
|
||||||
|
await queryClient.invalidateQueries();
|
||||||
|
pagesLoaded.current = {};
|
||||||
|
|
||||||
|
if (force) {
|
||||||
|
setData(getInitialData(totalItemCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
await onRangeChanged({
|
||||||
|
endIndex: currentPageRef.current * itemsPerPage,
|
||||||
|
startIndex: currentPageRef.current * itemsPerPage,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[itemsPerPage, onRangeChanged, queryClient, totalItemCount],
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateItems = useCallback((indexes: number[], value: object) => {
|
||||||
|
setData((prev: any[]) => {
|
||||||
|
return prev.map((item: any, index) => {
|
||||||
|
if (!item) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!indexes.includes(index)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
...value,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleRefresh = (payload: { key: string }) => {
|
||||||
|
if (!eventKey || eventKey !== payload.key) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return refresh(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
eventEmitter.on('ITEM_LIST_REFRESH', handleRefresh);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
eventEmitter.off('ITEM_LIST_REFRESH', handleRefresh);
|
||||||
|
};
|
||||||
|
}, [eventKey, refresh]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleFavorite = (payload: UserFavoriteEventPayload) => {
|
||||||
|
const idToIndexMap = data
|
||||||
|
.filter(Boolean)
|
||||||
|
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||||
|
acc[item.id] = index;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||||
|
|
||||||
|
if (dataIndexes.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateItems(dataIndexes, { userFavorite: payload.favorite });
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleRating = (payload: UserRatingEventPayload) => {
|
||||||
|
const idToIndexMap = data
|
||||||
|
.filter(Boolean)
|
||||||
|
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||||
|
acc[item.id] = index;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||||
|
|
||||||
|
if (dataIndexes.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateItems(dataIndexes, { userRating: payload.rating });
|
||||||
|
};
|
||||||
|
|
||||||
|
eventEmitter.on('USER_FAVORITE', handleFavorite);
|
||||||
|
eventEmitter.on('USER_RATING', handleRating);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
eventEmitter.off('USER_FAVORITE', handleFavorite);
|
||||||
|
eventEmitter.off('USER_RATING', handleRating);
|
||||||
|
};
|
||||||
|
}, [data, eventKey, updateItems]);
|
||||||
|
|
||||||
|
return { data, onRangeChanged, refresh, updateItems };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseListCountQuery = (query: any) => {
|
export const parseListCountQuery = (query: any) => {
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
import { useQuery, useSuspenseQuery, UseSuspenseQueryOptions } from '@tanstack/react-query';
|
import {
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
useSuspenseQuery,
|
||||||
|
UseSuspenseQueryOptions,
|
||||||
|
} from '@tanstack/react-query';
|
||||||
|
import { useCallback, useEffect, useMemo } from 'react';
|
||||||
|
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
||||||
|
import { UserFavoriteEventPayload, UserRatingEventPayload } from '/@/renderer/events/events';
|
||||||
import { getServerById } from '/@/renderer/store';
|
import { getServerById } from '/@/renderer/store';
|
||||||
|
|
||||||
export interface PaginatedListProps<TQuery> {
|
|
||||||
initialPage?: number;
|
|
||||||
itemsPerPage?: number;
|
|
||||||
query: Omit<TQuery, 'limit' | 'startIndex'>;
|
|
||||||
serverId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UseItemListPaginatedLoaderProps {
|
interface UseItemListPaginatedLoaderProps {
|
||||||
currentPage: number;
|
currentPage: number;
|
||||||
|
eventKey?: string;
|
||||||
itemsPerPage: number;
|
itemsPerPage: number;
|
||||||
listCountQuery: UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
listCountQuery: UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
listQueryFn: (args: { apiClientProps: any; query: any }) => Promise<{ items: unknown[] }>;
|
listQueryFn: (args: { apiClientProps: any; query: any }) => Promise<{ items: unknown[] }>;
|
||||||
@@ -25,12 +27,14 @@ function getInitialData(itemCount: number) {
|
|||||||
|
|
||||||
export const useItemListPaginatedLoader = ({
|
export const useItemListPaginatedLoader = ({
|
||||||
currentPage,
|
currentPage,
|
||||||
|
eventKey,
|
||||||
itemsPerPage = 100,
|
itemsPerPage = 100,
|
||||||
listCountQuery,
|
listCountQuery,
|
||||||
listQueryFn,
|
listQueryFn,
|
||||||
query = {},
|
query = {},
|
||||||
serverId,
|
serverId,
|
||||||
}: UseItemListPaginatedLoaderProps) => {
|
}: UseItemListPaginatedLoaderProps) => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
const { data: totalItemCount } = useSuspenseQuery<number, any, number, any>(listCountQuery);
|
const { data: totalItemCount } = useSuspenseQuery<number, any, number, any>(listCountQuery);
|
||||||
|
|
||||||
const pageCount = Math.ceil(totalItemCount / itemsPerPage);
|
const pageCount = Math.ceil(totalItemCount / itemsPerPage);
|
||||||
@@ -38,13 +42,16 @@ export const useItemListPaginatedLoader = ({
|
|||||||
const fetchRange = getFetchRange(currentPage, itemsPerPage);
|
const fetchRange = getFetchRange(currentPage, itemsPerPage);
|
||||||
const startIndex = fetchRange.startIndex;
|
const startIndex = fetchRange.startIndex;
|
||||||
|
|
||||||
const queryParams = {
|
const queryParams = useMemo(
|
||||||
limit: itemsPerPage,
|
() => ({
|
||||||
startIndex: startIndex,
|
limit: itemsPerPage,
|
||||||
...query,
|
startIndex: startIndex,
|
||||||
};
|
...query,
|
||||||
|
}),
|
||||||
|
[itemsPerPage, startIndex, query],
|
||||||
|
);
|
||||||
|
|
||||||
const { data } = useQuery({
|
const { data, refetch: queryRefetch } = useQuery({
|
||||||
gcTime: 1000 * 15,
|
gcTime: 1000 * 15,
|
||||||
placeholderData: getInitialData(itemsPerPage),
|
placeholderData: getInitialData(itemsPerPage),
|
||||||
queryFn: async ({ signal }) => {
|
queryFn: async ({ signal }) => {
|
||||||
@@ -59,6 +66,102 @@ export const useItemListPaginatedLoader = ({
|
|||||||
staleTime: 1000 * 15,
|
staleTime: 1000 * 15,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const refresh = useCallback(() => {
|
||||||
|
return queryRefetch();
|
||||||
|
}, [queryRefetch]);
|
||||||
|
|
||||||
|
const updateItems = useCallback(
|
||||||
|
(indexes: number[], value: object) => {
|
||||||
|
return queryClient.setQueryData(
|
||||||
|
queryKeys.albums.list(serverId, queryParams),
|
||||||
|
(prev: undefined | unknown[]) => {
|
||||||
|
if (!prev) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
return prev.map((item: any, index) => {
|
||||||
|
if (!item) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!indexes.includes(index)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
...value,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[queryClient, queryParams, serverId],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleRefresh = (payload: { key: string }) => {
|
||||||
|
if (!eventKey || eventKey !== payload.key) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return refresh();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFavorite = (payload: UserFavoriteEventPayload) => {
|
||||||
|
if (!data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idToIndexMap = data
|
||||||
|
.filter(Boolean)
|
||||||
|
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||||
|
acc[item.id] = index;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||||
|
|
||||||
|
if (dataIndexes.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateItems(dataIndexes, { userFavorite: payload.favorite });
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleRating = (payload: UserRatingEventPayload) => {
|
||||||
|
if (!data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idToIndexMap = data.reduce(
|
||||||
|
(acc: Record<string, number>, item: any, index: number) => {
|
||||||
|
acc[item.id] = index;
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
);
|
||||||
|
|
||||||
|
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||||
|
|
||||||
|
if (dataIndexes.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateItems(dataIndexes, { userRating: payload.rating });
|
||||||
|
};
|
||||||
|
|
||||||
|
eventEmitter.on('ITEM_LIST_REFRESH', handleRefresh);
|
||||||
|
eventEmitter.on('USER_FAVORITE', handleFavorite);
|
||||||
|
eventEmitter.on('USER_RATING', handleRating);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
eventEmitter.off('ITEM_LIST_REFRESH', handleRefresh);
|
||||||
|
eventEmitter.off('USER_FAVORITE', handleFavorite);
|
||||||
|
eventEmitter.off('USER_RATING', handleRating);
|
||||||
|
};
|
||||||
|
}, [data, eventKey, refresh, updateItems]);
|
||||||
|
|
||||||
return { data, pageCount, totalItemCount };
|
return { data, pageCount, totalItemCount };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { memo, useMemo } from 'react';
|
import { Fragment, memo, useMemo } from 'react';
|
||||||
import { generatePath, Link } from 'react-router-dom';
|
import { generatePath, Link } from 'react-router-dom';
|
||||||
|
|
||||||
import styles from './album-artists-column.module.css';
|
import styles from './album-artists-column.module.css';
|
||||||
@@ -39,17 +39,12 @@ const AlbumArtistsColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
{albumArtists.map((albumArtist, index) => (
|
{albumArtists.map((albumArtist, index) => (
|
||||||
<Text
|
<Fragment key={albumArtist.id}>
|
||||||
component={Link}
|
<Text component={Link} isLink isMuted isNoSelect to={albumArtist.path}>
|
||||||
isLink
|
{albumArtist.name}
|
||||||
isMuted
|
</Text>
|
||||||
isNoSelect
|
|
||||||
key={albumArtist.id}
|
|
||||||
to={albumArtist.path}
|
|
||||||
>
|
|
||||||
{albumArtist.name}
|
|
||||||
{index < albumArtists.length - 1 && ', '}
|
{index < albumArtists.length - 1 && ', '}
|
||||||
</Text>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</TableColumnContainer>
|
</TableColumnContainer>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { memo, useMemo } from 'react';
|
import { Fragment, memo, useMemo } from 'react';
|
||||||
import { generatePath, Link } from 'react-router-dom';
|
import { generatePath, Link } from 'react-router-dom';
|
||||||
|
|
||||||
import styles from './album-artists-column.module.css';
|
import styles from './album-artists-column.module.css';
|
||||||
@@ -39,17 +39,12 @@ const ArtistsColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
{artists.map((artist, index) => (
|
{artists.map((artist, index) => (
|
||||||
<Text
|
<Fragment key={artist.id}>
|
||||||
component={Link}
|
<Text component={Link} isLink isMuted isNoSelect to={artist.path}>
|
||||||
isLink
|
{artist.name}
|
||||||
isMuted
|
</Text>
|
||||||
isNoSelect
|
|
||||||
key={artist.id}
|
|
||||||
to={artist.path}
|
|
||||||
>
|
|
||||||
{artist.name}
|
|
||||||
{index < artists.length - 1 && ', '}
|
{index < artists.length - 1 && ', '}
|
||||||
</Text>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</TableColumnContainer>
|
</TableColumnContainer>
|
||||||
|
|||||||
@@ -2,13 +2,19 @@ import {
|
|||||||
ItemTableListInnerColumn,
|
ItemTableListInnerColumn,
|
||||||
TableColumnContainer,
|
TableColumnContainer,
|
||||||
} from '/@/renderer/components/item-list/item-table-list/item-table-list-column';
|
} from '/@/renderer/components/item-list/item-table-list/item-table-list-column';
|
||||||
|
import { useCreateFavorite } from '/@/renderer/features/shared/mutations/create-favorite-mutation';
|
||||||
|
import { useDeleteFavorite } from '/@/renderer/features/shared/mutations/delete-favorite-mutation';
|
||||||
import { ActionIcon } from '/@/shared/components/action-icon/action-icon';
|
import { ActionIcon } from '/@/shared/components/action-icon/action-icon';
|
||||||
|
import { LibraryItem } from '/@/shared/types/domain-types';
|
||||||
|
|
||||||
export const FavoriteColumn = (props: ItemTableListInnerColumn) => {
|
export const FavoriteColumn = (props: ItemTableListInnerColumn) => {
|
||||||
const row: boolean | undefined = (props.data as (any | undefined)[])[props.rowIndex]?.[
|
const row: boolean | undefined = (props.data as (any | undefined)[])[props.rowIndex]?.[
|
||||||
props.columns[props.columnIndex].id
|
props.columns[props.columnIndex].id
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const createFavorite = useCreateFavorite({});
|
||||||
|
const deleteFavorite = useDeleteFavorite({});
|
||||||
|
|
||||||
if (typeof row === 'boolean') {
|
if (typeof row === 'boolean') {
|
||||||
return (
|
return (
|
||||||
<TableColumnContainer {...props}>
|
<TableColumnContainer {...props}>
|
||||||
@@ -20,6 +26,25 @@ export const FavoriteColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
fill: row ? 'primary' : undefined,
|
fill: row ? 'primary' : undefined,
|
||||||
size: 'md',
|
size: 'md',
|
||||||
}}
|
}}
|
||||||
|
onClick={() => {
|
||||||
|
if (row) {
|
||||||
|
deleteFavorite.mutate({
|
||||||
|
query: {
|
||||||
|
id: [(props.data as any)?.[props.rowIndex]?.id as string],
|
||||||
|
type: (props.data as any)?.[props.rowIndex]
|
||||||
|
?.itemType as LibraryItem,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
createFavorite.mutate({
|
||||||
|
query: {
|
||||||
|
id: [(props.data as any)?.[props.rowIndex]?.id as string],
|
||||||
|
type: (props.data as any)?.[props.rowIndex]
|
||||||
|
?.itemType as LibraryItem,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
size="xs"
|
size="xs"
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -2,8 +2,9 @@
|
|||||||
display: -webkit-box;
|
display: -webkit-box;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
-webkit-line-clamp: 2;
|
-webkit-line-clamp: 2;
|
||||||
-webkit-box-orient: vertical;
|
|
||||||
color: var(--theme-colors-foreground-muted);
|
color: var(--theme-colors-foreground-muted);
|
||||||
|
user-select: none;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
}
|
}
|
||||||
|
|
||||||
.genres-container.compact {
|
.genres-container.compact {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { memo, useMemo } from 'react';
|
import { Fragment, memo, useMemo } from 'react';
|
||||||
import { generatePath, Link } from 'react-router-dom';
|
import { generatePath, Link } from 'react-router-dom';
|
||||||
|
|
||||||
import styles from './genre-column.module.css';
|
import styles from './genre-column.module.css';
|
||||||
@@ -39,17 +39,12 @@ const GenreColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
{genres.map((genre, index) => (
|
{genres.map((genre, index) => (
|
||||||
<Text
|
<Fragment key={genre.id}>
|
||||||
component={Link}
|
<Text component={Link} isLink isMuted isNoSelect to={genre.path}>
|
||||||
isLink
|
{genre.name}
|
||||||
isMuted
|
</Text>
|
||||||
isNoSelect
|
|
||||||
key={genre.id}
|
|
||||||
to={genre.path}
|
|
||||||
>
|
|
||||||
{genre.name}
|
|
||||||
{index < genres.length - 1 && ', '}
|
{index < genres.length - 1 && ', '}
|
||||||
</Text>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</TableColumnContainer>
|
</TableColumnContainer>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
ItemTableListInnerColumn,
|
ItemTableListInnerColumn,
|
||||||
TableColumnContainer,
|
TableColumnContainer,
|
||||||
} from '/@/renderer/components/item-list/item-table-list/item-table-list-column';
|
} from '/@/renderer/components/item-list/item-table-list/item-table-list-column';
|
||||||
|
import { useSetRating } from '/@/renderer/features/shared/mutations/set-rating-mutation';
|
||||||
import { Rating } from '/@/shared/components/rating/rating';
|
import { Rating } from '/@/shared/components/rating/rating';
|
||||||
|
|
||||||
export const RatingColumn = (props: ItemTableListInnerColumn) => {
|
export const RatingColumn = (props: ItemTableListInnerColumn) => {
|
||||||
@@ -9,11 +10,34 @@ export const RatingColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
props.columns[props.columnIndex].id
|
props.columns[props.columnIndex].id
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const setRatingMutation = useSetRating({});
|
||||||
|
|
||||||
|
const handleChangeRating = (rating: number) => {
|
||||||
|
const previousRating = row || 0;
|
||||||
|
|
||||||
|
let newRating = rating;
|
||||||
|
|
||||||
|
if (previousRating === rating) {
|
||||||
|
newRating = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = props.data[props.rowIndex] as any;
|
||||||
|
|
||||||
|
setRatingMutation.mutate({
|
||||||
|
query: {
|
||||||
|
item: [item],
|
||||||
|
rating: newRating,
|
||||||
|
},
|
||||||
|
serverId: item.serverId as string,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (typeof row === 'number' || row === null) {
|
if (typeof row === 'number' || row === null) {
|
||||||
return (
|
return (
|
||||||
<TableColumnContainer {...props}>
|
<TableColumnContainer {...props}>
|
||||||
<Rating
|
<Rating
|
||||||
className={row ? undefined : 'hover-only-flex'}
|
className={row ? undefined : 'hover-only-flex'}
|
||||||
|
onChange={handleChangeRating}
|
||||||
size="xs"
|
size="xs"
|
||||||
value={row || 0}
|
value={row || 0}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export const RowIndexColumn = (props: ItemTableListInnerColumn) => {
|
|||||||
size="xs"
|
size="xs"
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
/>
|
/>
|
||||||
<Text className="hide-on-hover" isMuted>
|
<Text className="hide-on-hover" isMuted isNoSelect>
|
||||||
{props.rowIndex}
|
{props.rowIndex}
|
||||||
</Text>
|
</Text>
|
||||||
</TableColumnContainer>
|
</TableColumnContainer>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import isElectron from 'is-electron';
|
|||||||
|
|
||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
||||||
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
||||||
import { useFavoriteEvent } from '/@/renderer/store/event.store';
|
import { useFavoriteEvent } from '/@/renderer/store/event.store';
|
||||||
@@ -31,6 +32,22 @@ export const useCreateFavorite = (args: MutationHookArgs) => {
|
|||||||
apiClientProps: { serverId: args.apiClientProps.serverId },
|
apiClientProps: { serverId: args.apiClientProps.serverId },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
onError: (_error, variables) => {
|
||||||
|
eventEmitter.emit('USER_FAVORITE', {
|
||||||
|
favorite: false,
|
||||||
|
id: variables.query.id,
|
||||||
|
itemType: variables.query.type,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onMutate: (variables) => {
|
||||||
|
eventEmitter.emit('USER_FAVORITE', {
|
||||||
|
favorite: true,
|
||||||
|
id: variables.query.id,
|
||||||
|
itemType: variables.query.type,
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
},
|
||||||
onSuccess: (_data, variables) => {
|
onSuccess: (_data, variables) => {
|
||||||
const { apiClientProps } = variables;
|
const { apiClientProps } = variables;
|
||||||
const serverId = apiClientProps.serverId;
|
const serverId = apiClientProps.serverId;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import isElectron from 'is-electron';
|
|||||||
|
|
||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
||||||
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
||||||
import { useFavoriteEvent } from '/@/renderer/store/event.store';
|
import { useFavoriteEvent } from '/@/renderer/store/event.store';
|
||||||
@@ -31,6 +32,22 @@ export const useDeleteFavorite = (args: MutationHookArgs) => {
|
|||||||
apiClientProps: { serverId: args.apiClientProps.serverId },
|
apiClientProps: { serverId: args.apiClientProps.serverId },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
onError: (_error, variables) => {
|
||||||
|
eventEmitter.emit('USER_FAVORITE', {
|
||||||
|
favorite: true,
|
||||||
|
id: variables.query.id,
|
||||||
|
itemType: variables.query.type,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onMutate: (variables) => {
|
||||||
|
eventEmitter.emit('USER_FAVORITE', {
|
||||||
|
favorite: false,
|
||||||
|
id: variables.query.id,
|
||||||
|
itemType: variables.query.type,
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
},
|
||||||
onSuccess: (_data, variables) => {
|
onSuccess: (_data, variables) => {
|
||||||
const { apiClientProps } = variables;
|
const { apiClientProps } = variables;
|
||||||
const serverId = apiClientProps.serverId;
|
const serverId = apiClientProps.serverId;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import isElectron from 'is-electron';
|
|||||||
|
|
||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { eventEmitter } from '/@/renderer/events/event-emitter';
|
||||||
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
import { MutationHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
import { useSetAlbumListItemDataById } from '/@/renderer/store';
|
||||||
import { useRatingEvent } from '/@/renderer/store/event.store';
|
import { useRatingEvent } from '/@/renderer/store/event.store';
|
||||||
@@ -53,6 +54,12 @@ export const useSetRating = (args: MutationHookArgs) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onMutate: (variables) => {
|
onMutate: (variables) => {
|
||||||
|
eventEmitter.emit('USER_RATING', {
|
||||||
|
id: variables.query.item.map((item) => item.id),
|
||||||
|
itemType: variables.query.item[0].itemType,
|
||||||
|
rating: variables.query.rating,
|
||||||
|
});
|
||||||
|
|
||||||
const songIds: string[] = [];
|
const songIds: string[] = [];
|
||||||
for (const item of variables.query.item) {
|
for (const item of variables.query.item) {
|
||||||
switch (item.itemType) {
|
switch (item.itemType) {
|
||||||
|
|||||||
Reference in New Issue
Block a user