minor changes

This commit is contained in:
Andrew Pareles 2025-03-01 18:19:33 -08:00
parent 334401eb50
commit 1ef22641b7
3 changed files with 36 additions and 38 deletions

View file

@ -169,6 +169,7 @@ export interface IChatThreadService {
setCurrentMessageState: (messageIdx: number, newState: Partial<UserMessageState>) => void
getCurrentThreadStagingSelections: () => StagingSelectionItem[]
setCurrentThreadStagingSelections: (stagingSelections: StagingSelectionItem[]) => void
getSortedThreadIdsByTime: () => string[]
// call to edit a message
@ -284,36 +285,30 @@ class ChatThreadService extends Disposable implements IChatThreadService {
public deleteThreadById(threadId: string): void {
const { allThreads, currentThreadId } = this.state
const newThreads = { ...allThreads }
if (threadId in newThreads) {
delete newThreads[threadId]
if (!(threadId in allThreads)) {
console.error('Void: Tried deleting thread with id that does not exist.')
return
}
// If we're deleting the current thread, switch to another thread first
const needsThreadSwitch = threadId === currentThreadId
let newCurrentThreadId = currentThreadId
if (needsThreadSwitch) {
// Find another thread to switch to
const remainingThreadIds = Object.keys(newThreads)
if (remainingThreadIds.length > 0) {
// Switch to the most recently modified thread
newCurrentThreadId = remainingThreadIds.sort((threadId1, threadId2) =>
newThreads[threadId2].lastModified > newThreads[threadId1].lastModified ? 1 : -1
)[0]
} else {
// If no threads left, create a new one
const newThread = newThreadObject()
newThreads[newThread.id] = newThread
newCurrentThreadId = newThread.id
// If we're on the thread we're about to delete, switch away from it
if (threadId === currentThreadId) {
const switchToThreadId = this.getSortedThreadIdsByTime().find(id => id !== threadId)
if (switchToThreadId !== undefined) {
this.switchToThread(switchToThreadId)
}
else {
this.openNewThread()
}
}
this._storeAllThreads(newThreads)
this._setState({
allThreads: newThreads,
currentThreadId: newCurrentThreadId
}, true)
// Delete the thread ID
const newAllThreads = { ...allThreads }
delete newAllThreads[threadId]
this._storeAllThreads(newAllThreads)
this._setState({ allThreads: newAllThreads, }, false)
}
// this should be the only place this.state = ... appears besides constructor
@ -572,6 +567,11 @@ class ChatThreadService extends Disposable implements IChatThreadService {
this._setState({ allThreads: newThreads, currentThreadId: newThread.id }, true)
}
getSortedThreadIdsByTime() {
const allThreads = this.state.allThreads
return Object.keys(allThreads ?? {}).sort((threadId1, threadId2) => allThreads[threadId1].lastModified > allThreads[threadId2].lastModified ? -1 : 1)
}
_addMessageToThread(threadId: string, message: ChatMessage) {
const { allThreads } = this.state

View file

@ -278,7 +278,7 @@ export const VoidChatArea: React.FC<VoidChatAreaProps> = ({
};
const useResizeObserver = () => {
const ref = useRef(null);
const ref = useRef<any>(null);
const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
useEffect(() => {

View file

@ -30,9 +30,7 @@ export const SidebarThreadSelector = () => {
const { allThreads } = threadsState
// sorted by most recent to least recent
const sortedThreadIds = Object.keys(allThreads ?? {})
.sort((threadId1, threadId2) => allThreads![threadId1].lastModified > allThreads![threadId2].lastModified ? -1 : 1)
.filter(threadId => allThreads![threadId].messages.length !== 0)
const sortedThreadIds = chatThreadsService.getSortedThreadIdsByTime().filter(threadId => allThreads![threadId].messages.length !== 0)
return (
<div className="flex p-2 flex-col gap-y-1 max-h-[400px] overflow-y-auto">
@ -113,15 +111,15 @@ export const SidebarThreadSelector = () => {
<div className='truncate'>{`${firstMsg}`}</div>
<div>{`\u00A0(${numMessages})`}</div>
<IconTrash
size={TRASH_ICON_SIZE}
className='ml-auto'
onClick={(e) => {
// Prevent the click event from bubbling up to the parent button
// to prevent the deleted thread from being switched to.
e.stopPropagation();
chatThreadsService.deleteThreadById(pastThread.id);
}}
/>
size={TRASH_ICON_SIZE}
className='ml-auto'
onClick={(e) => {
// Prevent the click event from bubbling up to the parent button
// to prevent the deleted thread from being switched to.
e.stopPropagation();
chatThreadsService.deleteThreadById(pastThread.id);
}}
/>
</button>
</li>
);