Files
suwayomi-material-you-webui/src/base/components/buttons/ValueRotationButton.tsx

88 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-10-03 04:02:59 +02:00
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2026-03-11 00:11:29 +01:00
import type { ReactNode } from 'react';
import { useMemo } from 'react';
2024-10-03 04:02:59 +02:00
import Button from '@mui/material/Button';
import { useLingui } from '@lingui/react/macro';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
2026-03-11 00:11:29 +01:00
import type { MultiValueButtonProps } from '@/base/Base.types.ts';
import { getNextRotationValue } from '@/base/utils/ValueRotationButton.utils.ts';
import { Superscript } from '@/base/components/texts/Superscript.tsx';
2024-10-03 04:02:59 +02:00
export const ValueRotationButton = <Value extends string | number>({
tooltip,
2024-10-03 04:02:59 +02:00
value,
defaultValue,
2024-10-03 04:02:59 +02:00
values,
setValue,
valueToDisplayData,
2024-11-04 18:00:35 +01:00
isDefaultable,
onDefault,
defaultIcon,
}: MultiValueButtonProps<Value> & { defaultIcon?: ReactNode }) => {
const { t } = useLingui();
2024-10-03 04:02:59 +02:00
2024-11-04 18:00:35 +01:00
const isDefault = value === undefined;
const indexOfValue = useMemo(() => {
if (isDefault) {
return -1;
}
return values.indexOf(value);
}, [value, values]);
2024-10-03 04:02:59 +02:00
return (
<CustomTooltip title={tooltip}>
{isDefault ? (
<Button
onClick={() => setValue(values[0])}
sx={{ justifyContent: 'start', textTransform: 'unset', flexGrow: 1 }}
variant="contained"
startIcon={defaultIcon}
size="large"
>
{defaultValue === undefined ? (
t`Default`
) : (
<Superscript
superscript={`(${t`Default`})`}
text={
typeof valueToDisplayData[defaultValue].title === 'string'
? valueToDisplayData[defaultValue].title
: t(valueToDisplayData[defaultValue].title)
}
/>
)}
</Button>
) : (
<Button
onClick={() => {
const nextValue = getNextRotationValue(indexOfValue, values, isDefaultable);
2024-11-04 18:00:35 +01:00
if (nextValue === undefined) {
onDefault?.();
return;
}
2024-11-04 18:00:35 +01:00
setValue(nextValue);
}}
sx={{ justifyContent: 'start', textTransform: 'unset', flexGrow: 1 }}
variant="contained"
startIcon={valueToDisplayData[value].icon}
size="large"
>
{typeof valueToDisplayData[value].title === 'string'
? valueToDisplayData[value].title
: t(valueToDisplayData[value].title)}
</Button>
)}
</CustomTooltip>
2024-10-03 04:02:59 +02:00
);
};