clean up proposal logic

This commit is contained in:
Will Chen
2025-04-18 13:23:26 -07:00
parent 639b3a320c
commit a4702f90b0
7 changed files with 74 additions and 61 deletions

View File

@@ -8,35 +8,39 @@ export function useProposal(chatId?: number | undefined) {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const fetchProposal = useCallback(async () => {
if (chatId === undefined) {
setProposalResult(null);
setIsLoading(false);
setError(null);
return;
}
setIsLoading(true);
setError(null);
setProposalResult(null); // Reset on new fetch
try {
// Type assertion might be needed depending on how IpcClient is typed
const result = (await IpcClient.getInstance().getProposal(
chatId
)) as ProposalResult | null;
if (result) {
setProposalResult(result);
} else {
setProposalResult(null); // Explicitly set to null if IPC returns null
const fetchProposal = useCallback(
async (overrideChatId?: number) => {
chatId = overrideChatId ?? chatId;
if (chatId === undefined) {
setProposalResult(null);
setIsLoading(false);
setError(null);
return;
}
} catch (err: any) {
console.error("Error fetching proposal:", err);
setError(err.message || "Failed to fetch proposal");
setProposalResult(null); // Clear proposal data on error
} finally {
setIsLoading(false);
}
}, [chatId]); // Depend on chatId
setIsLoading(true);
setError(null);
setProposalResult(null); // Reset on new fetch
try {
// Type assertion might be needed depending on how IpcClient is typed
const result = (await IpcClient.getInstance().getProposal(
chatId
)) as ProposalResult | null;
if (result) {
setProposalResult(result);
} else {
setProposalResult(null); // Explicitly set to null if IPC returns null
}
} catch (err: any) {
console.error("Error fetching proposal:", err);
setError(err.message || "Failed to fetch proposal");
setProposalResult(null); // Clear proposal data on error
} finally {
setIsLoading(false);
}
},
[chatId]
); // Depend on chatId
useEffect(() => {
fetchProposal();

View File

@@ -22,7 +22,9 @@ export function getRandomString() {
return Math.random().toString(36).substring(2, 15);
}
export function useStreamChat() {
export function useStreamChat({
hasChatId = true,
}: { hasChatId?: boolean } = {}) {
const [messages, setMessages] = useAtom(chatMessagesAtom);
const [isStreaming, setIsStreaming] = useAtom(isStreamingAtom);
const [error, setError] = useAtom(chatErrorAtom);
@@ -32,8 +34,14 @@ export function useStreamChat() {
const { refreshApp } = useLoadApp(selectedAppId);
const setStreamCount = useSetAtom(chatStreamCountAtom);
const { refreshVersions } = useLoadVersions(selectedAppId);
const { id: chatId } = useSearch({ from: "/chat" });
const { refreshProposal } = useProposal(chatId);
let chatId: number | undefined;
if (hasChatId) {
const { id } = useSearch({ from: "/chat" });
chatId = id;
}
let { refreshProposal } = hasChatId ? useProposal(chatId) : useProposal();
const streamMessage = useCallback(
async ({
prompt,
@@ -93,7 +101,7 @@ export function useStreamChat() {
if (response.updatedFiles) {
setIsPreviewOpen(true);
}
refreshProposal();
refreshProposal(chatId);
// Keep the same as below
setIsStreaming(false);