2023-04-05 08:44:00 -07:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-04-02 04:15:07 +08:00
|
|
|
import DialogTemplate from '../ui/DialogTemplate';
|
|
|
|
|
import { Dialog } from '../ui/Dialog.tsx';
|
|
|
|
|
import { Input } from '../ui/Input.tsx';
|
|
|
|
|
import { Label } from '../ui/Label.tsx';
|
|
|
|
|
import { cn } from '~/utils/';
|
2023-04-04 01:12:14 +08:00
|
|
|
import cleanupPreset from '~/utils/cleanupPreset';
|
2023-04-05 08:44:00 -07:00
|
|
|
import { useCreatePresetMutation } from '~/data-provider';
|
2023-04-02 04:15:07 +08:00
|
|
|
import store from '~/store';
|
|
|
|
|
|
2023-04-04 01:12:14 +08:00
|
|
|
const SaveAsPresetDialog = ({ open, onOpenChange, preset }) => {
|
2023-04-04 01:39:40 +08:00
|
|
|
const [title, setTitle] = useState(preset?.title || 'My Preset');
|
2023-04-05 21:21:02 +08:00
|
|
|
const endpointsFilter = useRecoilValue(store.endpointsFilter);
|
2023-04-05 08:44:00 -07:00
|
|
|
const createPresetMutation = useCreatePresetMutation();
|
2023-04-02 04:15:07 +08:00
|
|
|
|
|
|
|
|
const defaultTextProps =
|
|
|
|
|
'rounded-md border border-gray-300 bg-transparent text-sm shadow-[0_0_10px_rgba(0,0,0,0.10)] outline-none placeholder:text-gray-400 focus:outline-none focus:ring-gray-400 focus:ring-opacity-20 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-400 dark:bg-gray-700 dark:text-gray-50 dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] dark:focus:border-gray-400 dark:focus:outline-none dark:focus:ring-0 dark:focus:ring-gray-400 dark:focus:ring-offset-0';
|
|
|
|
|
|
|
|
|
|
const submitPreset = () => {
|
2023-04-04 01:12:14 +08:00
|
|
|
const _preset = cleanupPreset({
|
2023-04-05 21:21:02 +08:00
|
|
|
preset: {
|
|
|
|
|
...preset,
|
|
|
|
|
title
|
|
|
|
|
},
|
|
|
|
|
endpointsFilter
|
2023-04-02 04:15:07 +08:00
|
|
|
});
|
2023-04-05 08:44:00 -07:00
|
|
|
createPresetMutation.mutate(_preset);
|
2023-04-02 04:15:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-04-04 01:39:40 +08:00
|
|
|
setTitle(preset?.title || 'My Preset');
|
2023-04-02 04:15:07 +08:00
|
|
|
}, [open]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
|
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
|
>
|
|
|
|
|
<DialogTemplate
|
|
|
|
|
title="Save As Preset"
|
2023-04-01 19:42:09 -04:00
|
|
|
main={
|
|
|
|
|
<div className="grid w-full items-center gap-2">
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor="chatGptLabel"
|
|
|
|
|
className="text-left text-sm font-medium"
|
|
|
|
|
>
|
|
|
|
|
Preset Name
|
|
|
|
|
</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="chatGptLabel"
|
|
|
|
|
value={title || ''}
|
|
|
|
|
onChange={e => setTitle(e.target.value || '')}
|
|
|
|
|
placeholder="Set a custom name, in case you can find this preset"
|
|
|
|
|
className={cn(
|
|
|
|
|
defaultTextProps,
|
|
|
|
|
'flex h-10 max-h-10 w-full resize-none px-3 py-2 focus:outline-none focus:ring-0 focus:ring-opacity-0 focus:ring-offset-0'
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2023-04-02 04:15:07 +08:00
|
|
|
selection={{
|
|
|
|
|
selectHandler: submitPreset,
|
|
|
|
|
selectClasses: 'bg-green-600 hover:bg-green-700 dark:hover:bg-green-800 text-white',
|
|
|
|
|
selectText: 'Save'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SaveAsPresetDialog;
|