Skip to content

Commit d2226f1

Browse files
authored
fix: ghost temp conversation #805 (#807)
1 parent 098c313 commit d2226f1

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

webapp/components/views/Threads/ConversationContext.tsx renamed to webapp/components/views/Threads/Conversation/ConversationContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { preProcessingCommands } from '@/utils/commands';
4040
import useShortcuts, { ShortcutIds } from '@/hooks/useShortcuts';
4141
import { CommandManager } from '@/utils/commands/types';
4242
import { sendMessage, updateMessageContent } from '@/utils/messages';
43-
import { PromptContext } from './Prompt/PromptContext';
43+
import { PromptContext } from '../Prompt/PromptContext';
4444

4545
type Context = {
4646
selectedMessageId: string | undefined;
@@ -226,7 +226,7 @@ function ConversationProvider({
226226
}
227227

228228
if (clearPrompt) {
229-
updatedConversations = clearPrompt(updatedConversation, updatedConversations);
229+
updatedConversations = await clearPrompt(updatedConversation, updatedConversations);
230230
}
231231

232232
logger.info(

webapp/components/views/Threads/Conversation/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { MenuAction, Page, ViewName } from '@/types/ui';
2424
import logger from '@/utils/logger';
2525
import ConversationList from './ConversationList';
2626
import PromptsGrid from './PromptsGrid';
27-
import { useConversationContext } from '../ConversationContext';
27+
import { useConversationContext } from './ConversationContext';
2828
import { usePromptContext } from '../Prompt/PromptContext';
2929

3030
export type ConversationPanelProps = {

webapp/components/views/Threads/Prompt/PromptContext.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ type Context = {
5959
clearPrompt: (
6060
conversation: Conversation | undefined,
6161
newConversations: Conversation[],
62-
) => Conversation[];
63-
handleChangePrompt: (prompt: ParsedPrompt) => void;
64-
selectTemplate: (template: PromptTemplate) => void;
62+
) => Promise<Conversation[]>;
63+
// handleChangePrompt: (prompt: ParsedPrompt) => void;
64+
selectTemplate: (template: PromptTemplate) => Promise<void>;
6565
tokenValidator: TokenValidator;
6666
usage: Usage | undefined;
6767
};
@@ -162,7 +162,7 @@ function PromptProvider({
162162
]);
163163

164164
const clearPrompt = useCallback(
165-
(conversation: Conversation | undefined, newConversations: Conversation[]) => {
165+
async (conversation: Conversation | undefined, newConversations: Conversation[]) => {
166166
setChangedPrompt(undefined);
167167

168168
let updatedConversations = newConversations;
@@ -171,18 +171,18 @@ function PromptProvider({
171171
{ ...conversation, currentPrompt: undefined, temp: false },
172172
newConversations,
173173
);
174-
updateConversations(updatedConversations);
174+
await updateConversations(updatedConversations);
175175
}
176176
return updatedConversations;
177177
},
178178
[updateConversations],
179179
);
180180

181181
const handleUpdatePrompt = useCallback(
182-
(prompt: ParsedPrompt | undefined, conversationName = getDefaultConversationName(t)) => {
182+
async (prompt: ParsedPrompt | undefined, conversationName = getDefaultConversationName(t)) => {
183183
if (prompt?.raw === '' && tempConversationId) {
184184
setChangedPrompt(undefined);
185-
updateConversations(conversations.filter((c) => !c.temp));
185+
await updateConversations(conversations.filter((c) => !c.temp));
186186
onUpdateTempConversation(undefined);
187187
return;
188188
}
@@ -191,12 +191,12 @@ function PromptProvider({
191191
setChangedPrompt(undefined);
192192
return;
193193
}
194-
let updatedConversations: Conversation[];
194+
let updatedConversations: Conversation[] | undefined;
195195
if (conversation) {
196196
conversation.currentPrompt = prompt;
197197
updatedConversations = conversations.filter((c) => !(c.temp && c.id !== conversationId));
198198
updatedConversations = updateConversation(conversation, updatedConversations, true);
199-
} else {
199+
} else if (prompt?.raw !== '') {
200200
updatedConversations = conversations.filter((c) => !c.temp);
201201
let newConversation = createConversation(conversationName);
202202
newConversation.temp = true;
@@ -211,8 +211,12 @@ function PromptProvider({
211211
}
212212
updatedConversations.push(newConversation);
213213
onUpdateTempConversation(newConversation.id);
214+
} else {
215+
updatedConversations = conversations.filter((c) => !c.temp);
216+
}
217+
if (updatedConversations) {
218+
await updateConversations(updatedConversations);
214219
}
215-
updateConversations(updatedConversations);
216220
setChangedPrompt(undefined);
217221
},
218222
[
@@ -230,17 +234,17 @@ function PromptProvider({
230234

231235
useDebounceFunc<ParsedPrompt | undefined>(handleUpdatePrompt, changedPrompt, 500);
232236

233-
const handleChangePrompt = useCallback(
237+
/* const handleChangePrompt = useCallback(
234238
(prompt: ParsedPrompt) => {
235239
if (prompt.raw !== conversationPrompt?.raw) {
236240
setChangedPrompt(prompt);
237241
}
238242
},
239243
[conversationPrompt],
240-
);
244+
); */
241245

242246
const selectTemplate = useCallback(
243-
(template: PromptTemplate) => {
247+
async (template: PromptTemplate) => {
244248
handleUpdatePrompt(parseAndValidatePrompt(template.value), template.name);
245249
},
246250
[handleUpdatePrompt, parseAndValidatePrompt],
@@ -253,7 +257,7 @@ function PromptProvider({
253257
parseAndValidatePrompt,
254258
changedPrompt,
255259
setChangedPrompt,
256-
handleChangePrompt,
260+
// handleChangePrompt,
257261
tokenValidator,
258262
selectTemplate,
259263
usage,
@@ -263,7 +267,7 @@ function PromptProvider({
263267
clearPrompt,
264268
parseAndValidatePrompt,
265269
changedPrompt,
266-
handleChangePrompt,
270+
// handleChangePrompt,
267271
tokenValidator,
268272
selectTemplate,
269273
usage,
@@ -278,12 +282,12 @@ const usePromptContext = (): Context => {
278282
if (!context) {
279283
return {
280284
conversationPrompt: EmptyParsedPrompt,
281-
clearPrompt: () => [],
285+
clearPrompt: async () => [],
282286
parseAndValidatePrompt: () => EmptyParsedPrompt,
283287
changedPrompt: undefined,
284-
handleChangePrompt: () => {},
288+
// handleChangePrompt: () => {},
285289
tokenValidator: () => [EmptyPromptToken, undefined],
286-
selectTemplate: () => {},
290+
selectTemplate: async () => {},
287291
setChangedPrompt: () => {},
288292
usage: undefined,
289293
};

webapp/components/views/Threads/Prompt/index.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { ShortcutBadge } from '../../../common/ShortCut';
4646
import PromptInput from './PromptInput';
4747
import PromptCommands from './PromptCommands';
4848
import { usePromptContext } from './PromptContext';
49-
import { useConversationContext } from '../ConversationContext';
49+
import { useConversationContext } from '../Conversation/ConversationContext';
5050

5151
export type PromptProps = {
5252
conversationId: string;
@@ -149,7 +149,7 @@ export default function Prompt({
149149
const newPrompt = parsePrompt({ text: value, caretStartIndex }, tokenValidator);
150150
if (e.key === 'Enter' && e.shiftKey) {
151151
e.preventDefault();
152-
setChangedPrompt?.(newPrompt);
152+
setChangedPrompt(newPrompt);
153153
} else if (e.key === 'Enter' && !e.shiftKey) {
154154
e.preventDefault();
155155
sendMessage(newPrompt);
@@ -158,23 +158,19 @@ export default function Prompt({
158158

159159
const handleValueChange = useCallback(
160160
(text: string, caretStartIndex: number) => {
161-
if (!tokenValidator) return;
162161
const parsedPrompt = parsePrompt({ text, caretStartIndex }, tokenValidator);
163-
if (prompt?.raw !== text && setChangedPrompt) {
164-
// onUpdatePrompt(parsedPrompt);
162+
if (prompt?.raw !== text) {
165163
setChangedPrompt(parsedPrompt);
166164
}
167165
},
168-
[tokenValidator, prompt?.raw, setChangedPrompt],
166+
[tokenValidator, prompt, setChangedPrompt],
169167
);
170168

171169
const handleFocus = (event: ChangeEvent<HTMLTextAreaElement>) => {
172170
const lengthOfInput = event.target.value.length;
173171
event.currentTarget.setSelectionRange(lengthOfInput, lengthOfInput);
174-
if (!tokenValidator) return;
175172
const newPrompt = parsePrompt({ textarea: event.target }, tokenValidator);
176-
// onUpdatePrompt(newPrompt);
177-
setChangedPrompt?.(newPrompt);
173+
setChangedPrompt(newPrompt);
178174
};
179175

180176
const shortcutSend: KeyBinding = defaultShortcuts.find(

webapp/components/views/Threads/Thread.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { getLocalProvider } from '@/utils/data/providers';
5353
import useShortcuts, { ShortcutIds } from '@/hooks/useShortcuts';
5454
import ThreadHeader from './Header';
5555
import { PromptProvider } from './Prompt/PromptContext';
56-
import { ConversationProvider } from './ConversationContext';
56+
import { ConversationProvider } from './Conversation/ConversationContext';
5757
import { ConversationPanel } from './Conversation';
5858
import Prompt from './Prompt';
5959

@@ -119,11 +119,6 @@ function Thread({
119119
if (isMessageUpdating) {
120120
return;
121121
}
122-
/* let notfocused = false;
123-
if (!document.activeElement || document.activeElement.tagName === 'BODY') {
124-
notfocused = true;
125-
}
126-
setNotFocused(notfocused); */
127122
logger.info('getNewMessages', conversationId, isMessageUpdating);
128123

129124
setIsMessageUpdating(true);
@@ -243,7 +238,7 @@ function Thread({
243238
setTempConversationId(undefined);
244239
}
245240
if (!tempConversationId && !_conversationId) {
246-
const temp = conversations.find((c) => c.temp);
241+
const temp = conversations.find((c) => c.temp && c.currentPrompt);
247242
if (temp) {
248243
setTempConversationId(temp.id);
249244
}

0 commit comments

Comments
 (0)