Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: reversing chat items order and scrolling

Ilian Sapundshievea0e915c8dff
diff --git a/src/renderer/components/chat/ChatView.tsx b/src/renderer/components/chat/ChatView.tsx
index c6b99e768..64773073a 100644
--- a/src/renderer/components/chat/ChatView.tsx
+++ b/src/renderer/components/chat/ChatView.tsx
@@ -150,7 +150,10 @@ function ChatView() {
const handleCopy = useCallback(() => {
setAnchorEl(null);
if (milkdownDivRef.current) {
- const textToCopy = milkdownDivRef.current.innerText;
+ const milkdownContent =
+ // @ts-ignore
+ milkdownDivRef.current.querySelector('.ProseMirror').innerText;
+ const textToCopy = milkdownContent || milkdownDivRef.current.innerText;
navigator.clipboard
.writeText(textToCopy)
.then(() => {
@@ -165,8 +168,17 @@ function ChatView() {
const saveAsHtml = useCallback(() => {
setAnchorEl(null);
if (milkdownDivRef.current) {
- const html = milkdownDivRef.current.innerHTML;
- const blob = new Blob([html], { type: 'text/html' });
+ const milkdownContent =
+ milkdownDivRef.current.querySelector('.ProseMirror').innerHTML;
+ const html = milkdownContent || milkdownDivRef.current.innerHTML;
+ const blob = new Blob(
+ [
+ '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>',
+ html,
+ '</body></html>',
+ ],
+ { type: 'text/html' },
+ );
const dateTimeTag = formatDateTime4Tag(new Date(), true);
const filename = `tagspaces-chat [export ${dateTimeTag}].html`;
saveAsTextFile(blob, filename);
@@ -232,25 +244,31 @@ function ChatView() {
</Grid>
</Grid>
{/* Chat markdown editor */}
- <Grid sx={{ padding: 0, overflowY: 'auto', flexGrow: 1 }}>
- <div className="chatMD" ref={milkdownDivRef}>
- <style>
- {`
- .chatMD .milkdown .ProseMirror {
+ <Grid
+ id="chatMD"
+ sx={{ padding: 0, overflowY: 'auto', flexGrow: 1 }}
+ ref={milkdownDivRef}
+ >
+ <style>
+ {`
+ #chatMD .milkdown .ProseMirror {
padding: 10px;
}
- .chatMD .milkdown .ProseMirror a {
+ #chatMD .milkdown .ProseMirror a {
color: ${theme.palette.primary.main};
}
- .chatMD .milkdown .ProseMirror img {
+ #chatMD .milkdown .ProseMirror img {
max-width: 99%;
}
`}
- </style>
- <MilkdownProvider>
- <ChatMdEditor ref={editorRef} currentFolder={openedEntry.path} />
- </MilkdownProvider>
- </div>
+ </style>
+ <MilkdownProvider>
+ <ChatMdEditor
+ showCurrent={isLoading.current}
+ ref={editorRef}
+ currentFolder={openedEntry.path}
+ />
+ </MilkdownProvider>
</Grid>
{/* Images and chat input */}
<Grid container spacing={1} direction="column">
@@ -324,7 +342,7 @@ function ChatView() {
),
endAdornment: (
<InputAdornment position="end" sx={{ height: 32 }}>
- {isTyping && (
+ {isLoading.current && (
<CircularProgress size={24} color="inherit" />
)}
{isLoading.current && (
diff --git a/src/renderer/components/md/ChatMdEditor.tsx b/src/renderer/components/md/ChatMdEditor.tsx
index 41d0be1be..d0036540f 100644
--- a/src/renderer/components/md/ChatMdEditor.tsx
+++ b/src/renderer/components/md/ChatMdEditor.tsx
@@ -25,24 +25,26 @@ import { EditorStatus } from '@milkdown/core';
import { Crepe } from '@milkdown/crepe';
import { Milkdown, useEditor } from '@milkdown/react';
import { format } from 'date-fns';
-import React, { useRef } from 'react';
+import React, { useEffect, useRef } from 'react';
interface ChatMdEditorProps {
currentFolder?: string;
placeholder?: string;
+ showCurrent?: boolean;
}
const ChatMdEditor = React.forwardRef<CrepeRef, ChatMdEditorProps>(
(props, ref) => {
- const { currentFolder, placeholder } = props;
+ const { currentFolder, placeholder, showCurrent } = props;
const { openLink } = useOpenedEntryContext();
const { chatHistoryItems } = useChatContext();
const crepeInstanceRef = useRef<Crepe | null>(null);
// Memoize formatted chat content for performance
const formattedChatContent = React.useMemo(
- () => formatChatItems(chatHistoryItems),
- [chatHistoryItems],
+ () =>
+ formatChatItems(showCurrent ? [chatHistoryItems[0]] : chatHistoryItems),
+ [chatHistoryItems, showCurrent],
);
// Use Milkdown's useEditor, but only recreate the editor when currentFolder changes
@@ -71,71 +73,50 @@ const ChatMdEditor = React.forwardRef<CrepeRef, ChatMdEditorProps>(
crepe.editor.onStatusChange((status: EditorStatus) => {
if (status === EditorStatus.Created) {
crepeInstanceRef.current = crepe;
- // console.log('Scroll: ' + ref.scrollHeight);
- // crepeInstanceRef.current.scrollTo({
- // top: crepeInstanceRef.current.scrollHeight,
- // behavior: 'smooth',
- // });
- // const content = document.querySelector('.chatMD');
- // if (!content) return;
- // setTimeout(() => {
- // content.scrollTo({
- // top: content.scrollHeight,
- // behavior: 'smooth',
- // });
- // console.log('Scrolled to: ' + content.scrollHeight);
- // }, 100);
}
});
return crepe;
},
- [currentFolder, chatHistoryItems], // Only recreate editor when folder changes
+ [currentFolder, chatHistoryItems],
);
- // useEffect(() => {
- // return () => {
- // if (crepeInstanceRef.current) {
- // crepeInstanceRef.current.destroy();
- // }
- // };
- // }, []);
-
- // useEffect(() => {
- // const content = document.querySelector('.milkdown');
- // if (content) {
- // content.scrollTo({
- // top: content.scrollHeight,
- // behavior: 'smooth',
- // });
- // }
- // }, [formattedChatContent]);
-
useCrepeHandler(ref, () => crepeInstanceRef.current, get, loading);
+ // Scroll to bottom when markdown content changes
+ useEffect(() => {
+ const container = document.querySelector('#chatMD');
+ if (container) {
+ setTimeout(() => {
+ container.scrollTo({
+ top: container.scrollHeight,
+ behavior: 'auto', // 'smooth',
+ });
+ }, 500);
+ }
+ }, [formattedChatContent]);
+
function formatChatItems(chatItems: ChatItem[] = []): string {
if (!chatItems.length) return '';
- return (
- [...chatItems]
- // .reverse()
- .map((item) => {
- const dateStr = item.timestamp
- ? `**User on ${format(item.timestamp, 'yyyy-MM-dd HH:mm:ss')}**`
- : '**User**';
- const requestStr = item.request ?? '';
- const modelName = item.modelName ?? 'AI model';
- const responseStr = item.response
- ? `**${modelName}**:\\\n${item.response}`
- : '';
- const imagesStr = (item.imagePaths ?? [])
- .map(
- (img) =>
- `![chat image](${AppConfig.metaFolder}/${AppConfig.aiFolder}/${img})`,
- )
- .join('\n');
- return `${dateStr}: \\\n${requestStr}\n${imagesStr}\n${responseStr}\n***\n`;
- })
- .join('\n')
- );
+ return [...chatItems]
+ .reverse()
+ .map((item) => {
+ const dateStr = item.timestamp
+ ? `**You on ${format(item.timestamp, 'yyyy-MM-dd HH:mm:ss')}**`
+ : '**You**';
+ const requestStr = item.request ?? '';
+ const modelName = item.modelName ?? 'AI model';
+ const responseStr = item.response
+ ? `**AI (${modelName})**:\\\n${item.response}`
+ : '';
+ const imagesStr = (item.imagePaths ?? [])
+ .map(
+ (img) =>
+ `![chat image](${AppConfig.metaFolder}/${AppConfig.aiFolder}/${img})`,
+ )
+ .join('\n');
+ return `${dateStr}: \\\n${requestStr}\n${imagesStr}\n${responseStr}\n***\n`;
+ })
+ .join('\n');
}
return <Milkdown />;