stash/ui/v2.5/src/components/Shared/PopoverCountButton.tsx
Joe Scylla 9083796a42
#1810 Truncate large numbers on buttons (#2781)
* #1810 Truncate large numbers on buttons
* Apply to card popovers as well

Co-authored-by: Roland Karle <roland.karle@aufwind-group.de>
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
2022-10-06 12:43:17 +11:00

107 lines
2.4 KiB
TypeScript

import {
faFilm,
faImage,
faImages,
faPlayCircle,
} from "@fortawesome/free-solid-svg-icons";
import React, { useMemo } from "react";
import { Button } from "react-bootstrap";
import { FormattedNumber, useIntl } from "react-intl";
import { Link } from "react-router-dom";
import { IUIConfig } from "src/core/config";
import { ConfigurationContext } from "src/hooks/Config";
import { TextUtils } from "src/utils";
import Icon from "./Icon";
type PopoverLinkType = "scene" | "image" | "gallery" | "movie";
interface IProps {
className?: string;
url: string;
type: PopoverLinkType;
count: number;
}
export const PopoverCountButton: React.FC<IProps> = ({
className,
url,
type,
count,
}) => {
const { configuration } = React.useContext(ConfigurationContext);
const abbreviateCounter =
(configuration?.ui as IUIConfig)?.abbreviateCounters ?? false;
const intl = useIntl();
function getIcon() {
switch (type) {
case "scene":
return faPlayCircle;
case "image":
return faImage;
case "gallery":
return faImages;
case "movie":
return faFilm;
}
}
function getPluralOptions() {
switch (type) {
case "scene":
return {
one: "scene",
other: "scenes",
};
case "image":
return {
one: "image",
other: "images",
};
case "gallery":
return {
one: "gallery",
other: "galleries",
};
case "movie":
return {
one: "movie",
other: "movies",
};
}
}
function getTitle() {
const pluralCategory = intl.formatPlural(count);
const options = getPluralOptions();
const plural = options[pluralCategory as "one"] || options.other;
return `${count} ${plural}`;
}
const countEl = useMemo(() => {
if (!abbreviateCounter) {
return count;
}
const formatted = TextUtils.abbreviateCounter(count);
return (
<span>
<FormattedNumber
value={formatted.size}
maximumFractionDigits={formatted.digits}
/>
{formatted.unit}
</span>
);
}, [count, abbreviateCounter]);
return (
<Link className={className} to={url} title={getTitle()}>
<Button className="minimal">
<Icon icon={getIcon()} />
<span>{countEl}</span>
</Button>
</Link>
);
};