ommit
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
This commit is contained in:
1 parent
cbb750a413
commit
497b9a2fc5
42 files changed
+945
-354
No files matched your search
@@ -0,0 +1,159 @@
|
||||
import { getUserDataApi } from "@jellyfin/sdk/lib/utils/api/user-data-api";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import Tooltip from "@mui/material/Tooltip";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useSnackbar } from "notistack";
|
||||
import React, { type CSSProperties } from "react";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
|
||||
import "./ratingControl.scss";
|
||||
|
||||
const LIKED_COLOR = "#65e6a3";
|
||||
const DISLIKED_COLOR = "#ff8a7a";
|
||||
|
||||
type RatingControlProps = {
|
||||
itemId?: string | null;
|
||||
/** Jellyfin Likes: true = like, false = dislike, null/undefined = none. */
|
||||
likes?: boolean | null;
|
||||
queryKey?: string[];
|
||||
userId?: string;
|
||||
itemName?: string | null;
|
||||
compact?: boolean;
|
||||
/** Keep the active state monochrome (filled icon, no accent colour). */
|
||||
monochrome?: boolean;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Thumb up / thumb down for music. This is the Jellyfin rating (`Likes`),
|
||||
* separate from the favorite heart: disliking a track does not remove it
|
||||
* from favorites.
|
||||
*/
|
||||
export default function RatingControl({
|
||||
itemId,
|
||||
likes,
|
||||
queryKey,
|
||||
userId,
|
||||
itemName,
|
||||
compact = false,
|
||||
monochrome = false,
|
||||
className,
|
||||
}: RatingControlProps) {
|
||||
const api = useApiInContext((state) => state.api);
|
||||
const queryClient = useQueryClient();
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
const [rating, setRating] = React.useState<boolean | null | undefined>(likes);
|
||||
|
||||
React.useEffect(() => {
|
||||
setRating(likes);
|
||||
}, [likes]);
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationKey: ["ratingControl", itemId],
|
||||
mutationFn: async (next: boolean | null) => {
|
||||
if (!api || !itemId || !userId) return null;
|
||||
const userDataApi = getUserDataApi(api);
|
||||
const response =
|
||||
next === null
|
||||
? await userDataApi.deleteUserItemRating({ itemId, userId })
|
||||
: await userDataApi.updateUserItemRating({
|
||||
itemId,
|
||||
userId,
|
||||
likes: next,
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
if (data) setRating(data.Likes ?? null);
|
||||
},
|
||||
onError: () => {
|
||||
setRating(likes);
|
||||
enqueueSnackbar(
|
||||
`Не удалось обновить оценку${itemName ? ` «${itemName}»` : ""}`,
|
||||
{ variant: "error" },
|
||||
);
|
||||
},
|
||||
onSettled: async () => {
|
||||
if (queryKey) {
|
||||
await queryClient.invalidateQueries({ queryKey });
|
||||
}
|
||||
// Keep item details (full player, floating bar, headers) in sync.
|
||||
if (itemId) {
|
||||
await queryClient.invalidateQueries({ queryKey: ["item", itemId] });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const updateRating = (next: boolean) => {
|
||||
if (!api || !itemId || !userId || mutation.isPending) return;
|
||||
const value = rating === next ? null : next;
|
||||
setRating(value);
|
||||
mutation.mutate(value);
|
||||
};
|
||||
|
||||
const disabled = !api || !itemId || !userId;
|
||||
const isLiked = rating === true;
|
||||
const isDisliked = rating === false;
|
||||
const size = compact ? "small" : "medium";
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`rating-control${compact ? " is-compact" : ""}${
|
||||
className ? ` ${className}` : ""
|
||||
}`}
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
onKeyDown={(event) => event.stopPropagation()}
|
||||
>
|
||||
<Tooltip title={isLiked ? "Убрать оценку «Нравится»" : "Нравится"}>
|
||||
<span>
|
||||
<IconButton
|
||||
size={size}
|
||||
disabled={disabled || mutation.isPending}
|
||||
aria-label="Нравится"
|
||||
aria-pressed={isLiked}
|
||||
onClick={() => updateRating(true)}
|
||||
sx={{ opacity: mutation.isPending ? 0.5 : 1 }}
|
||||
>
|
||||
<span
|
||||
className="material-symbols-rounded"
|
||||
style={
|
||||
{
|
||||
"--fill": isLiked ? 1 : 0,
|
||||
color: isLiked && !monochrome ? LIKED_COLOR : undefined,
|
||||
} as CSSProperties
|
||||
}
|
||||
>
|
||||
thumb_up
|
||||
</span>
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip
|
||||
title={isDisliked ? "Убрать оценку «Не нравится»" : "Не нравится"}
|
||||
>
|
||||
<span>
|
||||
<IconButton
|
||||
size={size}
|
||||
disabled={disabled || mutation.isPending}
|
||||
aria-label="Не нравится"
|
||||
aria-pressed={isDisliked}
|
||||
onClick={() => updateRating(false)}
|
||||
sx={{ opacity: mutation.isPending ? 0.5 : 1 }}
|
||||
>
|
||||
<span
|
||||
className="material-symbols-rounded"
|
||||
style={
|
||||
{
|
||||
"--fill": isDisliked ? 1 : 0,
|
||||
color: isDisliked && !monochrome ? DISLIKED_COLOR : undefined,
|
||||
} as CSSProperties
|
||||
}
|
||||
>
|
||||
thumb_down
|
||||
</span>
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user