2025-12-22 21:33:23 +01: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import TextField from '@mui/material/TextField';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
|
|
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import InputAdornment from '@mui/material/InputAdornment';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import { useTheme } from '@mui/material/styles';
|
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
|
|
|
|
import InputLabel from '@mui/material/InputLabel';
|
|
|
|
|
import FormControl from '@mui/material/FormControl';
|
|
|
|
|
import { d } from 'koration';
|
|
|
|
|
import { useElementSize } from '@mantine/hooks';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { MessageDescriptor } from '@lingui/core';
|
2025-12-28 02:07:52 +01:00
|
|
|
import { KeyValueItems } from '@/features/settings/components/images/KeyValueItems.tsx';
|
2025-12-22 21:33:23 +01:00
|
|
|
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
|
|
|
|
import {
|
|
|
|
|
IMAGE_PROCESSING_CALL_TIMEOUT,
|
|
|
|
|
IMAGE_PROCESSING_COMPRESSION,
|
|
|
|
|
IMAGE_PROCESSING_CONNECT_TIMEOUT,
|
|
|
|
|
IMAGE_PROCESSING_INPUT_WIDTH,
|
|
|
|
|
IMAGE_PROCESSING_TARGET_MODES_SELECT_VALUES,
|
2025-12-23 02:31:25 +01:00
|
|
|
TARGET_DISABLED,
|
2025-12-22 21:33:23 +01:00
|
|
|
} from '@/features/settings/Settings.constants.ts';
|
|
|
|
|
import { Select } from '@/base/components/inputs/Select.tsx';
|
|
|
|
|
import { TypographyMaxLines } from '@/base/components/texts/TypographyMaxLines.tsx';
|
|
|
|
|
import { MimeTypeTextField } from '@/features/settings/components/images/MimeTypeTextField.tsx';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { TSettingsDownloadConversion } from '@/features/settings/Settings.types.ts';
|
|
|
|
|
import { ImageProcessingTargetMode } from '@/features/settings/Settings.types.ts';
|
2025-12-22 21:33:23 +01:00
|
|
|
import {
|
2025-12-28 02:07:52 +01:00
|
|
|
getUpdatedSearchParams,
|
2025-12-22 21:33:23 +01:00
|
|
|
isDefaultMimeType,
|
2025-12-28 02:07:52 +01:00
|
|
|
isUrlTargetMode,
|
2025-12-22 21:33:23 +01:00
|
|
|
isValidCallTimeoutSetting,
|
|
|
|
|
isValidCompressionLevel,
|
|
|
|
|
isValidConnectTimeoutSetting,
|
|
|
|
|
} from '@/features/settings/ImageProcessing.utils.ts';
|
2025-12-28 02:07:52 +01:00
|
|
|
import { UrlUtil } from '@/lib/UrlUtil.ts';
|
2025-12-22 21:33:23 +01:00
|
|
|
|
|
|
|
|
export const Processing = ({
|
|
|
|
|
conversion,
|
2025-12-28 02:07:52 +01:00
|
|
|
conversion: { mode, mimeType, target, compressionLevel, headers, searchParams, callTimeout, connectTimeout },
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange,
|
|
|
|
|
isDuplicate,
|
|
|
|
|
}: {
|
|
|
|
|
conversion: TSettingsDownloadConversion;
|
|
|
|
|
onChange: (newConversion: TSettingsDownloadConversion | null) => void;
|
|
|
|
|
isDuplicate: boolean;
|
|
|
|
|
}) => {
|
2026-01-10 19:45:02 +01:00
|
|
|
const { t } = useLingui();
|
2025-12-22 21:33:23 +01:00
|
|
|
const theme = useTheme();
|
|
|
|
|
|
|
|
|
|
const { ref: textFieldRef, height: textFieldHeight } = useElementSize();
|
|
|
|
|
|
2025-12-28 02:07:52 +01:00
|
|
|
const [areSearchParamsCollapsed, setAreSearchParamsCollapsed] = useState(true);
|
2025-12-22 21:33:23 +01:00
|
|
|
const [areHeadersCollapsed, setAreHeadersCollapsed] = useState(true);
|
|
|
|
|
|
2025-12-23 02:31:25 +01:00
|
|
|
const isDisabledMode = mode === ImageProcessingTargetMode.DISABLED;
|
2025-12-22 21:33:23 +01:00
|
|
|
const isImageMode = mode === ImageProcessingTargetMode.IMAGE;
|
2025-12-28 02:07:52 +01:00
|
|
|
const isUrlMode = mode === ImageProcessingTargetMode.URL;
|
2025-12-22 21:33:23 +01:00
|
|
|
|
|
|
|
|
const isCallTimeoutValid = isValidCallTimeoutSetting(callTimeout);
|
|
|
|
|
const isConnectTimeoutValid = isValidConnectTimeoutSetting(connectTimeout);
|
|
|
|
|
const isCompressionLevelValid = isValidCompressionLevel(compressionLevel);
|
|
|
|
|
const isDefault = isDefaultMimeType(mimeType) && !isDuplicate;
|
|
|
|
|
const isDisabled = isDefault && !target && compressionLevel == null;
|
|
|
|
|
|
|
|
|
|
return (
|
2025-12-22 22:04:05 +01:00
|
|
|
<Stack>
|
2025-12-22 21:33:23 +01:00
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
gap: 1,
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
flexWrap: 'nowrap',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
pt: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
gap: 1,
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'baseline',
|
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
|
[theme.breakpoints.down('md')]: {
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
width: '100%',
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<MimeTypeTextField
|
|
|
|
|
mode={ImageProcessingTargetMode.IMAGE}
|
|
|
|
|
shouldAutoFocus
|
|
|
|
|
isDefault={isDefault}
|
|
|
|
|
isDuplicate={isDuplicate}
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`MIME-Type`}
|
2025-12-22 21:33:23 +01:00
|
|
|
value={mimeType}
|
|
|
|
|
onUpdate={(value) => {
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
mimeType: value,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2025-12-22 22:04:05 +01:00
|
|
|
<TypographyMaxLines
|
|
|
|
|
sx={{
|
|
|
|
|
[theme.breakpoints.up('md')]: { mx: 1 },
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
→
|
|
|
|
|
</TypographyMaxLines>
|
2025-12-22 21:33:23 +01:00
|
|
|
<FormControl sx={{ minWidth: '100px' }}>
|
2026-01-10 19:45:02 +01:00
|
|
|
<InputLabel id="image-conversion-target-mode-label">{t`Target mode`}</InputLabel>
|
2025-12-22 21:33:23 +01:00
|
|
|
<Select
|
|
|
|
|
ref={textFieldRef}
|
|
|
|
|
id="image-conversion-target-mode"
|
|
|
|
|
labelId="image-conversion-target-mode-label"
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`Target mode`}
|
2025-12-22 21:33:23 +01:00
|
|
|
value={mode}
|
2025-12-28 02:07:52 +01:00
|
|
|
onChange={(e) => {
|
|
|
|
|
if (!isUrlMode) {
|
|
|
|
|
setAreSearchParamsCollapsed(true);
|
|
|
|
|
setAreHeadersCollapsed(true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
mode: e.target.value,
|
2025-12-23 02:31:25 +01:00
|
|
|
target:
|
|
|
|
|
e.target.value === ImageProcessingTargetMode.DISABLED ? TARGET_DISABLED : '',
|
2025-12-28 02:07:52 +01:00
|
|
|
headers: !isUrlMode ? null : headers,
|
|
|
|
|
searchParams: !isUrlMode ? null : searchParams,
|
|
|
|
|
});
|
|
|
|
|
}}
|
2025-12-22 21:33:23 +01:00
|
|
|
>
|
|
|
|
|
{IMAGE_PROCESSING_TARGET_MODES_SELECT_VALUES.map(([selectValue, { text: selectText }]) => (
|
|
|
|
|
<MenuItem key={selectValue} value={selectValue}>
|
2026-01-10 19:45:02 +01:00
|
|
|
{t(selectText as MessageDescriptor)}
|
2025-12-22 21:33:23 +01:00
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</FormControl>
|
2025-12-23 02:31:25 +01:00
|
|
|
{!isDisabledMode && (
|
|
|
|
|
<MimeTypeTextField
|
|
|
|
|
mode={mode}
|
|
|
|
|
shouldAutoFocus={false}
|
|
|
|
|
isDefault={false}
|
|
|
|
|
isDuplicate={false}
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`MIME-Type target`}
|
2025-12-23 02:31:25 +01:00
|
|
|
value={target}
|
2025-12-28 02:07:52 +01:00
|
|
|
onUpdate={(value) => {
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
2025-12-23 02:31:25 +01:00
|
|
|
target: value,
|
2025-12-28 02:07:52 +01:00
|
|
|
searchParams: isImageMode ? null : getUpdatedSearchParams(value, searchParams),
|
|
|
|
|
});
|
|
|
|
|
}}
|
2025-12-22 21:33:23 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
2025-12-23 02:31:25 +01:00
|
|
|
{(() => {
|
|
|
|
|
if (isDisabledMode) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isImageMode) {
|
|
|
|
|
return (
|
|
|
|
|
<TextField
|
|
|
|
|
sx={{ width: IMAGE_PROCESSING_INPUT_WIDTH }}
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`Compression level`}
|
2025-12-23 02:31:25 +01:00
|
|
|
value={compressionLevel ?? ''}
|
|
|
|
|
type="number"
|
|
|
|
|
error={!isCompressionLevelValid}
|
2026-01-10 19:45:02 +01:00
|
|
|
helperText={!isCompressionLevelValid ? t`Invalid input` : ''}
|
2025-12-23 02:31:25 +01:00
|
|
|
slotProps={{
|
|
|
|
|
input: {
|
|
|
|
|
inputProps: IMAGE_PROCESSING_COMPRESSION,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
compressionLevel: e.target.value ? Number(e.target.value) : null,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<TextField
|
|
|
|
|
sx={{ width: IMAGE_PROCESSING_INPUT_WIDTH }}
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`Call timeout`}
|
2025-12-23 02:31:25 +01:00
|
|
|
value={callTimeout ? d(callTimeout).seconds.inWholeSeconds : ''}
|
|
|
|
|
type="number"
|
|
|
|
|
error={!isCallTimeoutValid}
|
2026-01-10 19:45:02 +01:00
|
|
|
helperText={!isCallTimeoutValid ? t`Invalid input` : ''}
|
2025-12-23 02:31:25 +01:00
|
|
|
slotProps={{
|
|
|
|
|
input: {
|
|
|
|
|
inputProps: IMAGE_PROCESSING_CALL_TIMEOUT,
|
2026-01-10 19:45:02 +01:00
|
|
|
endAdornment: <InputAdornment position="end">{t`Second`}</InputAdornment>,
|
2025-12-23 02:31:25 +01:00
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
callTimeout: e.target.value
|
|
|
|
|
? d(Number(e.target.value)).seconds.toISOString()
|
|
|
|
|
: null,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextField
|
|
|
|
|
sx={{ width: IMAGE_PROCESSING_INPUT_WIDTH }}
|
2026-01-10 19:45:02 +01:00
|
|
|
label={t`Connect timeout`}
|
2025-12-23 02:31:25 +01:00
|
|
|
value={connectTimeout ? d(connectTimeout).seconds.inWholeSeconds : ''}
|
|
|
|
|
type="number"
|
|
|
|
|
error={!isConnectTimeoutValid}
|
2026-01-10 19:45:02 +01:00
|
|
|
helperText={!isConnectTimeoutValid ? t`Invalid input` : ''}
|
2025-12-23 02:31:25 +01:00
|
|
|
slotProps={{
|
|
|
|
|
input: {
|
|
|
|
|
inputProps: IMAGE_PROCESSING_CONNECT_TIMEOUT,
|
2026-01-10 19:45:02 +01:00
|
|
|
endAdornment: <InputAdornment position="end">{t`Second`}</InputAdornment>,
|
2025-12-23 02:31:25 +01:00
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
connectTimeout: e.target.value
|
|
|
|
|
? d(Number(e.target.value)).seconds.toISOString()
|
|
|
|
|
: null,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2025-12-28 02:07:52 +01:00
|
|
|
<Button
|
|
|
|
|
disabled={!isUrlTargetMode(target)}
|
|
|
|
|
onClick={() => setAreSearchParamsCollapsed(!areSearchParamsCollapsed)}
|
|
|
|
|
variant={areSearchParamsCollapsed ? 'outlined' : 'contained'}
|
|
|
|
|
sx={{ height: textFieldHeight }}
|
|
|
|
|
>
|
2026-01-10 19:45:02 +01:00
|
|
|
{t`Search parameters (${searchParams?.length ?? 0})`}
|
2025-12-28 02:07:52 +01:00
|
|
|
</Button>
|
2025-12-23 02:31:25 +01:00
|
|
|
<Button
|
|
|
|
|
onClick={() => setAreHeadersCollapsed(!areHeadersCollapsed)}
|
|
|
|
|
variant={areHeadersCollapsed ? 'outlined' : 'contained'}
|
|
|
|
|
sx={{ height: textFieldHeight }}
|
|
|
|
|
>
|
2026-01-10 19:45:02 +01:00
|
|
|
{t`Headers (${headers?.length ?? 0})`}
|
2025-12-23 02:31:25 +01:00
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
})()}
|
2025-12-22 21:33:23 +01:00
|
|
|
</Stack>
|
2026-01-10 19:45:02 +01:00
|
|
|
<CustomTooltip disabled={isDisabled} title={t`Delete`}>
|
2025-12-22 21:33:23 +01:00
|
|
|
<IconButton
|
|
|
|
|
disabled={isDisabled}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onChange(null);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<DeleteIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</CustomTooltip>
|
|
|
|
|
</Stack>
|
2025-12-28 02:07:52 +01:00
|
|
|
<KeyValueItems
|
2026-01-10 19:45:02 +01:00
|
|
|
title={t`Search parameters`}
|
2025-12-28 02:07:52 +01:00
|
|
|
open={isUrlMode && !areSearchParamsCollapsed}
|
|
|
|
|
items={searchParams}
|
|
|
|
|
onChange={(params) => {
|
|
|
|
|
const baseUrl = target?.split('?')[0] ?? '';
|
|
|
|
|
const updatedParams = Object.fromEntries(params?.map(({ name, value }) => [name, value]) ?? []);
|
|
|
|
|
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
target: UrlUtil.addParams(baseUrl, updatedParams),
|
|
|
|
|
searchParams: params,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<KeyValueItems
|
2026-01-10 19:45:02 +01:00
|
|
|
title={t`Headers`}
|
2025-12-28 02:07:52 +01:00
|
|
|
open={isUrlMode && !areHeadersCollapsed}
|
|
|
|
|
items={headers}
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange={(updatedHeaders) =>
|
|
|
|
|
onChange({
|
|
|
|
|
...conversion,
|
|
|
|
|
headers: updatedHeaders,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|