-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1713 from aalimsahin/refactor/client-react-query
refactor: client api
- Loading branch information
Showing
11 changed files
with
119 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./mutations"; | ||
export * from "./queries"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./sendMessageMutation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { CustomMutationResult } from "../types"; | ||
|
||
import { useMutation } from "@tanstack/react-query"; | ||
import { ROUTES } from "../routes"; | ||
import { SetStateAction } from "react"; | ||
|
||
export type TextResponse = { | ||
text: string; | ||
user: string; | ||
attachments?: { url: string; contentType: string; title: string }[]; | ||
}; | ||
|
||
type SendMessageMutationProps = { | ||
text: string; | ||
agentId: string; | ||
selectedFile: File | null; | ||
}; | ||
|
||
type Props = Required<{ | ||
setMessages: (value: SetStateAction<TextResponse[]>) => void; | ||
setSelectedFile: (value: SetStateAction<File | null>) => void; | ||
}>; | ||
|
||
export const useSendMessageMutation = ({ | ||
setMessages, | ||
setSelectedFile, | ||
}: Props): CustomMutationResult<TextResponse[], SendMessageMutationProps> => { | ||
const mutation = useMutation({ | ||
mutationFn: async ({ | ||
text, | ||
agentId, | ||
selectedFile, | ||
}: SendMessageMutationProps) => { | ||
const formData = new FormData(); | ||
formData.append("text", text); | ||
formData.append("userId", "user"); | ||
formData.append("roomId", `default-room-${agentId}`); | ||
|
||
if (selectedFile) { | ||
formData.append("file", selectedFile); | ||
} | ||
|
||
const res = await fetch(ROUTES.sendMessage(agentId), { | ||
method: "POST", | ||
body: formData, | ||
}); | ||
|
||
return res.json() as Promise<TextResponse[]>; | ||
}, | ||
onSuccess: (data) => { | ||
setMessages((prev) => [...prev, ...data]); | ||
setSelectedFile(null); | ||
}, | ||
onError: (error) => { | ||
console.error("[useSendMessageMutation]:", error); | ||
}, | ||
}); | ||
|
||
return mutation; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./useGetAgentsQuery"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Queries { | ||
AGENTS = "agents", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import type { CustomQueryResult } from "../types"; | ||
import { Queries } from "./queries"; | ||
import { ROUTES } from "../routes"; | ||
|
||
export type Agent = { | ||
id: string; | ||
name: string; | ||
}; | ||
|
||
export const useGetAgentsQuery = (): CustomQueryResult<Agent[] | undefined> => { | ||
return useQuery({ | ||
queryKey: [Queries.AGENTS], | ||
queryFn: async () => { | ||
const res = await fetch(ROUTES.getAgents()); | ||
const data = await res.json(); | ||
return data.agents as Agent[]; | ||
}, | ||
retry: (failureCount) => failureCount < 3, | ||
staleTime: 5 * 60 * 1000, // 5 minutes | ||
refetchOnWindowFocus: false, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const ROUTES = { | ||
sendMessage: (agentId: string): string => `/api/${agentId}/message`, | ||
getAgents: (): string => `/api/agents`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { UseMutationResult, UseQueryResult } from "@tanstack/react-query"; | ||
|
||
export type CustomMutationResult<TData, TArgs = unknown> = UseMutationResult< | ||
TData, | ||
Error, | ||
TArgs, | ||
unknown | ||
>; | ||
|
||
export type CustomQueryResult<TData, TArgs = unknown> = Omit< | ||
UseQueryResult<TData, TArgs>, | ||
"data" | "refetch" | "promise" | ||
> & { data: TData; refetch: () => void; promise: unknown }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters