From 5631354025448ff1002d91d4b6ea1cc2117d361c Mon Sep 17 00:00:00 2001 From: BetaHuhn Date: Wed, 30 Aug 2023 17:10:04 +0200 Subject: [PATCH] feat: directly edit chat name --- src/components/ChatHeader.tsx | 79 ++++++++++++++++++++++++++++ src/components/CreatePromptModal.tsx | 3 ++ src/components/EditChatModal.tsx | 3 ++ src/components/EditPromptModal.tsx | 3 ++ src/components/Layout.tsx | 28 +--------- 5 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 src/components/ChatHeader.tsx diff --git a/src/components/ChatHeader.tsx b/src/components/ChatHeader.tsx new file mode 100644 index 0000000..8dfd68d --- /dev/null +++ b/src/components/ChatHeader.tsx @@ -0,0 +1,79 @@ +import { ActionIcon, Box, Header, TextInput, Tooltip } from "@mantine/core"; +import { IconAdjustments } from "@tabler/icons-react"; +import { EditChatModal } from "./EditChatModal"; +import { Chat, detaDB } from "../db"; +import { useEffect, useRef, useState } from "react"; +import { useChat, useChats } from "../hooks/contexts"; +import { useDebouncedValue } from "@mantine/hooks"; +import { notifications } from "@mantine/notifications"; + +export function ChatHeader() { + const [title, setTitle] = useState('') + const [debounced] = useDebouncedValue(title, 800); + + const { chat, setChat } = useChat() + const { setChats } = useChats() + + useEffect(() => { + if (chat?.description) setTitle(chat.description) + }, [chat]) + + useEffect(() => { + const save = async () => { + if (!chat || !debounced || debounced === chat.description) return + await detaDB.chats.update({ description: title }, chat!.key) + setChat(current => ({ ...(current as Chat), description: title })) + setChats(current => (current || []).map(item => { + if (item.key === chat.key) { + return { ...item, description: title }; + } + + return item; + })) + + notifications.show({ + title: "Saved", + message: "Chat name updated.", + }); + } + + save() + }, [debounced]) + + const ref = useRef(null); + + useEffect(() => { + if (title && ref.current) { + ref.current.size = Math.max(title.length - 1, 8) + } + }, [title]) + + return ( +
+ +
+ + setTitle(event.currentTarget.value)} + styles={{ input: { fontSize: '1rem', textAlign: 'center' } }} + autoComplete="off" + data-lpignore="true" + data-form-type="other" + /> + + + + + + + + +
+
+ ) +} \ No newline at end of file diff --git a/src/components/CreatePromptModal.tsx b/src/components/CreatePromptModal.tsx index b512219..3a70f1d 100644 --- a/src/components/CreatePromptModal.tsx +++ b/src/components/CreatePromptModal.tsx @@ -117,6 +117,9 @@ export function CreatePromptModal({ content, title: titleProp, open: openProp }: onChange={(event) => setTitle(event.currentTarget.value)} formNoValidate data-autofocus + autoComplete="off" + data-lpignore="true" + data-form-type="other" /> setValue(event.currentTarget.value)} formNoValidate data-autofocus + autoComplete="off" + data-lpignore="true" + data-form-type="other" />