Refactor chat input & properly update state for proposal

This commit is contained in:
Will Chen
2025-04-18 12:32:57 -07:00
parent db7ac39c97
commit b2e3631a29
10 changed files with 140 additions and 48 deletions

View File

@@ -1,30 +1,26 @@
import { useState, useEffect, useCallback } from "react";
import { IpcClient } from "@/ipc/ipc_client";
import type { Proposal } from "@/lib/schemas"; // Import Proposal type
// Define the structure returned by the IPC call
interface ProposalResult {
proposal: Proposal;
messageId: number;
}
import type { Proposal, ProposalResult } from "@/lib/schemas"; // Import Proposal type
import { proposalResultAtom } from "@/atoms/proposalAtoms";
import { useAtom } from "jotai";
export function useProposal(chatId?: number | undefined) {
const [proposalData, setProposalData] = useState<ProposalResult | null>(null);
const [proposalResult, setProposalResult] = useAtom(proposalResultAtom);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const fetchProposal = useCallback(
async (innerChatId?: number) => {
chatId = chatId ?? innerChatId;
console.log("fetching proposal for chatId", chatId);
if (chatId === undefined) {
setProposalData(null);
setProposalResult(null);
setIsLoading(false);
setError(null);
return;
}
setIsLoading(true);
setError(null);
setProposalData(null); // Reset on new fetch
setProposalResult(null); // Reset on new fetch
try {
// Type assertion might be needed depending on how IpcClient is typed
const result = (await IpcClient.getInstance().getProposal(
@@ -32,14 +28,14 @@ export function useProposal(chatId?: number | undefined) {
)) as ProposalResult | null;
if (result) {
setProposalData(result);
setProposalResult(result);
} else {
setProposalData(null); // Explicitly set to null if IPC returns null
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");
setProposalData(null); // Clear proposal data on error
setProposalResult(null); // Clear proposal data on error
} finally {
setIsLoading(false);
}
@@ -64,8 +60,8 @@ export function useProposal(chatId?: number | undefined) {
);
return {
proposal: proposalData?.proposal ?? null,
messageId: proposalData?.messageId,
proposal: proposalResult?.proposal ?? null,
messageId: proposalResult?.messageId,
isLoading,
error,
refreshProposal, // Expose the refresh function

View File

@@ -47,7 +47,6 @@ export function useStreamChat() {
}
setError(null);
console.log("streaming message - set messages", prompt);
setMessages((currentMessages: Message[]) => {
if (redo) {
let remainingMessages = currentMessages.slice();
@@ -92,10 +91,10 @@ export function useStreamChat() {
if (response.updatedFiles) {
setIsPreviewOpen(true);
}
refreshProposal(chatId);
// Keep the same as below
setIsStreaming(false);
refreshProposal(chatId);
refreshChats();
refreshApp();
refreshVersions();