Refresh proposal & loaders for proposal action
This commit is contained in:
@@ -1,44 +1,73 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
import type { Proposal } from "@/lib/schemas"; // Import Proposal type
|
||||
|
||||
export function useProposal(chatId: number | undefined) {
|
||||
const [proposal, setProposal] = useState<Proposal | null>(null);
|
||||
// Define the structure returned by the IPC call
|
||||
interface ProposalResult {
|
||||
proposal: Proposal;
|
||||
messageId: number;
|
||||
}
|
||||
|
||||
export function useProposal(chatId?: number | undefined) {
|
||||
const [proposalData, setProposalData] = useState<ProposalResult | null>(null);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (chatId === undefined) {
|
||||
setProposal(null);
|
||||
setIsLoading(false);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchProposal = async () => {
|
||||
const fetchProposal = useCallback(
|
||||
async (innerChatId?: number) => {
|
||||
chatId = chatId ?? innerChatId;
|
||||
if (chatId === undefined) {
|
||||
setProposalData(null);
|
||||
setIsLoading(false);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
setProposalData(null); // Reset on new fetch
|
||||
try {
|
||||
const fetchedProposal = await IpcClient.getInstance().getProposal(
|
||||
// Type assertion might be needed depending on how IpcClient is typed
|
||||
const result = (await IpcClient.getInstance().getProposal(
|
||||
chatId
|
||||
);
|
||||
setProposal(fetchedProposal);
|
||||
)) as ProposalResult | null;
|
||||
|
||||
if (result) {
|
||||
setProposalData(result);
|
||||
} else {
|
||||
setProposalData(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");
|
||||
setProposal(null); // Clear proposal on error
|
||||
setProposalData(null); // Clear proposal data on error
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
},
|
||||
[chatId]
|
||||
); // Depend on chatId
|
||||
|
||||
useEffect(() => {
|
||||
fetchProposal();
|
||||
|
||||
// Cleanup function if needed (e.g., for aborting requests)
|
||||
// return () => {
|
||||
// // Abort logic here
|
||||
// };
|
||||
}, [chatId]); // Re-run effect if chatId changes
|
||||
}, [fetchProposal]); // Re-run effect if fetchProposal changes (due to chatId change)
|
||||
|
||||
return { proposal, isLoading, error };
|
||||
const refreshProposal = useCallback(
|
||||
(chatId?: number) => {
|
||||
fetchProposal(chatId);
|
||||
},
|
||||
[fetchProposal]
|
||||
);
|
||||
|
||||
return {
|
||||
proposal: proposalData?.proposal ?? null,
|
||||
messageId: proposalData?.messageId,
|
||||
isLoading,
|
||||
error,
|
||||
refreshProposal, // Expose the refresh function
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import { useLoadApp } from "./useLoadApp";
|
||||
import { selectedAppIdAtom } from "@/atoms/appAtoms";
|
||||
import { useLoadVersions } from "./useLoadVersions";
|
||||
import { showError } from "@/lib/toast";
|
||||
import { useProposal } from "./useProposal";
|
||||
|
||||
export function getRandomString() {
|
||||
return Math.random().toString(36).substring(2, 15);
|
||||
@@ -30,7 +31,7 @@ export function useStreamChat() {
|
||||
const { refreshApp } = useLoadApp(selectedAppId);
|
||||
const setStreamCount = useSetAtom(chatStreamCountAtom);
|
||||
const { refreshVersions } = useLoadVersions(selectedAppId);
|
||||
|
||||
const { refreshProposal } = useProposal();
|
||||
const streamMessage = useCallback(
|
||||
async ({
|
||||
prompt,
|
||||
@@ -94,6 +95,7 @@ export function useStreamChat() {
|
||||
|
||||
// Keep the same as below
|
||||
setIsStreaming(false);
|
||||
refreshProposal(chatId);
|
||||
refreshChats();
|
||||
refreshApp();
|
||||
refreshVersions();
|
||||
|
||||
Reference in New Issue
Block a user