create useGetNudgesQuery to get nudges easily
This commit is contained in:
parent
33ba6abf49
commit
1197f51518
1 changed files with 42 additions and 0 deletions
42
frontend/src/app/api/queries/useGetNudgesQuery.ts
Normal file
42
frontend/src/app/api/queries/useGetNudgesQuery.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import {
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query";
|
||||
|
||||
type Nudge = string;
|
||||
|
||||
const DEFAULT_NUDGES = [
|
||||
"Show me this quarter's top 10 deals",
|
||||
"Summarize recent client interactions",
|
||||
"Search OpenSearch for mentions of our competitors",
|
||||
];
|
||||
|
||||
export const useGetNudgesQuery = (
|
||||
chatId?: string | null,
|
||||
options?: Omit<UseQueryOptions, "queryKey" | "queryFn">,
|
||||
) => {
|
||||
const queryClient = useQueryClient();
|
||||
console.log(chatId);
|
||||
async function getNudges(): Promise<Nudge[]> {
|
||||
try {
|
||||
const response = await fetch(`/api/nudges${chatId ? `/${chatId}` : ""}`);
|
||||
const data = await response.json();
|
||||
return data.response.split("\n").filter(Boolean) || DEFAULT_NUDGES;
|
||||
} catch (error) {
|
||||
console.error("Error getting nudges", error);
|
||||
return DEFAULT_NUDGES;
|
||||
}
|
||||
}
|
||||
|
||||
const queryResult = useQuery(
|
||||
{
|
||||
queryKey: ["nudges", chatId],
|
||||
queryFn: getNudges,
|
||||
...options,
|
||||
},
|
||||
queryClient,
|
||||
);
|
||||
|
||||
return queryResult;
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue