Skip to content

Commit

Permalink
chat store title
Browse files Browse the repository at this point in the history
  • Loading branch information
heimoshuiyu committed Feb 6, 2025
1 parent 336fd5e commit eec2fb5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/components/ConversationTitle..tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { STORAGE_NAME } from "@/const";
import { AppChatStoreContext, AppContext } from "@/pages/App";
import { ChatStore } from "@/types/chatstore";
import { memo, useContext, useEffect, useMemo, useState } from "react";

const ConversationTitle = ({ chatStoreIndex }: { chatStoreIndex: number }) => {
const { db, selectedChatIndex } = useContext(AppContext);
const [title, setTitle] = useState("");

const getTitle = async () => {
const chatStore = (await (
await db
).get(STORAGE_NAME, chatStoreIndex)) as ChatStore;
if (chatStore.history.length === 0) {
setTitle(`${chatStoreIndex}`);
return;
}
const content = chatStore.history[0]?.content;
if (!content) {
setTitle(`${chatStoreIndex}`);
return;
}

if (typeof content === "string") {
console.log(content);
setTitle(content.substring(0, 39));
}
};

useEffect(() => {
try {
getTitle();
} catch (e) {
console.error(e);
}
}, []);

return <span>{title}</span>;
};

const CachedConversationTitle = memo(
({
chatStoreIndex,
selectedChatStoreIndex,
}: {
chatStoreIndex: number;
selectedChatStoreIndex: number;
}) => {
return <ConversationTitle chatStoreIndex={chatStoreIndex} />;
},
(prevProps, nextProps) => {
return nextProps.selectedChatStoreIndex === nextProps.chatStoreIndex;
}
);

export default CachedConversationTitle;
8 changes: 7 additions & 1 deletion src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import { ModeToggle } from "@/components/ModeToggle";
import Search from "@/components/Search";

import Navbar from "@/components/navbar";
import ConversationTitle from "@/components/ConversationTitle.";

export function App() {
// init selected index
Expand Down Expand Up @@ -356,7 +357,12 @@ export function App() {
asChild
isActive={i === selectedChatIndex}
>
<span>{i}</span>
<span>
<ConversationTitle
chatStoreIndex={i}
selectedChatStoreIndex={selectedChatIndex}
/>
</span>
</SidebarMenuButton>
</SidebarMenuItem>
);
Expand Down

0 comments on commit eec2fb5

Please sign in to comment.