diff --git a/src/components/Common/ShareBtn.tsx b/src/components/Common/ShareBtn.tsx index ffce24c..e1146a0 100644 --- a/src/components/Common/ShareBtn.tsx +++ b/src/components/Common/ShareBtn.tsx @@ -7,12 +7,13 @@ import React from "react" import { useMutation } from "@tanstack/react-query" import { getPageShareUrl } from "~/services/ollama" import { cleanUrl } from "~/libs/clean-url" -import { getUserId, saveWebshare } from "@/db" +import { getTitleById, getUserId, saveWebshare } from "@/db" import { useTranslation } from "react-i18next" import fetcher from "@/libs/fetcher" type Props = { messages: Message[] + historyId: string } const reformatMessages = (messages: Message[], username: string) => { @@ -76,7 +77,7 @@ export const PlaygroundMessage = ( ) } -export const ShareBtn: React.FC = ({ messages }) => { +export const ShareBtn: React.FC = ({ messages, historyId }) => { const { t } = useTranslation("common") const [open, setOpen] = useState(false) const [form] = Form.useForm() @@ -84,11 +85,13 @@ export const ShareBtn: React.FC = ({ messages }) => { React.useEffect(() => { if (messages.length > 0) { - form.setFieldsValue({ - title: messages[0].message + getTitleById(historyId).then((title) => { + form.setFieldsValue({ + title + }) }) } - }, [messages]) + }, [messages, historyId]) const onSubmit = async (values: { title: string; name: string }) => { const owner_id = await getUserId() diff --git a/src/components/Layouts/Header.tsx b/src/components/Layouts/Header.tsx index 65fab8e..af39f1c 100644 --- a/src/components/Layouts/Header.tsx +++ b/src/components/Layouts/Header.tsx @@ -46,6 +46,7 @@ export const Header: React.FC = ({ setSelectedSystemPrompt, messages, streaming, + historyId } = useMessageOption() const { data: models, @@ -205,7 +206,9 @@ export const Header: React.FC = ({ {pathname === "/" && messages.length > 0 && !streaming && - shareModeEnabled && } + shareModeEnabled && } { + const chatHistories = await this.getChatHistories() + const chatHistory = chatHistories.find((history) => history.id === id) + return chatHistory?.title || "" + } + async addChatHistory(history: HistoryInfo) { const chatHistories = await this.getChatHistories() const newChatHistories = [history, ...chatHistories] @@ -482,4 +488,11 @@ export const getRecentChatFromCopilot = async () => { const messages = await db.getChatHistory(history.id) return { history, messages } +} + + +export const getTitleById = async (id: string) => { + const db = new PageAssitDatabase() + const title = await db.getChatHistoryTitleById(id) + return title } \ No newline at end of file