2023-11-12 01:41:10 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-14 15:50:12 +02:00
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import ListItemText from '@mui/material/ListItemText';
|
2023-11-12 01:41:10 +01:00
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import ListItemButton from '@mui/material/ListItemButton';
|
|
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
|
|
|
|
|
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
|
|
|
|
|
import { TimePicker } from '@mui/x-date-pickers/TimePicker';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
|
|
|
|
|
export const TimeSetting = ({
|
|
|
|
|
settingName,
|
|
|
|
|
value,
|
|
|
|
|
defaultValue,
|
|
|
|
|
handleChange,
|
|
|
|
|
}: {
|
|
|
|
|
settingName: string;
|
2024-04-28 01:01:36 +02:00
|
|
|
value: string;
|
2023-11-12 01:41:10 +01:00
|
|
|
defaultValue: string;
|
|
|
|
|
handleChange: (path: string) => void;
|
|
|
|
|
}) => {
|
2024-05-02 22:14:34 +02:00
|
|
|
const { t } = useTranslation();
|
2023-11-12 01:41:10 +01:00
|
|
|
|
|
|
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
2024-04-28 01:01:36 +02:00
|
|
|
const [dialogValue, setDialogValue] = useState(value);
|
2023-11-12 01:41:10 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDialogValue(value);
|
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
|
|
const closeDialog = useCallback(
|
|
|
|
|
(resetValue: boolean) => {
|
|
|
|
|
setIsDialogOpen(false);
|
|
|
|
|
|
|
|
|
|
if (resetValue) {
|
2024-04-28 01:01:36 +02:00
|
|
|
setDialogValue(value);
|
2023-11-12 01:41:10 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[value],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const closeDialogWithReset = useCallback(() => closeDialog(true), [closeDialog]);
|
|
|
|
|
|
|
|
|
|
const updateSetting = useCallback(
|
|
|
|
|
(newValue: string, shouldCloseDialog: boolean = true) => {
|
|
|
|
|
if (shouldCloseDialog) {
|
|
|
|
|
closeDialog(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const didValueChange = value !== newValue;
|
|
|
|
|
if (!didValueChange) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleChange(newValue);
|
|
|
|
|
},
|
|
|
|
|
[value, handleChange, closeDialog],
|
|
|
|
|
);
|
2024-05-05 02:32:24 +02:00
|
|
|
|
2023-11-12 01:41:10 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ListItemButton onClick={() => setIsDialogOpen(true)}>
|
|
|
|
|
<ListItemText
|
|
|
|
|
primary={settingName}
|
2024-05-02 22:14:34 +02:00
|
|
|
secondary={dayjs(value, 'HH:mm').format('LT')}
|
2023-11-12 01:41:10 +01:00
|
|
|
secondaryTypographyProps={{ style: { display: 'flex', flexDirection: 'column' } }}
|
|
|
|
|
/>
|
|
|
|
|
</ListItemButton>
|
|
|
|
|
|
|
|
|
|
<Dialog open={isDialogOpen} onClose={closeDialog}>
|
2025-07-20 14:02:16 +02:00
|
|
|
<DialogTitle>{settingName}</DialogTitle>
|
2023-11-12 01:41:10 +01:00
|
|
|
<DialogContent>
|
2024-06-25 17:11:33 +02:00
|
|
|
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale={dayjs.locale()}>
|
2023-11-12 01:41:10 +01:00
|
|
|
<TimePicker
|
2025-07-20 22:49:07 +02:00
|
|
|
autoFocus
|
2023-11-12 01:41:10 +01:00
|
|
|
value={dayjs(dialogValue, 'HH:mm')}
|
|
|
|
|
defaultValue={dayjs(defaultValue, 'HH:mm')}
|
|
|
|
|
format="LT"
|
|
|
|
|
onChange={(time) => setDialogValue(time?.format('HH:mm') ?? '00:00')}
|
|
|
|
|
/>
|
|
|
|
|
</LocalizationProvider>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
{defaultValue !== undefined ? (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setDialogValue(defaultValue);
|
|
|
|
|
updateSetting(defaultValue, false);
|
|
|
|
|
}}
|
|
|
|
|
color="primary"
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.reset_to_default')}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
<Button onClick={closeDialogWithReset} color="primary">
|
|
|
|
|
{t('global.button.cancel')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
updateSetting(dialogValue);
|
|
|
|
|
}}
|
|
|
|
|
color="primary"
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.ok')}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|