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 { useTranslation } from 'react-i18next';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Collapse from '@mui/material/Collapse';
|
|
|
|
|
import { Maybe } from '@/lib/graphql/generated/graphql.ts';
|
2025-12-28 02:07:52 +01:00
|
|
|
import { addStableIdToKeyValueItems, isDuplicateKeyValueItem } from '@/features/settings/ImageProcessing.utils.ts';
|
|
|
|
|
import { KeyValueItem } from '@/features/settings/components/images/KeyValueItem.tsx';
|
|
|
|
|
import { TSettingsDownloadConversionKeyValueItem } from '@/features/settings/Settings.types.ts';
|
2025-12-22 21:33:23 +01:00
|
|
|
|
2025-12-28 02:07:52 +01:00
|
|
|
export const KeyValueItems = ({
|
|
|
|
|
title,
|
2025-12-22 21:33:23 +01:00
|
|
|
open,
|
2025-12-28 02:07:52 +01:00
|
|
|
items,
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange,
|
|
|
|
|
}: {
|
2025-12-28 02:07:52 +01:00
|
|
|
title: string;
|
2025-12-22 21:33:23 +01:00
|
|
|
open: boolean;
|
2025-12-28 02:07:52 +01:00
|
|
|
items: Maybe<TSettingsDownloadConversionKeyValueItem[] | undefined>;
|
|
|
|
|
onChange: (items: Maybe<TSettingsDownloadConversionKeyValueItem[]>) => void;
|
2025-12-22 21:33:23 +01:00
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Collapse in={open}>
|
2025-12-22 22:04:05 +01:00
|
|
|
<Stack sx={{ justifyContent: 'start', gap: 2, pt: 2 }}>
|
2025-12-28 02:07:52 +01:00
|
|
|
<Typography>{title}</Typography>
|
|
|
|
|
{items?.map((header, index) => (
|
|
|
|
|
<KeyValueItem
|
2025-12-22 21:33:23 +01:00
|
|
|
key={header.id}
|
|
|
|
|
{...header}
|
2025-12-28 02:07:52 +01:00
|
|
|
isDuplicate={isDuplicateKeyValueItem(header.name, index, items)}
|
2025-12-22 21:33:23 +01:00
|
|
|
onChange={(updatedHeader) => {
|
|
|
|
|
const isDeletion = updatedHeader == null;
|
|
|
|
|
if (isDeletion) {
|
2025-12-28 02:07:52 +01:00
|
|
|
onChange(items?.toSpliced(index, 1));
|
2025-12-22 21:33:23 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 02:07:52 +01:00
|
|
|
onChange((items ?? []).toSpliced(index, 1, updatedHeader));
|
2025-12-22 21:33:23 +01:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
<Button
|
2025-12-28 02:07:52 +01:00
|
|
|
onClick={() =>
|
|
|
|
|
onChange([...(items ?? []), ...addStableIdToKeyValueItems([{ name: '', value: '' }])])
|
|
|
|
|
}
|
2025-12-22 21:33:23 +01:00
|
|
|
variant="contained"
|
|
|
|
|
sx={{ width: 'fit-content' }}
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.add')}
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Collapse>
|
|
|
|
|
);
|
|
|
|
|
};
|