-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
94 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
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,42 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import toast from "react-hot-toast"; | ||
import { getToken } from "#/services/auth"; | ||
import { SaveFileSuccessResponse, ErrorResponse } from "#/api/open-hands.types"; | ||
|
||
const saveFileMutationFn = async (variables: { | ||
path: string; | ||
content: string; | ||
}) => { | ||
const { path, content } = variables; | ||
|
||
const response = await fetch("/api/save-file", { | ||
method: "POST", | ||
body: JSON.stringify({ filePath: path, content }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${getToken()}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to save file"); | ||
} | ||
|
||
const data = (await response.json()) as | ||
| SaveFileSuccessResponse | ||
| ErrorResponse; | ||
|
||
if ("error" in data) { | ||
throw new Error(data.error); | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
export const useSaveFile = () => | ||
useMutation({ | ||
mutationFn: saveFileMutationFn, | ||
onError: (error) => { | ||
toast.error(error.message); | ||
}, | ||
}); |
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,27 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import toast from "react-hot-toast"; | ||
import { Feedback, FeedbackResponse } from "#/api/open-hands.types"; | ||
import { getToken } from "#/services/auth"; | ||
|
||
export const useSubmitFeedback = () => | ||
useMutation({ | ||
mutationFn: async (variables: { feedback: Feedback }) => { | ||
const response = await fetch("/api/submit-feedback", { | ||
method: "POST", | ||
body: JSON.stringify(variables.feedback), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${getToken()}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to submit feedback"); | ||
} | ||
|
||
return (await response.json()) as FeedbackResponse; | ||
}, | ||
onError: (error) => { | ||
toast.error(error.message); | ||
}, | ||
}); |
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,38 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { getToken } from "#/services/auth"; | ||
import { | ||
FileUploadSuccessResponse, | ||
ErrorResponse, | ||
} from "#/api/open-hands.types"; | ||
|
||
const uploadFilesMutationFn = async (variables: { files: File[] }) => { | ||
const formData = new FormData(); | ||
variables.files.forEach((file) => formData.append("files", file)); | ||
|
||
const response = await fetch("/api/upload-files", { | ||
method: "POST", | ||
body: formData, | ||
headers: { | ||
Authorization: `Bearer ${getToken()}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to upload files"); | ||
} | ||
|
||
const data = (await response.json()) as | ||
| FileUploadSuccessResponse | ||
| ErrorResponse; | ||
|
||
if ("error" in data) { | ||
throw new Error(data.error); | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
export const useUploadFiles = () => | ||
useMutation({ | ||
mutationFn: uploadFilesMutationFn, | ||
}); |
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
Oops, something went wrong.