forked from prehistoric-systems/chatpad
91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import {
|
|
ActionIcon,
|
|
Button,
|
|
Modal,
|
|
Stack,
|
|
Textarea,
|
|
TextInput,
|
|
Tooltip,
|
|
} from "@mantine/core";
|
|
import { useDisclosure } from "@mantine/hooks";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { IconPencil } from "@tabler/icons-react";
|
|
import { useEffect, useState } from "react";
|
|
import { db, Prompt } from "../db";
|
|
|
|
export function EditPromptModal({ prompt }: { prompt: Prompt }) {
|
|
const [opened, { open, close }] = useDisclosure(false);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
const [value, setValue] = useState("");
|
|
const [title, setTitle] = useState("");
|
|
useEffect(() => {
|
|
setValue(prompt?.content ?? "");
|
|
setTitle(prompt?.title ?? "");
|
|
}, [prompt]);
|
|
|
|
return (
|
|
<>
|
|
<Modal opened={opened} onClose={close} title="Edit Prompt" size="lg">
|
|
<form
|
|
onSubmit={async (event) => {
|
|
try {
|
|
setSubmitting(true);
|
|
event.preventDefault();
|
|
await db.prompts.where({ id: prompt.id }).modify((chat) => {
|
|
chat.title = title;
|
|
chat.content = value;
|
|
});
|
|
notifications.show({
|
|
title: "Saved",
|
|
message: "Prompt updated",
|
|
});
|
|
} catch (error: any) {
|
|
if (error.toJSON().message === "Network Error") {
|
|
notifications.show({
|
|
title: "Error",
|
|
color: "red",
|
|
message: "No internet connection.",
|
|
});
|
|
} else {
|
|
notifications.show({
|
|
title: "Error",
|
|
color: "red",
|
|
message: "Your OpenAI Key is invalid.",
|
|
});
|
|
}
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
}}
|
|
>
|
|
<Stack>
|
|
<TextInput
|
|
label="Title"
|
|
value={title}
|
|
onChange={(event) => setTitle(event.currentTarget.value)}
|
|
formNoValidate
|
|
data-autofocus
|
|
/>
|
|
<Textarea
|
|
label="Content"
|
|
autosize
|
|
minRows={5}
|
|
maxRows={10}
|
|
value={value}
|
|
onChange={(event) => setValue(event.currentTarget.value)}
|
|
/>
|
|
<Button type="submit" loading={submitting}>
|
|
Save
|
|
</Button>
|
|
</Stack>
|
|
</form>
|
|
</Modal>
|
|
<Tooltip label="Edit Prompt">
|
|
<ActionIcon size="lg" onClick={open}>
|
|
<IconPencil size={20} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
</>
|
|
);
|
|
}
|