useDeleteApp.ts raw
1 import React from "react";
2 import { toast } from "sonner";
3
4 import { App } from "src/types";
5 import { handleRequestError } from "src/utils/handleRequestError";
6 import { request } from "src/utils/request";
7
8 export function useDeleteApp(app: App, onSuccess?: () => void) {
9 const [isDeleting, setDeleting] = React.useState(false);
10
11 const deleteApp = React.useCallback(async () => {
12 setDeleting(true);
13 try {
14 // Delete the app/sub-wallet
15 await request(`/api/apps/${app.appPubkey}`, {
16 method: "DELETE",
17 headers: {
18 "Content-Type": "application/json",
19 },
20 });
21
22 toast("Connection deleted");
23
24 if (onSuccess) {
25 onSuccess();
26 }
27 } catch (error) {
28 await handleRequestError("Failed to delete connection", error);
29 } finally {
30 setDeleting(false);
31 }
32 }, [onSuccess, app]);
33
34 return React.useMemo(
35 () => ({ deleteApp, isDeleting }),
36 [deleteApp, isDeleting]
37 );
38 }
39