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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-04 18:00:35 +01:00
|
|
|
import { ReactNode, useMemo } from 'react';
|
2024-10-03 04:02:59 +02:00
|
|
|
import Button from '@mui/material/Button';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
|
|
|
|
import { 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>({
|
2024-12-09 16:24:32 +01:00
|
|
|
tooltip,
|
2024-10-03 04:02:59 +02:00
|
|
|
value,
|
2024-12-13 16:38:41 +01:00
|
|
|
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 }) => {
|
2026-01-10 19:45:02 +01:00
|
|
|
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 (
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={tooltip}>
|
2024-12-09 16:24:32 +01:00
|
|
|
{isDefault ? (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => setValue(values[0])}
|
|
|
|
|
sx={{ justifyContent: 'start', textTransform: 'unset', flexGrow: 1 }}
|
|
|
|
|
variant="contained"
|
|
|
|
|
startIcon={defaultIcon}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
2024-12-13 16:38:41 +01:00
|
|
|
{defaultValue === undefined ? (
|
2026-01-10 19:45:02 +01:00
|
|
|
t`Default`
|
2024-12-13 16:38:41 +01:00
|
|
|
) : (
|
|
|
|
|
<Superscript
|
2026-01-10 19:45:02 +01:00
|
|
|
superscript={`(${t`Default`})`}
|
|
|
|
|
text={
|
2024-12-13 16:38:41 +01:00
|
|
|
valueToDisplayData[defaultValue].isTitleString
|
|
|
|
|
? valueToDisplayData[defaultValue].title
|
|
|
|
|
: t(valueToDisplayData[defaultValue].title)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-12-09 16:24:32 +01:00
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const nextValue = getNextRotationValue(indexOfValue, values, isDefaultable);
|
2024-11-04 18:00:35 +01:00
|
|
|
|
2024-12-09 16:24:32 +01:00
|
|
|
if (nextValue === undefined) {
|
|
|
|
|
onDefault?.();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-04 18:00:35 +01:00
|
|
|
|
2024-12-09 16:24:32 +01:00
|
|
|
setValue(nextValue);
|
|
|
|
|
}}
|
|
|
|
|
sx={{ justifyContent: 'start', textTransform: 'unset', flexGrow: 1 }}
|
|
|
|
|
variant="contained"
|
|
|
|
|
startIcon={valueToDisplayData[value].icon}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
{valueToDisplayData[value].isTitleString
|
|
|
|
|
? valueToDisplayData[value].title
|
|
|
|
|
: t(valueToDisplayData[value].title)}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-10-03 04:02:59 +02:00
|
|
|
);
|
|
|
|
|
};
|